KatInterview

Building a Duplicate-Safe Money Movement Service with Settlement Matching

System DesignModel answer
MediumModel answerSystem Designidempotencypaymentsstate-machine

Last updated

Design a service that moves customer funds through external bank rails without double-charging, tracks each transfer's lifecycle, and repairs mismatches using daily bank settlement files.

Problem

You are designing the backend that lets customers of a brokerage app add money to their account (deposit) or take money out (withdrawal) over bank transfer (ACH) and debit rails. The service talks to outside payment processors and partner banks.

The environment is unreliable. Connections drop mid-request, mobile clients resend requests, processors time out or answer late, and webhooks may arrive twice or in the wrong order. Despite this, your design must guarantee that:

  • No customer is charged or credited twice.
  • Every unit of money is accounted for.
  • Every in-flight transfer is eventually resolved to a final outcome.

Functional requirements

  • Initiation: Accept deposit and withdrawal requests that carry a client-supplied idempotency key.
  • Provider integration: Submit transfers to external rails and capture outcomes from both synchronous responses and asynchronous webhooks.
  • Lifecycle tracking: Model each transfer with an explicit set of states, for example CREATED, PROCESSING, SUCCEEDED, FAILED, and REQUIRES_RECONCILIATION.
  • Reconciliation: Ingest daily settlement or clearing files from partner banks, find differences against internal records, and correct them.

Non-functional requirements

  • Strict idempotency: Repeated calls, deliberate or accidental, never cause a second money movement.
  • Durability and auditability: Every balance change, state change, and provider interaction is recorded in an append-only fashion.
  • Availability and fault tolerance: Survive provider outages, timeouts, and out-of-order or duplicate webhooks.

Scale

  • About 10 million transfers per day: roughly 120 requests/second on average, 600/second at peak.
  • Up to 100,000 transfers pending settlement or asynchronous resolution at any moment.

What to deliver

  1. API design for initiating transfers and receiving provider webhooks.
  2. A state machine and data model covering transfers, idempotency keys, and transitions.
  3. An end-to-end architecture that handles timeouts, retries, and cross-system consistency.
  4. A reconciliation approach for mismatches discovered when settlement files are loaded.

Hints

Hint 1

Throughput here is modest. Ask yourself what the genuinely hard part is, and state the core invariant you want the system to uphold, such as one external money movement per payment record and one balanced set of ledger entries.

Hint 2

Think about two separate idempotency boundaries: client to your API, and your service to the provider. Also consider what a timeout actually tells you about whether money moved, and whether a timed-out payment should ever be marked failed.

Hint 3

To avoid inconsistency between your database and the provider call, consider writing the intent durably in the same transaction as the payment and dispatching it from there. For webhooks and state changes, use deduplication tables and conditional updates so late, duplicate, or reordered events become harmless no-ops.