Wednesday, 28 March 2018

Nodejs - avoid blocking the event loop

I have reading a lot about the event loop in Nodejs, the fact that it is "single threaded" and that you should not block the event loop.I am trying to understand if Node.js is a good choice for the following scenario, because I guess I still haven't figured that out.I have sensors that every 5 minutes collect some data (for instance, think of the temperature) and then send data to the server. When the sensors are online, no problem. They send very little information (basically the body can be summarized as [timestamp, sensor_id, temperature]). The sensors retain the data up to a certain amount of time (depends from sensor to sensor, could be like 6 months). Unfortunately, these sensors are not reliable and it could happen they they get stuck, blocked, or, anyway, for whatever reason, they are not able to send data for a certain period of time. The period of time could even be months long (i'm not gonna explain why, it's bullshit, but that is what it is).The sensors are not-so-smart. If they were blocked, whenever they go back online they send everything they didn't send in a single , one-time, POST request to the server.The point is, the server receives the requests and has to do some work on every single data contained in the body before writing it into the database. The work is simple but it's a CPU operation (it's just a simple sum), nothing really CPU-intensive like AI or whatever else. This means that the computational complecity is O(n), so it grows linearly with the number elements of the body.Most of the times the requests are super simple and short and contain a single data record, so the complexity is basically O(1), but when it happens that a sensor sends a huge packet, the backend would have to loop over it and this would block the event loop thus making the server unresponsive.I am wondering what would be the good and proper way to handle such a load using Nodejs.Any suggestion is appreciated in order to seek the best solution and learn Nodejs better.

Submitted March 28, 2018 at 02:12PM by honestserpent

No comments:

Post a Comment