use case: have a producer sending lots market data events that trigger some revaluation/processing . The consumer is just interested in the latest event, we want some way for the producer to update events on the queue with the latest data if it has not been consumed yet. each event has a key associated to it (e.g. instrument name, or currency pair e.g. EURUSD)
solution 1:
initially had a custom data structure which was a combination of 1P1C queue, and a custom open addressed hash table. basic idea was
the producer would check hash table if the key exists, if it did exist the value for the associated key was updated (this involves a CAS operation if CAS failed it meant consumer had consumed the entry already ). if the key does not exist it was added to the hashtable and put the key on to the queue. consumer would take keys from the queue and look up the values for associated key from the hash table, once consumed the entry in hashtable was set to null (also a CAS operation if CAS failed it meant the producer updated the value, and consume would have to retry ) .
was not satisfied with the above approach as there is a CAS operation for each poll operation even for single producer single consumer .
solution 2:
solution 3:
simple 1P1C queue, let consumer do a batch take and do the coalescing on the consumer thread before processing the events , this to me seems like the simplest and cleanest approach.
are there any other ways to solve this problem
thanks
isaiah