How to Model Money Movement Correctly in the Payment Ledger
By Formance Staff
Ledger
Payments
Embedded Finance
Loading article content
Loading table of contents
Loading author
Try Formance Ledger
Clone Formance Ledger on GitHub, run it locally, and post the settlement from section 3.1 twice with the same idempotency key. The second post should be rejected, and no balance should change.
A payment ledger models money movement correctly when it can attribute every unit of client money held in an omnibus account (one bank account holding funds for many underlying clients) to a specific client at any past instant. Auditors test payment and e-money firms on one question: whose money is in the omnibus account, and as of when. Many home-built ledgers cannot produce the answer.
Your daily reconciliation shows per-client balances totaling $20,000 less than the omnibus balance on the bank statement. Three engineers spend two days tracing the shortfall through application code, but they can't detect who the money belongs to, because there is no history.
Unreconciled shortfalls become expensive when a firm fails. UK payment firms that became insolvent between Q1 2018 and Q2 2023 had average shortfalls of 65% of client funds. The FCA measures a shortfall as the mismatch between client funds owed and the funds safeguarded.
Each shortfall started as a mismatch between what the ledger said clients were owed and what the bank held. Diagnosing drift shows what to fix for financial teams.
Step 1: Diagnose where the payment ledger drifts from the bank statement
Diagnose drift by comparing yesterday's bank closing balance against the sum of your per-client balances at the same instant, then classify the difference as one of two symptoms.
Pull yesterday's closing balance on the omnibus account from the bank, then sum your per-client balances for the same instant. If the two numbers differ, the drift points to one or both of two symptoms.
1Retries create duplicate postings. A timeout on the payment service provider (PSP) call can trigger a retry from the job runner, and the same settlement then lands twice in transactions with two primary keys and no constraint to stop the duplicate.
2Mutable balance columns hide history. A mutable balance column reports the current total, not the total at 23:59, and the pending settlements that explain the difference live in application state rather than in postings.
Formance Ledger, an open-source, programmable core ledger that unifies fiat and digital assets with regulatory-grade traceability, answers the drift question with a direct query over postings. The postings query is reliable only when the storage invariants in Step 2 hold.
Step 2: Enforce four storage invariants for payment ledger integrity
Four storage invariants must hold on every write: every transaction sums to zero per asset, postings are immutable, every posting carries a deterministic idempotency key backed by a unique constraint, and every posting records bi-temporal timestamps (recorded time and effective time).
If any of the four invariants break, the modeling decisions in Step 3 can produce duplicate postings, lost attribution, or history that cannot be replayed.
2.1 Reject any transaction whose postings do not sum to zero per asset
A transaction is valid only when its postings sum to zero per asset, and the storage layer rejects any write that does not.
The zero-sum rule applies double-entry accounting to multi-leg transactions: a settlement split across four accounts is one write with four postings, committed atomically.
If the fee leg fails validation, the client legs do not commit either. A trigger or constraint on the postings table enforces the sum on every write, whichever service sends it.
2.2 Keep postings immutable and record corrections as new postings
Keep postings immutable so nothing updates or deletes a committed posting, and record a correction as a new posting that reverses the first and then posts the right amount.
A payment firm credits a customer account with the wrong amount, then overwrites the balance when someone catches the error. The current balance looks right, but nobody can show what the account held during the hours it was wrong.
For the schema, the erroneous posting and its reversal should both stay visible. An auditor asking about that window needs to see the account at the wrong amount.
2.3 Enforce idempotency keys at the posting level with a unique constraint
Enforce posting-level idempotency by attaching a deterministic key to every write and adding a unique constraint on that key in the postings store, so the second attempt is rejected before it touches a balance.
An application programming interface (API)-level idempotency key protects the Hypertext Transfer Protocol (HTTP) boundary by returning the same response for the same request. A job runner that builds a fresh request, or two services reacting to the same settlement event, bypass the API-level key. A row-level constraint catches both.
Derive the key deterministically from the business event (settlement file id plus line number, for example) and do not generate a random universally unique identifier (UUID) per attempt. A random key gives every retry a new identity and defeats the constraint.
2.4 Record bi-temporal timestamps on every posting for auditable history
Record two timestamps on every posting (recorded time, when the ledger wrote the posting, and effective time, when the movement took effect) so both time axes are queryable.
A settlement file dated Tuesday that arrives Thursday has an effective time of Tuesday and a recorded time of Thursday. Tuesday's close changes when the file lands, and the ledger should show both the balance reported on Tuesday night and the balance known now.
A backdated correction follows the same pattern: the correction's effective time points at the day being corrected, while its recorded time is today.
Corrections change the effective history, and the recorded history is append-only. A bi-temporality model built on recorded time and effective time answers the auditor's question from the ledger's state at the time. A single created_at column cannot.
Formance Ledger enforces these four invariants in the storage layer, and every caller must follow them.
Step 3: Model the omnibus account and every money movement across it
Model the omnibus account as a named bank boundary account (a ledger account that mirrors the real bank account) plus a multi-segment account namespace of per-client, suspense, and platform-fee sub-accounts whose positive balances, summed, equal the omnibus bank balance at each reconciliation point.
Account paths are separated by colons, so you can sum balances by prefix, such as @customers:*, without a separate table per client.
The account model has four parts:
1An omnibus bank boundary account (@platform:banks:omnibus:main).
2Per-client available and pending sub-accounts (@customers:<client_id>:available, @customers:<client_id>:pending). Per-client sub-accounts under one bank account are the subledger pattern.
3An unallocated account (@platform:suspense:unallocated) for money received but not yet attributed.
4A platform-owned fee account (@platform:revenue:fees) that sits outside the client namespace.
Pending and settled balances live in different accounts, and movement between them is a posting.
•A card capture inside its chargeback window lands in @customers:<id>:pending.
•On confirmation, a posting moves the amount to @customers:<id>:available.
•On reversal, a posting moves the amount back out to the provider account.
Each step is its own posting, so the ledger keeps the full transfer history.
A status flag on the transaction row cannot express pending-to-settled movement, because a flag change moves no value between accounts.
Client sub-accounts plus the unallocated account plus platform fees awaiting sweep should equal the omnibus bank balance represented by the bank boundary. The same model holds for fiat and digital assets, with each asset tracked under its own asset code.
Three money-movement patterns cover the omnibus account: inbound settlement allocation, outbound payout and foreign exchange (FX) conversion. Each flow is one atomic transaction whose postings sum to zero per asset.
3.1 Allocate an inbound PSP settlement to client sub-accounts
Allocate an inbound PSP settlement in one atomic transaction that debits the omnibus bank boundary and credits each client's available and pending sub-accounts plus platform fees.
Numscript is Formance's purpose-built language for describing atomic financial transactions. Every send statement in the file below commits together as one transaction when the file executes. The transaction uses USD/2 minor units.
The provider's payout enters through the named omnibus bank boundary, and the transaction distributes the payout. The cash-in record stays reconcilable.
In the $1,250,000.00 transaction, Acme Logistics receives $662,500.00 available and $150,000.00 held pending the chargeback window. Northwind Retail receives $400,000.00 available, and $37,500.00 goes to platform fee revenue.
The @platform:suspense:unallocated account receives no posting, so the four allocations sum to $1,250,000.00.
A later transaction, posted after settlement confirmation, moves the $150,000.00 from @customers:acmeLogistics:pending to @customers:acmeLogistics:available under its own deterministic idempotency key.
3.2 Pay a client out of the omnibus account with reversible postings
Pay a client out of the omnibus account in one transaction with three legs: debit the client's available sub-account, credit a payout-pending bank-boundary sub-account for the amount leaving, and credit platform fee revenue for the fee.
Acme Logistics instructs a $100,000.00 withdrawal to an external bank account, and the platform captures a $250.00 payout fee at the same time.
The payout stays in the payout-pending sub-account until the PSP confirms execution, so a single compensating posting can reverse a failed wire, and history stays intact.
// CLIENT_PAYOUT_INITIATED// Event: client-instructed withdrawal from available balance to external beneficiarysend [USD/2 10025000] ( source = @customers:acmeLogistics:available destination = { max [USD/2 10000000] to @platform:banks:omnibus:payouts_pending remaining to @platform:revenue:fees })set_tx_meta("event_type", "client_payout_initiated")set_tx_meta("payout_id", "pyt_8821")
On PSP confirmation, a second transaction moves $100,000.00 from @platform:banks:omnibus:payouts_pending to @platform:banks:omnibus:main as a debit against the bank boundary.
On failure, the same amount returns to @customers:acmeLogistics:available under a new key derived from the PSP failure event.
3.3 Convert a client balance across two asset codes with rate metadata
Convert a client balance across two asset codes in one atomic transaction, recording the rate and spread on the transaction using a metadata primitive so reconciliation can replay the conversion later.
Metadata attaches arbitrary context (quote rate, spread basis points, and quote ID) to the transaction without polluting the posting shape, and it is queryable alongside balances.
FX flows move value across two asset codes at once. Northwind Retail converts $50,000.00 to EUR at a quoted rate of 0.9200, with a 20-basis-point spread captured as platform FX revenue.
The USD leg debits the client's USD available account into an FX settlement account. The EUR leg credits EUR 46,000.00 to the client's EUR available account from an FX liquidity account at the quoted rate. The liquidity account is a platform-owned account that the example allows to go into an unbounded overdraft, so the transaction runs without pre-funding. The rate already includes the spread. The spread leg moves $100.00 from the settlement account to platform FX revenue.
The FX settlement and liquidity accounts are platform-owned, so client sub-accounts remain fully attributable in both currencies.
3.4 Query per-client attribution as a sum over namespace prefixes
Query per-client attribution as a sum over account prefixes in the namespace. Sum @customers:acmeLogistics:* for Acme's total, and sum @customers:* plus @platform:suspense:unallocated for the firm-wide client-money total.
Step 4: Reconcile the payment ledger against bank and PSP balances at fixed cut-offs
Reconcile the payment ledger by summing the positive attribution accounts for each asset at each reconciliation point and comparing the per-asset total to the balance the bank or PSP reports for the same instant.
Ledger reconciliation tests whether the controls from Steps 2 and 3 held in production. Formance Reconciliation matches internal records to external statements with policy-based matching rules that pair internal records against external statements, historicized balances for look-back, and bi-temporal cut-off logic sourced from invariant 2.4.
The per-asset total must equal the bank or PSP balance for the same instant. It must also equal the absolute value of the bank boundary account balance.
A settlement file dated Tuesday that arrives Thursday would otherwise show as drift on Tuesday night that disappears on Thursday. With effective time, the movement lands in Tuesday's cut-off, and the Tuesday balance matches the bank.
Can your payment ledger answer the omnibus attribution question?
If the query requires joining a balance column to a retry de-duplication table and a pending-settlements cache, at least one of the four invariants is enforced in application code, not in storage.
A ledger passes the omnibus attribution test when one query over immutable postings returns each client's balance at any past instant, and the per-asset total matches the bank or PSP balance for that same instant.
The ledger requires zero-sum transactions, immutable postings, posting-level idempotency, and bi-temporal timestamps stored in the ledger. It also requires an account model in which client, suspense, and fee balances sum to the omnibus boundary.
If any one of these is missing, you are back to the two-day trace from the opening example.