vuvu
vuvu

Reputation: 5338

Why is Node.js scalable?

node.js scalable, what is meant by the that? What part of a node.js server is scalable. I read that it is a single-threaded technology that is not suitable for applications that would need a lot of CPU resources. These facts do not fit with scalability, so what is meant by that?

Upvotes: 26

Views: 18577

Answers (4)

clever_bassi
clever_bassi

Reputation: 2480

All APIs of Node are written is such a way that they supports callbacks.

For example, a function to read a file may start reading file and return the control to execution environment immidiately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result. -- Tutorial's Point.

Upvotes: 0

FrameGrace
FrameGrace

Reputation: 603

Is not that is more scalable "per se". Is more that all you do is (I/O) scalable without having to do anything special.

I/O is safer and easier to do in parallel, as it tends to share no data between execution threads. Node.js lets you do it using event programming which is simple, elegant and easy to use. Is an old and proved programming paradigm, used for years by GUIs and other graphical intensive apps like games for example.

In fact is less scalable than full fledged languages like c++, c, java, etc.. which can scale out much better by the usage of full multithreading. This allows to scale CPU too, but also opens a can of worms. To share CPU, you have to share data, and that is another story (semaphores, locks, etc...)

You can do the same as node.js with any of the above languages, but is not part of the language itself, so you have to roll your own or use libraries that provide it to you. That said, is not that hard, but sure harder than in node.js.

Most web services, are IO bound, so Node.js fits well, and is Ok for most cases. But once you start to use CPU intensive work, the events are not serviced and all stops. In that case better use another language. There's no really a good solution in Node for that. You can spawn multiple processes, but then you will not be able to share data between them. Without data sharing, there's no way to scale CPU efficiently, so better don't try.

Use Node.js for IO, and some better suited language with proper multithreading for CPU intensive work.

Upvotes: 4

jedigo
jedigo

Reputation: 911

The javascript that node runs is single threaded, but a lot of the things you call in node - such as network or file io - run in background threads. See this post for a basic overview: Node is not single threaded

If you need the gritty details, you should look into libuv which is the 'magic' piece converting threads into event loops: http://nikhilm.github.io/uvbook/basics.html#event-loops

Additionally, if you need to do something CPU intensive in node itself, you can easily send this to a child process - see http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options for details

Upvotes: 19

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

it is scalable due to load balancing. Essentially you can have multiple jobs for node to process and it can handle it with no significant burden. This makes it scalable.

Upvotes: 2

Related Questions