KatInterview

Building a Future-Dated Payment Scheduler with Safe Cancellation

System DesignModel answer
HardModel answerSystem Designsystem-designschedulingidempotency

Last updated

Design a service that stores payments for later execution, lets users check or cancel them while pending, and guarantees no duplicate charges despite retries, crashes and time-zone quirks.

You are asked to design a backend service for payments that run at a later date and time. A user picks a payee, an amount and a future moment; the platform must charge at that moment, exactly once, and let the user revoke the request as long as it has not started processing.

What to cover

  1. Requirements and scale. Functional and non-functional goals, plus a back-of-the-envelope estimate of volume, peak load and storage. Assume tens of millions of users and several million scheduled payments per day, with strong clustering around moments such as midnight or the first of the month.
  2. API. REST or gRPC operations to create a scheduled payment, read its status (and list a user's payments), and cancel it. Describe request fields, response codes and any headers needed for safe retries.
  3. Data model and indexes. Tables, state machine, and the indexes that make "what is due now?" and "show me my payments" fast.
  4. Execution pipeline. How due payments are found, handed to workers (scheduler, queue, worker fleet) and charged through an external payment gateway.
  5. Duplicate protection. How you prevent double charges across client retries, repeated queue deliveries and gateway timeouts.
  6. Cancel vs. execute race. What happens when a user cancels at nearly the same moment the payment starts.
  7. Time semantics. Time zones, daylight-saving gaps and overlaps, timezone database updates, clock drift and ordering.
  8. Failures. Retries, backoff, dead-letter handling, and what to do when the gateway outcome is unknown.
  9. Consistency with the gateway. Transactional guarantees and reconciliation.
  10. Security and compliance. Card data handling, encryption, PII, audit.
  11. Scale-out and resilience. Partitioning, high availability, disaster recovery.
  12. Operations. Monitoring, alerting, auditing, and the SLAs you would commit to.

State your assumptions and justify the main trade-offs.

Hints

Hint 1

Treat the database as the single source of truth and the queue as a mere carrier of work. Think about what happens to a month-away job if it lives only in a delay queue.

Hint 2

Model the payment as a state machine where each transition is a compare-and-set on status and version. Both the worker start and the cancel then compete for the same row, and exactly one wins.

Hint 3

Use a deterministic idempotency key (for example the payment id) for every gateway call, reused across retries, and design an explicit 'unknown outcome' state resolved by querying or reconciling with the gateway.

Hint 4

Store the user's local wall time plus IANA zone and derive the UTC instant; decide in advance how to handle nonexistent and ambiguous local times.