Node.js – under the hood

24 Nov

If you follow news in web application development, you probably get in touch with Node.js, one of the hot topics for couple of last months. Let us take a look what we can find under the hood and explain, why Node has such a big attention of developer community.

Node is the server framework by Ryan Dahl. Its high-performance network applications framework, well optimized for high concurrent environments.

Node and V8

Node is created in C and JavaScript. It does most of the things in pure JavaScript, C implements thin layer to do the system integration.

Node.js architecture stack

Its running on top of the V8 JavaScript engine, which is developed as open source project by Google and powers their Chrome browser. V8 is led by Lars Bak, one of the best VM engineers, designer of high-performance virtual machines for Java and Smalltalk. Lars and his team of developers in Denmark are constantly improving V8 and leading the innovations in JavaScript world.

Why to use Node?

Node is standing on really solid ground using the V8 JavaScript interpreter to run its JavaScript. To keep high performance, Node is designed to be fully event driven server solution. Its inspired by systems like Ruby Event Machine and Python’s Twisted, but it takes this event model much further using the server-side JavaScript, which has the events as key concepts deeply integrated to the language core. This approach brings you lot of benefits. Let me quickly go through major ones:

1. Lower memory consumption

All the webservers out there have to face the concurrency environment and handle work with many clients at the same time. To allow you do that, they run in multiple threads. In this model its standard situation that one connection is handled within one server thread. If you combine it with fact that each thread allocates its own heap memory stack, you will easily get conclusion that threaded model is not really effective in memory usage.

Node has different strategy for that. Event driven code based on the infinite event loop allows it to work in totally different way – in one thread. This way Node avoid the need to allocate large heap memory objects for each client. It keeps simple workflow:

1. First think what you do is to register a listener function – callback. This way you say which function will run the code in case event is fired.

2. Server is waiting in infinite loop for an event within the event loop.

3. Once the event is fired Node run the callback code. Once callback is done with its work, server get back to the event loop waiting for another one.

This way every action is asynchronous and no operation blocks. From its principle Node cant allow you do that, because you are still working in one thread.

2. Non-blocking operations

Node architecture is build with non-blocking as one of the key concepts. That mean each time, you want to read from the disk, send data to socket or access your database, you need to provide the callback. After you perform the action, Node is free to do any other operations till your data are ready. Once everything is on the place (or an error occurs) your callback is triggered and allows you to take control back. You will realize strength of this behavior while working on the cloud applications. In cloud running on many separate machines connected with networks, there is lot of place for high latency operations, which can block your code. In event based architecture you dont expect the result to be ready immediately. Instead of that, you provide the callback and let your server do another more important work then waiting for the response.

3. Better usage of CPU power

Node take care about lowering usage of CPU too. In traditional thread model, server has to spawn threads and switch the context constantly between them. This operations are expensive and causing CPU usage overhead along with the possibility of dead-lock and another thread programming problems causing many developers lot of gray hairs.

Node avoids this problems running rather in single thread using the event loop and callbacks. If you think its not much effective to use just one core of your CPU, nothing stops you from running multiple separate Node instances and load balance them with proxy like Nginx.

For a better comparison of this two approaches i recommend you article Benchmarking Node.js, where author compares Apache (threaded model) vs Node.js (event model).

Conclusion

I hope i gave you basic information about Node.js. If you are thinking about usage, you will benefit in systems, where you work with real-time game engines, chat systems, collaboration tools or other similar application demanding communication of many clients at the same time. Node code is still in development but is more stabilized in couple last months and allows you to build real production systems already.

Leave a comment