Video in progress

How do you make sure money amounts agree between the payment, order and ledger services, and how do you detect differences?

Every money movement is stored in the ledger as a double-entry record, immutable and with an idempotency key. For agreement between services I rely not only on real-time guarantees but on regular reconciliation too: the reports of the services and the external provider (bank, PSP) are compared, and differences go to an alert and a review queue.

📖 Answer

In a distributed system you can't promise "there will never be a difference". The right question is: how quickly is a difference detected, and who fixes it?

The foundation is the ledger:

entry_id   tx_id      account            debit    credit
1          pay-812    customer:42        150000
2          pay-812    merchant:7                  147000
3          pay-812    fees:platform                 3000

The rules:

  • in every transaction the sum of debits = the sum of credits, checked at write time;
  • records are never changed or deleted: if there is a mistake, you add a reversing entry;
  • tx_id is unique, so a repeated event or retry doesn't write twice;
  • amounts are BigDecimal or a long in the smallest unit (tiyin), never double.

Levels of reconciliation:

  1. Internal: for every order in the PAID state, does the ledger have a matching tx_id and is the amount the same? A daily job compares the data of both services (or their copy in the DWH).
  2. External: against the PSP's or bank's daily settlement file. The bank shows 1000 payments and we have 998: two are stuck in PENDING because a webhook was lost.
  3. Constant invariants: in double-entry the sum over all accounts is zero, a simple SQL check that runs every hour.

When a difference is found: automatic correction only in clear cases (for example, the PSP says "successful" and we have PENDING: the state is queried and completed). The rest go to a queue the finance team reviews, with the amount and age. The count and total amount of "open differences" is a metric that finance and engineering watch together.

In short, rather than trying to build an "everything at once" guarantee in a money flow, it is more reliable to make every step idempotent, auditable and reconcilable. Banks have worked this way for decades.