# How Ledger Stores and Replicates Data

Source: https://docs.formance.com/v4.0/modules/ledger/get-started/architecture

Stack: v4.0

Ledger 3.0 stores its data in an embedded [Pebble](https://github.com/cockroachdb/pebble) store on each node and replicates every write through Raft consensus. There is no PostgreSQL server in the deployment.

This page explains why that changed, because the reasoning determines how you design for throughput and what you are responsible for operationally.

## What PostgreSQL was actually doing

Ledger 2.x kept its state in PostgreSQL, but the database was never only a place to put rows. It was the concurrency engine. Two database mechanisms enforced correctness:

| Mechanism                                | What it protected                                                    | What it cost                                              |
| ---------------------------------------- | -------------------------------------------------------------------- | --------------------------------------------------------- |
| Row-level locks on the balance table     | Two transactions could not update the same account's volumes at once | Concurrent writes to one account queued behind each other |
| A per-ledger advisory lock on log insert | The hash-chained log stayed strictly ordered and gap-free            | Writes within one ledger were serialized                  |

Both are correct. Both are also throughput ceilings, and they are the reason the 2.x guidance reads the way it does: spread writes across many source accounts so they hit different rows, and split across many named ledgers so they take different advisory locks.

The second ceiling had a worse consequence. Because the advisory lock was taken to maintain the hash chain, the documented way to lift it was to stop maintaining the chain — turning log hashing off or making it asynchronous. Integrity became a throughput setting you could trade away.

## Where ordering comes from in 3.0

Ledger 3.0 takes ordering back from the database and does it itself.

1. The leader validates and prepares a proposal, resolving any state it needs first.
2. Raft orders and replicates that proposal across the cluster.
3. A deterministic state machine applies the accepted proposal identically on every node.
4. State lands in the node's local Pebble store as part of that controlled apply path.

A follower that receives a write forwards it to the current leader. All ledgers in a cluster share one Raft group, so every accepted mutation takes a place in a single ordering domain.

## What that changes

| Concern                          | Ledger 2.x                                              | Ledger 3.0                                                                                                                                                                                           |
| -------------------------------- | ------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Write ordering                   | PostgreSQL row and advisory locks                       | Raft consensus over one global ordering domain                                                                                                                                                       |
| Concurrent writes to one account | Queued behind a row lock                                | One current volume pair per account, color, and asset, overwritten (last-write-wins) as the state machine applies entries in Raft order; that ordering serializes updates, so there are no row locks |
| Integrity                        | Hash chaining, which could be turned off for throughput | Audit hash chain over every proposal, always maintained                                                                                                                                              |
| Current balance                  | Rows updated under database concurrency control         | A persisted projection the state machine maintains deterministically                                                                                                                                 |
| Failover and replication         | Owned by the PostgreSQL deployment                      | Leader election, quorum, WAL, and snapshots owned by Ledger                                                                                                                                          |
| Deployment                       | Application plus an external database                   | Self-contained ledger nodes                                                                                                                                                                          |

Two consequences of Raft ordering and the deterministic state machine that invert previous advice that was correct in Ledger 2.x:

- **Hot accounts are no longer a bottleneck.** A central wallet, treasury, or fee collector debited by heavy concurrent traffic no longer queues. Splitting one logical account across many synthetic addresses to spread row-lock pressure is no longer a throughput technique. You can design your chart of accounts according to business requirements!
- **Integrity is not a throughput lever.** The audit chain covers every proposal and is no longer something you turn off to go faster. The hash primitive is a cluster-wide choice: `blake3`, the default, is cryptographic and makes the chain tamper-evident; `xxh3` is faster but non-cryptographic, so it detects corruption only. Choosing `xxh3` trades tamper evidence for speed; it does not switch the chain off.

## What you still own

Removing the database does not remove persistence engineering. It moves it inside the ledger's operational boundary, where it remains your responsibility:

- **Quorum topology.** Three voters is the normal production starting point. See [Manage the Cluster Lifecycle](https://docs.formance.com/v4.0/modules/ledger/operations/cluster-lifecycle).
- **WAL placement.** Raft WAL writes are synchronous and on the critical path of every write. They belong on fast storage, separated from data I/O where the platform supports it.
- **Storage growth.** History is permanent — there is no retention or archival tier. See [Plan Capacity and Tune Performance](https://docs.formance.com/v4.0/modules/ledger/operations/capacity-and-performance).
- **Backup and restore.** Application-level checkpoints and snapshots, not a database backup. See [Back Up Ledger](https://docs.formance.com/v4.0/modules/ledger/operations/backup).

Ledger 3.0 is not a drop-in replacement for a 2.x deployment. If you are moving an existing ledger, start with the [migration overview](https://docs.formance.com/v4.0/modules/ledger/migration/overview), which covers mirroring from a running 2.x instance and cutting over.

## Next

- [Design for Throughput](https://docs.formance.com/v4.0/modules/ledger/modeling/design-for-throughput) — what actually limits write rate in 3.0, and how to model for it.
- [Read Consistency and Checkpoints](https://docs.formance.com/v4.0/modules/ledger/querying/consistency-and-checkpoints) — how read modes interact with replication.
- [Deploy Ledger](https://docs.formance.com/v4.0/modules/ledger/operations/deploy) — cluster topology and node configuration.
