I have a scenario where I have to match records within a table on a FIFO basis. The table structure looks like below -

id, request_type, qty, crtd_ts
10, buy, 15, 15:00
12, buy, 6, 15:25
15, sell, 5, 15:35
16, sell, 6, 16:00
18, buy, 15, 16:30
19, sell, 20, 16:33
20, sell, 4, 17:45
22, sell, 1, 18:02

Given the sample data above - the idea is to be able to match records by buy and sell request types, starting with the oldest, and jumping through records orderly if necessary. So, in this case -

id(10), will match with id(s) 15, 16 and 19
id(12), will match with id 19
id(18), will match with id(s) 19, 20 and 22

This table will have many records, and I am wondering what my best approach is.

My original plan was to have a db reader and the writer will match accordingly. The challenge (given the FIFO requirement) is that this will be a single-process, single-threaded implementation, and it will be slow.

Will a master-slave implementation work ? Any other recommendations ?

Thanks for any tips!