SDE & MLE Prep

Payment System

Requirements: charge customers via PSPs (Stripe/Adyen), never double-charge, never lose money, full audit trail, reconciliation; correctness over latency.

checkout -> payment svc --(idempotency key)--> payment DB (SQL: payment, state machine)
                 |                                   |
                 v                                   v  outbox -> Kafka
         PSP (card network) <--webhook/callback--  ledger svc (double-entry, append-only)
                                                     |
                                       nightly reconciliation vs PSP settlement files

Deep dives

  • Idempotency everywhere: client idempotency keys; PSP calls carry a unique payment id; retries are safe.
  • State machine: created → authorized → captured → settled (or failed / refunded); transitions are transactional.
  • Double-entry ledger: every movement = debit + credit rows summing to zero; immutable, append-only.
  • Consistency: SQL with transactions; outbox pattern to emit events; sagas for multi-step flows (reserve inventory → charge → ship, with compensations).
  • Reconciliation: compare internal ledger with PSP/bank reports, flag mismatches.
  • Security: PCI-DSS scope minimization (tokenize cards via PSP), encryption, fraud checks.