DEV Community

Jannatul Nisa Jeem
Jannatul Nisa Jeem

Posted on

What If Your Sensor Data Arrives Out of Order?

Here's a small problem that can become a surprisingly annoying one.

Imagine your system receives:

10:01 → 72.4°C
10:02 → 72.8°C
10:03 → 73.1°C

Looks fine.

But because of network delays, you might actually receive:

10:01 → 72.4°C
10:03 → 73.1°C
10:02 → 72.8°C

Now what?

If your application assumes messages always arrive in chronological order, you've got a problem.

This is one reason timestamps matter so much in distributed systems.

The time a message was generated isn't necessarily the same as the time your server received it.

I like keeping both when possible:

{
"device_timestamp": "10:02:00",
"received_timestamp": "10:02:03",
"value": 72.8
}

That gives you much more information when debugging.

IoT systems are full of tiny details like this.

The individual problem looks small.

Multiply it by thousands of devices and suddenly it becomes very important.

Top comments (0)