How to deliver messages in the correct order in NodeJS?

Viewed 52

I have a node server deployed , It's job is to update the database of the connectivity status of IOT devices

Problem

  • Since NodeJS executes asynchronously and is single threaded let's assume I receive the status something like Disconnected and Connected simultaneously.
  • Sometimes what happens is that due to NodeJS being asynchronous , Connected gets processed first and Disconnected gets processed next.
  • This leaves my database in an inconsistent state.

Result Wanted

  • I want to update the database in order at which it happened at source ( i.e According to Timestamp )

So how do i achieve this if NodeJS is single threaded and each request does not share any memory with each other whatsoever ?

2 Answers

I would suggest that rather than keeping a "connected" state boolean field continuously updated with potentially out of order data, you should rather keep a narrative of the connection state.

For example:

|----------------------|------------------|------------------|
|      Timestamp       |   IoT DeviceID   |  Connected       |
|----------------------|------------------|------------------|
| 2020-05-01T10:05:00Z |       1234       |  True            |
|----------------------|------------------|------------------|
| 2020-05-01T10:00:00Z |       1234       |  False           |
|----------------------|------------------|------------------|
| 2020-05-01T08:45:00Z |       1234       |  True            |
|----------------------|------------------|------------------|

To get the current connected state you just query by the device id, ordering by timestamp.

This way it doesn't matter about the order of the connect / disconnect events. As long as you trust the timestamp the connection status will be accurate at the time of the query.

The query might be something like:

select timestamp, device_id, connected from connection_status order by timestamp desc limit 1;

The benefit of this is that you also have diagnostic data on connections for troubleshooting purposes. This can be very useful!

Of course you would need to purge the data eventually, but you can do this by deleting any data older than 1 month for example.

There is no such problem, it is asynchronous, it is not concurrent, therefore there are no race conditions. The request that enter first is going to be processed first, two states cannot enter at the same time.

Related