Concurrency

  • The main loop is single-threaded and all async calls are managed by libuv library.

libuv

Synchronous functions

  • Node.js provide some functions like fs.readFileSync() which is blocking
  • It is not possible to implement synchronous code like the above since it is implemented internally
  • Since Node.js philosophy is Non-blocking I/O hence functions like xxxSync() goes against it.
  • We should never call xxxSync() functions in an active server code since this could block server.

Why Node.js is Non-Blocking I/O

  • I/O is slow:
    • Accessing RAM is in order of nanoseconds (10^-9)
    • Accessing data on disk or network is in order of milliseconds (10^-3)
  • Not good for server

Busy Waiting

  • Busy waiting simply refers to polling the non-blocking asynchronous resources until some actual data is returned;
  • In this mechanism, a call to resource is immediately returned and we poll the resource again and again
  • This is very inefficient since for most of the time the resource is no ready, hence Polling algorithms usually result in a huge amount of wasted CPU time.

Event de-multiplexer

  • aka Synchronouse Event demultiplexer or Event notification Interface
  • Each operating system has its own interface for the Event Demultiplexer:
    • epoll: Linux
    • kqueue: Mac
    • I/O Completion Port API (IOCP): Windows
  • Each I/O operation can behave quite differently depending on the type of the resource, even within the same OS.

Similar Concepts

  • Event Driven Architecture (EDA)
  • Reactor Pattern (has Event demultiplexer??)
  • Reactive Programming
  • Observer Pattern
  • Pub/Sub Pattern