KatInterview

Design a Pay Calculation API with Retries and Idempotency

System DesignModel answer
MediumModel answerSystem Designapi-designidempotencyfault-tolerance

Last updated

Design a resilient HTTP API for event ingestion and worker earnings computation, featuring idempotency keys, deterministic windowing, batch partial success, and retries.

You are designing an HTTP API for a gig-economy platform to compute worker earnings based on order event logs (such as order assignment, pickup, delivery, and cancellation).

Your design should fully specify the following requirements:

  1. API Endpoints: Design routes for ingesting event streams, submitting pay calculation jobs, and retrieving computed earnings.
  2. Schemas & Data Types: Specify request/response models, exact currency representations, timestamp standards, and rules for evaluating time-range boundaries.
  3. Idempotency Strategy: Detail how write operations and calculation requests avoid race conditions, handle duplicate client submissions, and process partial batch failures safely.
  4. Failure Recovery & Retry Behavior: Explain how clients and servers handle transient and permanent failures, backoff policies, HTTP status mappings, and non-retryable error conditions.
  5. Observability & Auditability: Define the logging, metrics, tracing, and metadata required to verify financial calculations reproducibly.

Assume this service operates in an internal microservices architecture, taking events from order management systems and producing figures consumed by financial reporting and payout backends.

Hints

Hint 1

Financial systems require strict auditability: always represent monetary values as integer minor units combined with explicit currency codes.

Hint 2

Differentiate between event-level idempotency (handled via natural unique event IDs) and operation-level idempotency (handled via header tokens).

Hint 3

Use half-open intervals [start, end) for time windows to avoid double-counting events on exact period boundaries.