If you're coming to Node.js from a more synchronous language/environment such as Ruby on Rails or Java, the concept of asynchronous processing can be confusing. All the literature talks about Node being async, so it's easy to get the understanding that it's all asynchronous. But not every method in Node is async and this is important to be aware of. Likewise, not all Node modules are asynchronous either.
Other frameworks sometimes have a feature baked in that lets you do "asynchronous" or deferred processing of jobs. For example, while RoR is not async by default it contains the Active Job framework that let's you queue up jobs for background processing, preventing the web request from having to wait to return.
But because Node is async it takes care of this for you, right?
Let's imagine a likely real-world scenario: a sign-up request for a service comes in for a new user. There are a lot of back-end processes that need to take place in one or more services in order for the user to be officially registered, but we don't want to make the user wait that long to get the message on the UI that their registration is complete.
Assuming Node handles everything async, you might think the solution for this would look like this:
But your code might block the event loop, making it actually look like this:
The blocking code could be synchronous file system methods or even just a heavy amount of data manipulation you might need to do.
If you have a lot of orchestration you need to do that the client (in this case, the UI/user) doesn't need to wait for, then process as much of it in the background as possible to speed things up. The queue solution would look something like this:
Queues can be very powerful and can also get quite complex, but you can get a lot of mileage out of the basic pattern above. Node - as well as its most popular web framework, Express - does not handle queueing by itself.
Note that we didn't cover the queue processing itself, but that is for another post. Just remember that if you're still new to Node or have been working with it for some time and haven't fully grasped asynchronous concepts - Node is not "fire and forget"; you need to design at the architecture level around it's asynchronous patterns and understand what will be synchronous and blocking.
Subscribe for more Node.js content delivered directly to your inbox
No spam ever. Unsubscribe any time.