Where should money movements live, and how do I make sure no cent appears or disappears?
Applications like this: wallets, payment processors, billing systems, loyalty points. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.
| Dimension | This application |
|---|---|
| Writes | Steady. Every movement is a write, never an update in place. |
| Reads | Balances, statements, reconciliation jobs. |
| Read to write | Close to 1 to 1 on the ledger. |
| Consistency | Strict. A committed movement must survive any single failure, and the books must always balance. |
| Latency | Tens to hundreds of milliseconds is fine. Correctness matters more. |
| Growth | Forever. Ledgers are kept for years, often by law. |
These incidents are why the choices below matter. Each one is written up with its numbers and sources.
Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.
| Data | Where it goes | Why |
|---|---|---|
| Accounts and ledger entries | PostgreSQL or MySQL, with synchronous replication | Transactions, constraints, and a commit that waits for a standby, so a failover cannot lose an acknowledged payment. On MySQL, semi-sync waits only up to its timeout and then falls back to async, so watch Rpl_semi_sync_source_status, and use Group Replication when the ledger cannot lose an acknowledged payment. See what a commit waits for. |
| Idempotency keys | The same database, in the same transaction | A retried request must find the key that the first attempt wrote, or the money moves twice. |
| Statements and reconciliation | A replica or a column store | Long scans that must not slow down new payments. |
| Raw provider callbacks | Object storage, plus a table that points to them | Large, rarely read, kept for audit. |
How each family of store behaves under traffic: Database families.
CREATE TABLE idempotency_keys ( key text PRIMARY KEY, created_at timestamptz NOT NULL DEFAULT now() ); CREATE TABLE ledger_entries ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, idempotency_key text NOT NULL REFERENCES idempotency_keys, account text NOT NULL, amount_cents bigint NOT NULL ); -- every transfer writes two rows that sum to zero, in one transaction -- a balance is a sum, or a row updated in the same transaction SELECT sum(amount_cents) FROM ledger_entries WHERE account = $1;
The client sends a charge, times out, and sends it again. The idempotency key and the ledger entries go in one transaction, so the retry changes nothing.
BEGIN;
WITH k AS (
INSERT INTO idempotency_keys (key) VALUES ('order-981')
ON CONFLICT (key) DO NOTHING
RETURNING key
)
INSERT INTO ledger_entries (idempotency_key, account, amount_cents)
SELECT k.key, v.account, v.amount
FROM k, (VALUES ('customer:42', -4999), ('merchant:7', 4999)) AS v(account, amount)
RETURNING account, amount_cents;
COMMIT;BEGIN;
WITH k AS (
INSERT INTO idempotency_keys (key) VALUES ('order-981')
ON CONFLICT (key) DO NOTHING
RETURNING key
)
INSERT INTO ledger_entries (idempotency_key, account, amount_cents)
SELECT k.key, v.account, v.amount
FROM k, (VALUES ('customer:42', -4999), ('merchant:7', 4999)) AS v(account, amount)
RETURNING account, amount_cents;
COMMIT;SELECT account, sum(amount_cents) AS balance_cents FROM ledger_entries GROUP BY account ORDER BY account; SELECT sum(amount_cents) AS must_be_zero FROM ledger_entries;
Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.
| What happens | What you see | What I do |
|---|---|---|
| A retry without an idempotency key | Duplicate charges in reconciliation | Make the key mandatory in the API, and unique in the database. |
| Async replication and a failover | Payments acknowledged, then missing on the new primary | Use synchronous replication for this database, and accept the latency. |
| A hot account, such as the platform fee account | Lock waits on one balance row | Spread it over several sub-accounts and sum them, or derive the balance from entries. |
Cost. The synchronous standby is not optional here, so budget for it. Save money on the history instead: partition by month and move old partitions to cheaper storage.
| Stage | Where it runs |
|---|---|
| Where I would start | Managed relational with synchronous standby (RDS Multi-AZ or equivalent) |
| When it grows | The same, plus cross-region copies. Self-managed only with a team that rehearses failover |
For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.
The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.