One picture is worth more than a thousand words. This is what happens when you buy a product using Paypal/bank card under the hood.1/8
To understand this, we need to digest two concepts: ๐๐ฅ๐๐๐ซ๐ข๐ง๐ & ๐ฌ๐๐ญ๐ญ๐ฅ๐๐ฆ๐๐ง๐ญ. Clearing is a process that calculates who should pay whom with how much money; while settlement is a process where real money moves between reserves in the settlement bank. 2/8
Letโs say Bob wants to buy an SDI book from Claireโs shop on Amazon.
- Pay-in flow (Bob pays Amazon money): 1.1 Bob buys a book on Amazon using Paypal. 1.2 Amazon issues a money transfer request to Paypal.3/8
1.3 Since the payment token of Bobโs debit card is stored in Paypal, Paypal can transfer money, on Bobโs behalf, to Amazonโs bank account in Bank A.4/8
1.4 Both Bank A and Bank B send transaction statements to the clearing institution. It reduces the transactions that need to be settled. 5/8
1.5 & 1.6 The clearing institution sends clearing and settlement information to the settlement bank. Both Bank A and Bank B have pre-deposited funds in the settlement bank as money reserves, so actual money movement happens between two reserve accounts in the settlement bank6/8
- Pay-out flow (Amazon pays the money to the seller: Claire): 2.1 Amazon informs the seller (Claire) that she will get paid soon 2.2 Amazon issues a money transfer request from its own bank to the seller bank (bank C). Both banks record the transactions, but no money is moved7/8
2.3 Both Bank A and Bank C send transaction statements to the clearing institution. 2.4 & 2.5 The clearing institution sends clearing and settlement information to the settlement bank. Money is transferred from Bank Aโs reserve to Bank Cโs reserve. 8/8
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh
In a payment system, itโs very important to separate ๐ข๐ง๐๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐ฅ๐จ๐ฐ ๐๐ง๐ ๐๐ฎ๐ง๐ ๐๐ฅ๐จ๐ฐ.1/6
In the diagram below, we have three layers:
- Transaction layer: where the online purchases happen
- Payment and clearing layer: where the payment instructions and transaction netting happen
- Settlement layer: where the actual money movement happen 2/6
The first two layers are called information flow, and the settlement layer is called fund flow. 3/6
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
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