Process Multiple Events based on order of Timestamp

Viewed 38

I want to process multiple events in order of their timestamps coming into the system via multiple source systems like MQ, S3 ,KAFKA . What approach should be taken to solve the problem?

1 Answers

As soon as an event comes in, the program can't know if another source will send events that should be processed before this one but have not arrived yet. Therefore, you need some waiting period, e.g. 5 minutes, in which events won't be processed so that late events have a chance to cut in front.

There is a trade-off here, making the waiting window larger will give late events a higher chance to be processed in the right order, but will also delay event processing.

For implementation, one way is to use a priority-queue that sorts by min-timestamp. All event sources write to this queue and events are consumed only from the top and only if they are at least x seconds old.

One possible optimisation for the processing lag: As long as all data sources provide at least one event that is ready for consumption, you can safely process events until one source is empty again. This only works if sources provide their own events in-order. You could implement this by having a counter for each data source of how many events exist in the priority-queue.

Another aspect is what happens to the priority-queue when a node crashes. Using acknowledgements should help here, so that on crash the queue can be rebuilt from unacknowledged events.

Related