Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Use reliable queues by Redis to process incoming orders #4

Open
dsirotkin256 opened this issue Apr 10, 2020 · 0 comments
Open

Use reliable queues by Redis to process incoming orders #4

dsirotkin256 opened this issue Apr 10, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@dsirotkin256
Copy link
Owner

dsirotkin256 commented Apr 10, 2020

By using RPOPLPUSH we can guarantee that the order will be processed exactly once:

  1. The service subscribe (PSUBSCRIBE) on CONSUMER queue keyspace notifications
PSUBSCRIBE "__keyspace@0__:CONSUMER"
  1. Waits until LPUSH keyevent is fired
1) "pmessage"
2) "__keyspace@0__:CONSUMER"
3) "__keyspace@0__:CONSUMER"
4) "lpush"  
  1. Once it's triggered, the service pops the new order from the CONSUMER queue and atomically pushes to the CONSUMER_PROCESSING queue (RPOPLPUSH)
RPOPLPUSH "CONSUMER" "CONSUMER_PROCESSING"
  1. Later, the state of the popped item is saved in the datastore with status processing
  2. Upon successful persistence of the order the consumer removes (LREM) the element from the CONSUMER_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
@dsirotkin256 dsirotkin256 added the enhancement New feature or request label Apr 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant