Recently, at work, i was tasked with providing a training session to NodeJS to help adopt
node to our organization.
This post is a recap on what i covered on:
What is NodeJS?
- Single-threaded event loop, Non-blocking I/O.
- NPM for package management.
- Currently version 8.4.0
asynchronous I/O to
node. It should also be noted that the kestrel ( aspnet core http server ) is also leveraging
Why Non-Blocking I/O?
NodeJs is designed with non-blocking evented I/O, all I/O calls are returned on
callbacks. This is very different from traditional frameworks in the sense when an I/O operation is happening, no application threads is blocked.
The nature of most modern web servers are mostly I/O bound by nature. Blocking I/O is not scalable because each I/O would hold up a thread. For example, if we have 1000 I/O calls happening, then there are at least 1000 OS threads created, consuming 1000 * 10 MB of memory, context-switching in the cpu would also be really high. This is an inefficient concurrency model. Most languages/frameworks are not designed with this in mind.
- Non-blocking I/O supports tens of thousands of concurrent connections.
- Non-blocking IO single-threaded concurrency model.
- Can scale to high number of concurrent requests.
- Blocking I/O handle concurrency by having more threads.
- More threads = more memory and cpu context-switching.
- But Non-blocking IO without language support make code hard to read/write.
Non-Blocking I/O frameworks:
- Go https://golang.org
- C# https://www.microsoft.com/net/core#macos
- Play framework. https://playframework.com
- Java NIO https://docs.oracle.com/javase/7/docs/api/java/nio/package-summary.html
Language support for concurrency
Language support is important to prevent spaghetti code when writing concurrent code. The callback hell in nodejs early days is a good example.
With the new addition of
await in nodejs 8, concurrency support is awesome. https://blog.risingstack.com/mastering-async-await-in-nodejs/
C# are by far the best languages with concurrency support baked into the language.
- Used to install node program/modules.
- Specify dependencies in a file call package.json.
- Modules get installed into
- NPM version 5 generate
package.lock.jsonto guarantee similar dependencies are always restored.
- Alternative to npm: Yarn
NodeJs is a pretty decent choice for building apps that require high I/O scalability.