Scaling to One Million Requests
One million requests doesn’t mean one million online users. The number of users are generally much less, as multiple requests are required to serve just one user. Serving a high volume of requests is not a very uncommon scenario these days. If you have to develop a software solution that can handle these many requests, which language you should use? Java, .Net, Ruby, php, Python? Along with the language, which technology stack/framework will you use to build the application?
All of these languages approach the scalability problem in a similar way, i.e. by applying the common technique of using a new thread to serve a new request. Memory usage is generally directly proportionate to the number of threads used, which limits the number of threads that can be used in a server. In order to scale we first try to find the maximum number of threads that can be used by a server at a given time. And then based on this number, we determine how many servers should be added behind a load balancer.
Hardware is cheap these days, so can an addition of more servers be a cause for concern ? Yes, it is a cause for concern since with the increase in the number of servers comes additional cost of maintenance, cost of code deployment, and the cost of isolated testing and verification on each of these servers. More servers means more overhead cost, thus requiring a new team for managing the deployment and verification on these servers. Scaling horizontally (adding more servers) can appear to be a cheap solution considering just the hardware, but in reality it is not an efficient one.
Node.js takes a completely different approach for serving incoming requests. It uses single thread and an event loop for processing all the requests. If we are doing many network or database calls, like the way we do in most of our web applications these days, then this approach proves to be a really efficient one. In Node.js whenever we make a network/DB call, we pass a callback function, which is called when we get the result. The event loop monitors if the result is available, and whenever it is available, it uses the same thread to process that callback function. Meanwhile the thread can be used for serving other requests or doing other queued up tasks. There are a plenty of benchmarks which prove that Node.js’s approach is much more efficient for developing an application which has a lot of I/O. It can help in reducing the number of servers by a big factor. Having a single thread can be inefficient if we are doing a lot of computation, as that thread will be busy doing that computation and will not be able to serve other requests. Bearing in mind all of this, it does makes sense to consider Node.js as one of the first choices for developing applications that need to be scaled for huge number of users.
Developing Interactive Web Applications
All the major organizations are moving away from a heavy monolithic software approach and adopting a more distributed one - dividing the software into thin vertical layers, and exposing services for each of these layers. Each of these services should be capable of being deployed separately in lightweight containers. Since the whole software is divided into different services, a large number of containers are required for deploying all of these services.
One of the Node.js modules, ‘express’ makes it very easy to write and deploy these services. Using Node.js library, ‘http’ or module ‘express’, it’s very easy to create a simple http server and then expose these services through it. These services are capable of running by themselves and don’t need a container, thus making Node.js an ideal solution for some of these cases.
Use cases mentioned above are some of the very common use cases, and Node.js proves to be a promising alternative for writing applications for one of these cases. But, just like any other technology, Node.js has its own set of disadvantages such as a paradigm shift for developers to write even driven code rather than normal sequential code, modules of node.js are not mature, lesser IDE support etc. But looking at the advantages, these disadvantages are minor for many cases. I would recommend that before deciding on technology for the above cases, do consider Node.js as a possible alternative.