Stock Exchange 00:00:01 Two traders disagree about who was first Two traders see nearly identical timestamps on their own screens. An illustrative exchange needs one reproducible order for a symbol's commands. In this design, a sequencer and durable journal establish that order before matching. Buy 100 TOYS at 150.00, with client order ID c-7781. A limit buy for a hundred shares at a hundred fifty reserves fifteen thousand units of notional capacity under this example's risk rule. It becomes 1001 and gets a recorded time. Two copies confirm, so the accept report leaves. Best ask is 150.00 with 500 shares, resting. The buy price reaches it, so 100 trade at 150.00 and 400 stay. A sequence number orders accepted input for a particular engine. It is different from a customer's wall clock. State the acknowledgement and durability policy before promising what can be recovered after a crash. 00:01:01 A hundred shares walk two price levels A market buy requests a hundred shares. The best ask offers only fifty at one hundred forty-nine point nine five. Price priority determines where matching begins. Within a price level, this example uses arrival-time priority. A market buy of 100. It takes any price; speed over price control. 50 of 100 trade at 149.95. Order 997 is used up. Orders 995, 998 and 999 wait. 995 is first in line. The earliest resting order at the second price gives up fifty shares and retains thirty at the front of that queue. 50 times 149.95 plus 50 times 150.00 equals 14,997.50. Best ask is now 150.00 with 30 plus 70 plus 50 equals 150 shares. The remaining fifty execute at one hundred fifty, making total notional fourteen thousand nine hundred ninety-seven point five zero. A market order can consume multiple prices; an individual fill and a whole-order average price are different quantities. 00:02:16 A resting bid needs two ways to find it A new limit buy does not cross the best ask. The engine must retain its price priority and later find it quickly by identifier if the trader cancels. One data structure need not answer both questions equally well. Bids at 149.95 and 149.90. The best bid is 149.95. The lowest ask is 150.05, so it cannot trade. It will rest. Find the place for 150.00 and make a level. Update the identifier lookup and the best-bid reference consistently when the price level changes. The map finds it. 804 is now first at 149.95. The price structure finds the level, its ordered queue preserves arrival order, and the identifier index locates a specific resting order. A cancel racing a completed execution must report the actual outcome. 00:03:11 The same market changes between two commands Two comparable buy orders approach from different distances. Sequence them one thousand one and one thousand two. The first command can consume liquidity that the second command was hoping to find. A is about 1 ms away and B about 4 ms. The earlier accepted command receives the lower sequence in this design. The traders' local clocks do not override it. The engine takes 1001, then 1002, even if both wait at its door. 50 at 149.95: 7,497.50. 50 at 150.00: 7,500.00. The difference is 2.50. The two fifty-share fills differ by two and a half in total notional in this book. Deterministic replay must apply the same ordered commands to the same initial state to reproduce both results. 00:04:07 Restart from a journal with customers still waiting Now stop the active matcher immediately after an accepted command. A standby has been applying the ordered journal without publishing duplicate executions. Can a retried order be recognized after leadership changes? A numbers 1001 and copies it to B and C. Heartbeat every 100 ms. B applies 1001 to its own book and publishes nothing. Open orders were in A's memory. They are also in B's. B asks C for term 8 and wins: two of three. C agrees because B's journal is not behind. Gateways direct traffic to the elected leader and retry uncertain commands with their original identities. Fence the old producer, establish a qualified replacement, replay committed input and reuse the original client identifiers. A heartbeat gap can suggest failure, but it does not by itself authorize two independent books to accept orders.