This repository was archived by the owner on Feb 1, 2022. It is now read-only.
This repository was archived by the owner on Feb 1, 2022. It is now read-only.
Use reliable queues by Redis to process incoming orders #4
Open
Description
By using RPOPLPUSH
we can guarantee that the order will be processed exactly once:
- The service subscribe (
PSUBSCRIBE
) onCONSUMER
queue keyspace notifications
PSUBSCRIBE "__keyspace@0__:CONSUMER"
- Waits until
LPUSH
keyevent is fired
1) "pmessage"
2) "__keyspace@0__:CONSUMER"
3) "__keyspace@0__:CONSUMER"
4) "lpush"
- Once it's triggered, the service pops the new order from the
CONSUMER
queue and atomically pushes to theCONSUMER_PROCESSING
queue (RPOPLPUSH
)
RPOPLPUSH "CONSUMER" "CONSUMER_PROCESSING"
- Later, the state of the popped item is saved in the datastore with status
processing
- Upon successful persistence of the order the consumer removes (
LREM
) the element from theCONSUMER_PROCESSING
queue
LREM "CONSUMER_PROCESSING" 0 "order_id: 200, market: EUR_USD"
Edge cases:
- if the matching engine crashed and failed to save the status after popping the order from the producer queue, on next recovery it loads all orders with
processing
status from the datastore and removes already saved orders from the consumer queue, after cleanup of consumer queue it proceeds operation as normal