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
๐๐๐๐ฆ๐ฉ๐จ๐ญ๐๐ง๐๐ฒ
From an API standpoint, idempotency means clients can make the same call repeatedly and produce the same result. 4/6
For communication between clients (web and mobile applications) and servers, an idempotency key is usually a unique value that is generated by clients and expires after a certain period of time. 5/6
A UUID is commonly used as an idempotency key and it is recommended by many tech companies such as Stripe and PayPal. To perform an idempotent payment request, an idempotency key is added to the HTTP header: <idempotency-key: key_value>. 6/6
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh
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
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