How is concurrency used with Node.js

 


HOW IS CONCURRENCY IMPLEMENTED WITH NODE ? 


Concurrency means a program is able to run more than one task at a time this is not to be confused with the concept of parallelism. During concurrency , different tasks with different goals can be performed at the same time , while in parallelism different parts of the program execute one task.

NodeJS uses an event loop to maintain concurrency. An event loop is something that pulls stuff out of the queue and places it onto the function execution stack whenever the function stack becomes empty.

As soon as NodeJS starts , it initializes an event loop. The event loop works on a queue ( called an event queue) and performs tasks in FIFO ( First In First Out ) order


Asynchronous programming is a technique that enables your program to start a long running task as well as respond to other events while that task is running or being processed rather then having to wait until a single task has been completed.

You can write code as tasks , which are then executed concurrently. Executing concurrently means that all tasks are likely executed at the same time.

Restful APIs are a perfect example of concurrency , Users may make requests and receive responses all dependent on what request has been declared will determine what status code should be sent as well as what information to provide to a user. 

While there are many differences between oracle and mongoDB in terms of supporting data concurrency they both have one thing in common , both implement the concept of  locking and concurrency control to prevent users from carrying out destructive actions while allowing multiple users and clients to read and write the same data.



Comments