How we optimized a MEAN Stack project for better performance

MEAN Stack – Performance Optimizations

In the past few years, MEAN stack went from ‘yet another shiny JS thingy’ to being a viable alternative to build high-performance web applications. We have quite a few medium to large apps running, that are MEAN.

One of our recent MEAN projects saw rapid growth in the user base (hundreds of thousands of users within the first few weeks), resulting in the system hitting the initially designed max-loads much earlier than anticipated. Hence we had to review the architecture and performance parameters to identify and fix the bottlenecks.

This blog is a quick rundown of the whats and hows in our initial performance tuning. Since the system was hosted in AWS, we used Amazon services wherever possible.

We implemented the following to improve the MEAN Stack project performance.

  1. NodeJS Clustering
  2. Amazon S3 as CDN
  3. Mongo replication
  4. Microservice implementation

Load Balancing

When it comes to increasing the performance of websites and web services, there are a couple of options:

  • Improve the efficiency of the code.
  • Throw more hardware at it.

Option two (throwing in more hardware) is the fastest and easiest solution to mitigate the performance woes.

Let us discuss how we can scale the Node.js server infrastructure using ELB, which stands for Elastic Load Balancing. ELB automatically distributes the incoming application traffic across a group of backend servers. The load balancer routes the incoming requests to the second server when the first server is busy processing other requests so that the end user does not experience any delay in getting the response.

  1. Application Load Balancer
  2. Network Load Balancer
  3. Classic Load Balancer

Application Load Balancer is best suited for load balancing of HTTP and HTTPS traffic and provides advanced request routing targeted at the delivery of modern application architectures, including microservices and containers. Operating at the individual request level (Layer 7), Application Load Balancer routes traffic to targets within Amazon Virtual Private Cloud (Amazon VPC) based on the content of the request. The socket forwarding is supported only in the application load balancer.  So we have used the Amazon AWS application load balancer to scale the server architecture.

The decision on how many servers to be used in the load balancer depends upon the traffic expected in the server and the number of concurrent requests the server needs to handle. Moreover, it also depends upon the system configurations in which the server is hosted such as the number of cores, memory, etc. We also need to change some web server(ex: nginx or apache) configurations as described here.

To discover the availability of your EC2 instances, a load balancer periodically sends pings, attempts connections, or sends requests to test the EC2 instances. These tests are called health checks. The status of the instances that are healthy at the time of the health check is InService. The status of any instances that are unhealthy at the time of the health check is OutOfService. The load balancer performs health checks on all registered instances, whether the instance is in a healthy or unhealthy state.

The load balancer routes request only to the healthy instances. When the load balancer determines that an instance is unhealthy, it stops routing requests to that instance. The load balancer resumes routing requests to the specific instance when it has been restored to a healthy state.

The load balancer checks the health of the registered instances using either the default health check configuration provided by Elastic Load Balancing or a health check configuration that the developer configures. In the second case, the developer has to give one health checking URL ({main domain}/health) to the IT team (who are doing the ELB implementation), which serves as the health status of the instance.

The health URL should be an API configured in Node.js program, which connects to the database and fetches some lightweight data to make sure that the instance is properly working i.e, it ensures the health of web server (Nginx), server (Node.js instance) and the database server (MongoDB). If this API could respond with the HTTP status 200 within a particular time (Say 2 seconds, which we can set in the load balancer configuration), the instance is marked as healthy, otherwise, it’s marked as unhealthy.

The load balancer periodically (Say 10 sec, which we can set) checks the health status of each instance (If we scaled to 2 servers – checks these 2 instances) through the corresponding health URL, to mark it healthy or unhealthy. So, when the next request comes, the load balancer routes the request to a healthy server instance which is available.

Empower your Business with Mobile Application

iOS App Development: Grow Your Business With An iOS App

With smartphones and tablets emerging as the device of choice for most people, apps are in a boom phase. Businesses are joining the bandwagon in a big way, rolling out apps that offer a world of convenience to their customers and employees alike. However, while doing so, they need to be wary of losing focus.

Apps are not just another channel to execute the entire gamut of organizational functions. The most effective apps serve specific purposes, and in the process empower the business.

Businesses also need to take a serious look at iOS application development, considering iOS apps have proven to be very engaging. An average iPhone user spends about 75% of her time on apps, probably owing to iOS applications being able to leverage the advanced features and functionality of Apple devices.

Here are some ways in which you can empower your business, using iOS application.

Roll Out Customer-Facing Apps

Customer facing iOS app is a good idea, considering Apple devices, such as the iPhone, has more than a 15% share of the total smartphone market. Such apps not only promote sales but help in engaging with customer better. Marketers may send out push notifications on key milestones, keep track of customer behavior, issue special offers when a customer is in the vicinity of a store, and do more. Customers may be given several self-service options, from keeping track of loyalty points to access of neutral product reviews, and more.

Empower Internal Teams

Imagine the world of convenience when the salesperson, on meeting with a potential or existing customer, pulls out the iPhone, opens an app that draws on data from the CRM, and gather all details about the customer, right from his purchase history to open inquiries, his preferred channels of communication, and so on. The salesperson also gets highly customized tools and relevant insights, such as market surveys, reports, and more. iOS apps facilitate similar empowerment across all functional area, be it accounts, HR, logistics, and other teams.

Forge Deep Relationship with the Ecosystem

More and more businesses seek to develop apps that help the company forge deeper relationships with their ecosystem partners, such as suppliers, contractors, and others. An app that makes explicit real-time stock levels and issue alerts would prompt suppliers to make timely deliveries, and improve the efficiency of their business as well. Contractors could log into another intuitive iOS application to get a list of open projects, deadlines, billing, and more. Channel partners could also gain from first-hand access to a ton of resources, and all the information they require, without having to make follow-ups, or spend time seeking information.

Facilitate the “Internal” Customer

An iOS app could likewise benefit the “internal customer” of the enterprise. Employees could, for instance, use an internal employee app to make leave requests, access their payroll details, stay updated on everything from upcoming team meetings to office parties, and more. Such apps eliminate complicated and time-consuming paperwork, be it filing trip sheets, expense reports, or appraisal forms. With the bulk of information readily available at their fingertips, employees can focus wholeheartedly on their core tasks, improving their productivity manifold.

Transform Organizational Processes

Rolling out iOS applications could even eliminate certain traditional functions altogether. For instance, a training app that delivers instructional videos, handbooks, and other resources to the employee’s iPhone could completely eliminate the need for training sessions. The cost saved in terms of arranging training resources and the trainer’s time alone would repay the investment in the app many times over. Employees could also leverage such apps to unlock training and development opportunities not available before. The impact on employee performance and commitment are worth its weight in gold.

All the possibilities come to pass only when the app development is done right. iOS developers would do well to customize enterprise apps by giving it a distinct look and feel that resonate the brand image, offer appropriate options such as choice of languages, flexibility to enable or disable certain features, location mapping and more. The ability to customize the app and the presence of value-added features make the employee feel more connected and enhance the worth of the app.

Development of iOS apps is a far more straightforward process, compared to developing Android apps, as Apple provides standardized SDKs, APIs, and IDE needed for the application development. However, Apple’s app store discards apps that do not meet its guidelines, and as such iOS app development requires a much more concentrated and focused effort. A partnership with a reputable company to develop the iOS application helps pass the iTunes test and ensure the effort invested in the app development process does not go waste.

 

Kotlin over Java for Android Development

Write once, run anywhere – the alluring catchphrase of Java still seems relevant. Since Sun Microsystems unveiled Java, it has remained the most popular programming language in use. Object-oriented and class-based, Java stood its ground even when the development space became saturated with a plethora of new languages. However, with time, programming languages do get phased out once newer and better platforms take over.

And then came Kotlin. The search for a tenable Java alternative reached fruition with Kotlin, a statically-typed programming language running on the Java Virtual Machine (JVM). Essentially, Kotlin stripped Java of its complexities and came up with a refined alternative that is as versatile as its predecessor.

The Czech software company JetBrains unveiled Project Kotlin in 2011 with the objective of coming up with a better alternative to Java. They looked to Scala, another Java alternative with support for functional programming, but were taken aback by its slow compilation times.

Kotlin Goes Public
In 2016, JetBrains launched Kotlin v1.0, its official stable release to the public. Kotlin immediately gained traction among the Android development community. They lauded its refined framework and faster compilation times, complete with added tool support, as a significant advantage over Java. Things gained momentum when Google announced Kotlin as a first class language for developing Android apps at the I/O 2017.

What followed was an outburst of mainstream apps all written fully or partially with Kotlin. Kotlin had everything that developers couldn’t find in Java or any of its alternatives like, JRuby or Groovy. It is concise and 100 percent interoperable with Java. Added to that, Kotlin came with extensive compatibility with Android Studio in which IntelliJ IDEA, JetBrains’ own Java IDE serve as the backbone.

With interest in Kotlin skyrocketing, global adoption rates point to a widespread disruption. From freelance developers to software development houses, the adoption of Kotlin has increased manyfold. In the Stack Overflow Developer Survey Results of 2018, Kotlin is just behind Rust as the most loved programming languages. In the near future, Kotlin could become the de facto standard in Android development.

Kotlin’s Development Philosophy Strikes a Balance
Andrey Breslav, Kotlin’s lead language designer at JetBrains highlighted that the goal of Kotlin is to serve as a general-purpose language, which is concise, adaptable and fully interoperable with Java. By giving these extended set of functionalities, JetBrains intends to push the developer to migrate into Kotlin. Plain old Java has numerous flaws and the developers at JetBrains couldn’t conceive things as they wanted. For them, greater interoperability and productivity in the language is the need of the hour to quickly compile and execute code.

When working with Kotlin, we could easily pinpoint its structural similarities with its parent language, Java. The brains behind Kotlin did not just want to create a new language, but one which is in all ways a “better language” than Java. Platforms like Scala, although they offered extensive feature list and functionality, failed when it comes to build times and tool support. Kotlin managed to tick all the right boxes and struck a balance on how an ideal Java alternative should be.

Kotlin borrows some elements from Scala to bring out a more upgraded version. In fact, one could find that Kotlin does contain certain instances from other programming languages as well. Pascal, an early procedural and imperative programming language remained fairly influential in the development of Kotlin. Certain elements of it, like parameter lists and variable declarations with the data type following the name of the variable could be found in Kotlin too.

Bringing Full-Scale Interoperability with Java
What distinguishes Kotlin from other languages is its full interoperability with Java and the Android runtime. It is easier to call existing Java codes from Kotlin and Kotlin codes from Java. Developers could use all the Java frameworks to build highly responsive and feature-rich apps. Both the platforms, Kotlin and Java, identify each of their classes as their own, which enables seamless interchange and compatibility. This interoperability with Java could be demonstrated with a few lines of Java code as listed below:

MainActivity.kt

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

// accessing « getSum » method from a java class

val result = SumHelper.getSum(1, 2)

}

}

Java Lacks Key Elements that Current Developers Need
Java has been around for more than two decades. Developers still continue to build great apps using its highly versatile and robust framework. Like every language, Java too has its limitations chiefly because it is old. Even though numerous updates were released over the years, they failed to address some essential problems associated with coding using Java.

When it comes to Android development, Java lacks support for key elements like lambdas, streams, method references, etc. Null Pointer Exception continues to be a fundamental flaw of Java, particularly in the way it handles them. Null references in code often referred as the Billion Dollar Mistake is the key reason why several apps written in Java still undergo occasional crashes.

Moreover, Java is verbose and takes a lot of code to get even a simple program up and running. The APIs involved in Java get rather complicated as the developer is forced to go through multiple steps, which take more time. Kotlin, on the other hand, requires lesser coding than Java by reducing both redundant and boilerplate code, making development a whole lot faster.

Android Development Gets Swift and Efficient with Kotlin

Kotlin aims at being pragmatic and productive at the same time all the while serving itself as a general purpose language fully compatible with Java. Owing to its compactness, Kotlin has proved itself a workhorse among the Android development community. It steers clear of all the limitations of Java with its full interoperability with the existing ecosystems of libraries and code. Here are a few reasons why Kotlin remains the ideal platform for Android development currently.

Developer Friendly:

Novice developers can easily learn and adapt Kotlin owing to its simplicity. It takes less time to learn to code with Kotlin due to its uncomplicated learning curve. Developers already proficient in Java would appreciate the familiarity of its coding process and streamlined approach to programming. Meanwhile, the availability of existing Java libraries, frameworks and lesser coding, amps up the speed of coding, allowing developers to conceive applications faster than before.

Excellent IDE Support:

IntelliJ IDEA, JetBrains’ own platform serves as the base for Android Studio and thus offers extensive IDE support to Kotlin allowing developers to seamlessly build apps. This ready integration with the IntelliJ IDEA framework enables access to its wide range of productivity tools. Configuring Kotlin plugin could be undertaken within minutes in Android Studio. Besides, every single process of coding in Kotlin from writing, navigation, debugging and unit testing happens effortlessly with the improved support offered through the IDE.

Enhanced Versatility:

Kotlin was designed to address some of the fundamental flaws in Java and it did well. Integrated into its framework is a built-in null checking feature that helps avoid frequent errors. Kotlin’s compiler also includes a feature that automatically corrects mistakes in the code simultaneously while it is being written in the console. These rich features negate several issues in Android development giving developers a clean and versatile platform to work with. Besides, it allows developers to streamline the production process through its features like search and auto-completion of code.

Open source:

JetBrains announced open source status under Apache 2 for Kotlin in 2012. The Java to Kotlin converter tool can seamlessly convert an existing Java project into Kotlin to save on time and work. Besides, it is completely free to use for any developer wishing to expand their skill set or get acquainted with its refined framework.

Summing Up

Every new language introduced into programming has the potential to disrupt it. Developers, when faced with limitations while working on their existing language readily adopt a new language that mitigates these issues. And Kotlin proved to be just that with Java. Its swiftness and concise nature proved instrumental in its widespread adoption among the Android development community.

For them, Kotlin seemed a better Java and its great interoperable support combined with expressive nature meant they could code just like they did with Java but in a fast and efficient manner. That does not mean that it will completely phase out the former. Java will still be around as a versatile language for years.