'What is the proper way to handle time synchronization when generating sequential events?
The program I am working on generates timestamped sequential events with milliseconds precision.
E.G.
event_1 at time 2022-04-27T12:00:00.000000-04:00
event_2 at time 2022-04-27T12:00:00.001000-04:00
event_3 at time 2022-04-27T12:00:00.002000-04:00
etc...
The problem I am facing is that, as the system performs a time synchronization via NTP, it is possible for the system clock to "go back in time".
In rare circumstances, this might create a situation where an event that should logically be generated after another to be timestamped before it.
E.G.
event_1 at time 2022-04-27T12:00:00.000000-04:00
event_2 at time 2022-04-27T12:00:00.002000-04:00
TimeSync at this point
event_3 at time 2022-04-27T12:00:00.001000-04:00
etc...
Later on, when another system consumes these events (most likely to generate a report), if they assert the logical order of the events from their timestamps, it will fail since it'll appear as event_3 happened before event_2, which wouldn't be correct.
I was wondering what would be the correct way to tackle this issue.
Currently, if multiple events happen under a milliseconds of each other (which is possible), we doctor their timestamps to artificially give them that milliseconds difference/precision after their previous one. This has the side effect of protecting us from the above undesired behavior until the system slows down long enough for the real clock to "catch up" to our doctored timestamps.
Another obvious solution would be to give each event a unique sequential number independent from their timestamps. The problem here is that the domain/standard we are implementing doesn't support this sequential id, so most likely, if we need to interface with a vendor, we'll end up with the same issue.
For context, these timestamps are generated using boost library in C++.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
