Here is a simplified design diagram that explains how a stock exchange such as Nasdaq or Newyork exchange works under the hood: #developers#systemdesign
Step 1: A client places an order via the broker’s web or mobile app.
Step 2: The broker sends the order to the exchange.1/6
Step 3: The exchange client gateway performs operations such as validation, rate limiting, authentication, normalization, etc., and sends the order to the order manager.
Step 4-5: The order manager performs risk checks based on rules set by the risk manager.2/6
Step 6: Once risk checks are passed, the order manager checks if there is enough money in the wallet.
Step 7 - 9: The order is sent to the matching engine. The matching engine sends back the execution result if a match is found. 3/6
Step 10 - 14: The execution result is passed all the way back to the client.
Step M1 - M3 (market data flow): Market data, including the candlestick chart and order book, are sent to the data service for consolidation. Brokers query the data service to get the market data. 4/6
Step R1 - R2 (reporting flow): The reporter composes all the necessary reporting fields (e.g. client_id, price, quantity, order_type, filled_quantity, remaining_quantity) and writes data to the database.5/6
Please notice that steps 1 to 14 are on the critical path (the trading flow), while the market data flow and reporting flow are not. They have different latency requirements. If you have any questions or I missed anything, please leave a comment. 6/6
• • •
Missing some Tweet in this thread? You can try to
force a refresh
One of the most serious problems a payment system can have is to 𝐝𝐨𝐮𝐛𝐥𝐞 𝐜𝐡𝐚𝐫𝐠𝐞 𝐚 𝐜𝐮𝐬𝐭𝐨𝐦𝐞𝐫. When we design the payment system, it is important to guarantee that the payment system executes a payment order exactly-once.1/6
At the first glance, exactly-once delivery seems very hard to tackle, but if we divide the problem into two parts, it is much easier to solve. An operation is executed exactly-once if:
1. It is executed at least once. 2. At the same time, it is executed at most once.2/6
We now explain how to implement at least once using retry and at most once using idempotency check.
𝐑𝐞𝐭𝐫𝐲
Occasionally, we need to retry a payment transaction due to network errors or timeout. Retry provides the at-least-once guarantee. 3/6
Here is what happens when you click the Buy button on Amazon or any of your favorite shopping websites.
1. When a user clicks the “Buy” button, a payment event is generated and sent to the payment service.
2. The payment service stores the payment event in the database. 1/6
3. Sometimes a single payment event may contain several payment orders. For example, you may select products from multiple sellers in a single checkout process. The payment service will call the payment executor for each payment order. 2/6
4. The payment executor stores the payment order in the database.
5. The payment executor calls an external PSP to finish the credit card payment. 3/6