Handling Cooperative Multitasking using Node.js

Node.js and its multi-tasking over the years have been embraced by companies like Uber, eBay, and various large enterprises. Microsoft's SharePoint online road map for SPFX Node.js has replaced the traditional ASP.Net model. This is a clear shift from Multi-thread model to a single thread model now.

This article is about handling cooperative multitasking in a single thread model. However, I'm splitting the blog into parts that essentially revolves around 3 different things:

1. Multi-threading and Multitasking concept

2. Single-thread and multitasking using Node.js

3. How did we achieve cooperative multitasking using Node.js for our problem?

This primarily increases readability and I want to get a little deeper into these things to make users clearly understand each one of them.

To refresh a bit on what cooperative multitasking is… the processes running in the applications yield control to another process when it's either idle or blocked or based on some scheduled job. This enables multiple applications to cooperate and run concurrently.

Multi-Threading and Tasking: A familiar approach

Many backend servers wait for a lot of resources for response and a few instances to mention - fetching data from a database, reading a file from a disk, etc. The traditional ASP.Net model creates multiple threads and each thread "waits" (holds on) until it gets a response. This cannot be a practical approach for many reasons:

  • Results can sometimes be unpredictable
  • Higher possibility for potential deadlocks
  • Complex debugging and testing
  • Synchronization of shared resources
  • Starvation: a process may not get resources at all, etc.

Preemptive Multitasking

Though preemptive multitasking approach entails fair access to all the processes, the problem with it is that the preemptive scheduler gives equal access to resources to all the threads. This might ensure that every thread makes progress but efficient utilization of resources becomes a question here.

Cooperative Multitasking

On the contrary, the scheduler gives exclusive access to computing resources to a task until it is completed or until the task yields access to another task and is mostly preferred over preemptive multitasking to efficiently schedule work. However, Cooperative scheduling executes multiple threads to run concurrently without sharing resources that can reduce synchronization overheads and increases efficiency.

Concurrency Runtime

Having said that, cooperative multitasking does not solve all scheduling problems because there can be instances where a task utilizes all the resources preventing other tasks from making progress. To handle such scenarios, Concurrency Runtime leverages benefits of both preemptive and cooperative scheduling. So, the Concurrency Runtime uses cooperative scheduling to efficiently distribute work among resources and relies on preemptive scheduler to fairly distribute resources among applications.

Though, multitasking is accomplished using the multi-threaded approach, going with any of the above-mentioned models can make one or more processes wait for resources until it is freed up. This can be problematic and delay the execution process. That's the reason why we go for Node.js that can cooperatively handle multiple tasks using a single thread.

Start 1 2 3 End

Related Blog

Contact Us

*All the fields are mandatory.

+1 732 737 9188