Scaling for writes#
Writes contention#
The Formance Ledger uses a multi-ledger, single-writer, sequential writes architecture to create an auditable trail of transactions that is straightforward to reason about. The latest stable version of the ledger is optimized for 1K writes per second on an underlying commodity storage instance.
As a result, write-heavy applications above this threshold are advised to leverage the Formance Ledger's segmentation capabilities to scale horizontally. This will allow you to create multiple ledgers and to write to them in parallel.
PostgreSQL Row Locks#
When posting transactions, you may experience decreased throughput if all transactions use the same source account (e.g., the default world account). Even if the account allows overdraft (no balance enforcement), using the same source account repeatedly creates a bottleneck at the PostgreSQL level.
Every transaction involves writing changes to the accounts_volumes table. PostgreSQL applies row-level locks when updating balances, causing concurrent transactions targeting the same account to queue.
Locks are applied per (account, asset) pair. High concurrency on a single source account will cause transactions to serialize, significantly reducing throughput.
Solutions to Increase Throughput#
Option 1: Spread Load Across Multiple Source Accounts#
Distribute writes across multiple accounts instead of just one:
- Use dynamic account identifiers like
@world:<random_id>where the suffix is randomly generated - Or use dedicated overdraft source accounts meaningful to your use-case, such as
@provider:<id>:payment:<id> - Set a reasonable pool size (e.g., 20) to balance between performance and account count
This reduces contention on a single row and leverages PostgreSQL's ability to parallelize updates across multiple rows.
Option 2: Disable or Async Log Hashing#
Log hashing uses a PostgreSQL advisory lock on the entire ledger, which can limit write throughput.
To disable or make it asynchronous, create a ledger with the appropriate feature configuration:
Benefits: No advisory lock is taken, resulting in significantly higher write throughput.
Tradeoff: You lose the ability to cryptographically prove that the logs have not been altered or injected manually.
Performance Summary#
| Concern | Description |
|---|---|
| PostgreSQL locking bottleneck | Happens on balance updates; causes queuing if same row (source account) is updated concurrently |
| Mitigation | Use many source accounts (@world:<random_id>) |
| Log Hashing | Ensures log immutability, but adds overhead. Can be safely disabled if not needed |
| Consistency | Guaranteed by PostgreSQL transactional semantics (COMMIT/ROLLBACK) |
| Assets as parallelization tool | Locks are per (account, asset) → spreading across assets avoids contention |
Transaction commit cost breakdown#
As outlined in the performance model, the cost of a write is O(N) + W. Consider the following example:
This transaction increases the balance of @payments:1234 by $99.00, sourcing the funds from three user accounts before defaulting to @world as the source of last resort. The ledger performs these operations:
- Read the balance of
@users:1234:main - If the balance of
@users:1234:mainwas less than $99.00, read the balance of@users:1234:vouchers - If the balance of the previous account was less than $99.00, read the balance of
@users:1234:creditcard - Model the transaction
- Write the new balance of
@users:1234:main
The exact cost of this transaction depends on the number of accounts sourced from: at least O(1) + W and at most O(3) + W. This is because the transaction will need to read the balance of at least one account, and at most three accounts, and then commit the computed transaction to the ledger.
When optimizing for write throughput, it is important to keep in mind this cost breakdown and to design your transactions accordingly.
Scaling for reads#
Chart of accounts#
While usually not a concern for the scaling of writes, designing a proper chart of accounts is key to the performance of your reads. You'll especially want to make sure that the addresses of your accounts are using properly separated segments, as this will allow you to use wildcards to query your accounts.
An example of an inefficient account address would be users:1234_main.
Implemented instead as users:1234:main, the latter will enable the ledger to efficiently query all accounts of a given user by using the users:1234:* wildcard or all accounts of all users of a given type by using the users:*:main wildcard.
Once you've designed your chart of accounts, you can enforce it using a Ledger Schema. Schemas ensure transactions only use valid account structures, support pattern validation for dynamic segments, and record which schema version was used for each transaction for auditability.
Ledgers sizing#
As a consequence of their append-only nature, ledgers will only grow in size over time indefinitely. Optimal mitigation of this nature is one of the Formance Ledger's design goals. As a result of these optimization efforts, not all queries are negatively impacted by the size of the ledger, as explained in the performance model. It is however to be noted that some queries will be impacted by the size of the ledger, relative to the underlying storage database capacity.
In order to equip you with primitives to mitigate this, the Formance Ledger comes with the ability to segment your ledgers, allowing you to create multiple ledgers and to query them in parallel.
Segmentation strategies#
Consider an example financial application where users deposit funds into their account and subsequently use them for international transfers. The application is expected to grow and must handle a large number of users. A segmentation strategy lets you scale the ledgering system horizontally to millions of users without degrading query performance.
A first approach segments ledgers by user, grouping a fixed number of users into a single ledger.
Another approach segments ledgers by time, creating for example a ledger for each year, and then for each month as the number of transactions committed per day grows.
The proper segmentation strategy will eventually be a decision that you will have to make based on the nature of your application and the expected growth.