четверг, 29 марта 2012 г.

ordered high throughput message monitor

Consider we have some high-throughput message listener, say processing more than 1000 messages per sec and there is a task to add some checks on each message like latency or correct ordering checks.

The task is quite common in remote messaging systems with failover functionality: e.g. you can miss a few messages when your messaging system is failovered or get stale ones due to some network issues or bugs in your messaging system itself.

Latency checks are OK - just compare operation between 2 timestamps eventually.

The problem is with ordering checks as surely we'll have some race-conditions here but using locks/synchronization will most likely kill performance and scalability.

So we need some kind of non-blocking algorithm - sure CAS atomics will help us.

Let's specify the requirements, we need:
-track for missed messages (when get forward ones)
-ignore the stale messages (when get old)
No more words, let's just code it: