Day 26 Exercises
Payment System Design
Apply idempotency, distributed ledger design, and webhook delivery to financial system architecture.
Idempotency Key Scenarios
A merchant's payment server calls Stripe's API, but the network times out at 28 seconds (Stripe's server DID process the charge before the timeout). The merchant's retry logic fires after 5 seconds.
Tasks
- Without idempotency keys: what happens on the retry?
- With idempotency keys: trace what happens when the retry arrives at Stripe.
- What should the TTL be for idempotency keys, and why?
- Design the idempotency key format a client should use (what information to include).
Double-Entry Ledger Design
Alice pays Bob $100. Design the ledger rows that represent this transaction and the invariant that prevents money creation or destruction.
Tasks
- Write the exact SQL rows that represent this payment (table schema + INSERT statements).
- What is the invariant and how would you enforce it in a query?
- How do you handle a refund? Write the reversal ledger entries.
- What happens to the ledger if the server crashes between the two INSERT statements?
Webhook Reliability Design
Stripe must deliver a payment.succeeded webhook to 10,000 merchants. Some merchants have slow or unreliable servers. Design the webhook delivery system.
Tasks
- Why does at-least-once delivery require merchant handlers to be idempotent?
- Design the retry schedule with exponential backoff (give specific retry times).
- How do you prevent a single slow merchant from blocking webhook delivery for others?
- What should happen after 72 hours of failed delivery attempts?
High-Availability Payment Processing
Stripe processes $1T/year across 42 countries. A database failure in US-East would impact 40% of transactions. Design for 99.999% availability.
Tasks
- Design the multi-region active-active architecture for the payments database.
- How do you handle distributed transactions across regions (e.g., a cross-border payment)?
- Design the circuit breaker for card network calls (Visa/Mastercard API failures).
- What is Stripe's "payment intent" model and how does it improve retry safety?