🎉 Giảm 20% cho đơn hàng đầu tiên!✦🚚 Miễn phí vận chuyển cho đơn trên 500.000₫✦⚡ Flash Sale — Số lượng có hạn!✦🎁 Quà tặng kèm cho đơn từ 1.000.000₫✦🎉 Giảm 20% cho đơn hàng đầu tiên!✦🚚 Miễn phí vận chuyển cho đơn trên 500.000₫✦⚡ Flash Sale — Số lượng có hạn!✦🎁 Quà tặng kèm cho đơn từ 1.000.000₫✦
Designing a Banking Transaction System: Absolute Consistency and Auditability
Quay lại Blog

Designing a Banking Transaction System: Absolute Consistency and Auditability

18 tháng 11, 20268 phút đọcVũ Khôi
English
#English
Vũ Khôi

Vũ Khôi

Tác giả tại Wotech Electronics Blog

Title card "How to design a Banking Transaction System?" with the Go gopher mascot

Designing a Banking Transaction System: Absolute Consistency and Auditability

A bank's core system (Core Banking / Ledger System) is one of the most demanding problems in system design. A post by Duy Anh Bui in the Golang Backend group explains why financial transaction systems require accuracy down to the last cent, and how to design for it — putting data integrity and auditability above every speed metric.

Quick answer: A banking transaction system requires strong consistency (ACID), idempotency, auditability with an immutable ledger, and an RPO of zero. The standard design uses double-entry bookkeeping: every transaction creates at least two entries whose algebraic sum is zero, written to an append-only ledger table, never UPDATEing or DELETEing history. Data contention is handled with pessimistic locking under a fixed lock order, or optimistic locking on a version column. Duplicates are prevented with an Idempotency Key, distributed transactions use the Transactional Outbox combined with the Saga Pattern, and scaling comes from CQRS, sharding by account_id, and splitting hot accounts into sub-accounts.

A ledger is the immutable record of every balance movement of every account, used for reconciliation and auditing. Unlike a balance column that can be overwritten, a ledger is append-only: every movement is a permanent line, and the balance at any point in time can be derived from the history of those lines.

What does a banking transaction system require that a social network does not?

According to the post, unlike social networks or ordinary e-commerce sites — where a system can accept eventual consistency — a financial transaction system requires absolute accuracy down to the last cent, protection against duplicate transactions, and data integrity that cannot be broken even during hardware or network failures.

Functionally, the system must manage accounts (create accounts, check available balance and transaction history), execute internal and interbank transfers (debit the source account, credit the destination account), and record an immutable ledger of all balance movements.

On non-functional requirements, the post names four core ones: strong consistency under ACID (money must never disappear or be created from nothing, with strict atomicity and isolation); idempotency (a transfer request sent repeatedly must execute exactly once); auditability and immutability (never UPDATE or DELETE transaction history directly — every correction is a reversal entry); and high availability with durability (RPO = 0, transaction data must never be lost in any scenario).

Why must the balance not live in a single balance column?

The post points to the most common mistake of inexperienced engineers: storing only a balance column in the accounts table and adding or subtracting directly (UPDATE accounts SET balance = balance - 100). This approach cannot trace why a balance moved and is highly prone to reconciliation errors.

Instead, standard banking systems use double-entry bookkeeping: every financial transaction creates at least two ledger entries whose algebraic sum is zero — one Debit entry (subtracting from the source account) and one Credit entry (adding to the destination account). The three core tables are: accounts (account identity, currency, status ACTIVE/FROZEN, optionally a cached_balance with a version column to optimize reads); transactions (transaction overview: transaction_id, idempotency_key, status, created_at); and ledger_entries (append-only, one line per movement: entry_id, transaction_id, account_id, amount, direction DEBIT/CREDIT, balance_after).

Another rule the post stresses: never use floating-point types (FLOAT, DOUBLE) for money because of binary rounding errors. Always use integers (BIGINT storing the smallest unit, such as cents) or DECIMAL(19, 4).

How do the two concurrency-control approaches differ?

When thousands of transactions hit one account at once — for example, a large e-commerce marketplace account receiving payments continuously — race conditions leading to double spending happen easily. The post compares two approaches:

Pessimistic locking Optimistic locking
Mechanism Hard-lock the account's row (SELECT ... FOR UPDATE) from the start of the transaction until COMMIT or ROLLBACK No lock on read; only check the version column or a balance condition at write time
Deadlock / conflict handling Accounts must always be locked in a fixed order (e.g., ascending account_id) to avoid two transactions locking each other The write condition includes both version and balance >= amount, e.g., UPDATE ... WHERE id = 1 AND version = 5 AND balance >= 100
Trade-off Absolute safety, but lower throughput if an account stays locked too long High performance with few conflicts, but on a hotspot account the failure and application-level retry rate skyrockets

There is no universal answer between the two: the post presents them as a trade-off between absolute safety and performance, depending on the actual conflict level on each account.

How are duplicates prevented and distributed transactions handled?

Idempotency Key mechanism. Before sending a transfer request, the client generates a unique identifier (UUIDv4) called Idempotency-Key and sends it in the HTTP header. The backend stores this key in Redis or the transactions table with a UNIQUE constraint. If a second request arrives with the same key, the system skips the debit and immediately returns the result of the first transaction — however many times a flaky network repeats the request, money is deducted only once.

Cross-service transactions and the Saga Pattern. In a microservices architecture (e.g., a Payment Service separated from a Ledger Service and a Notification Service), the post advises against distributed Two-Phase Commit (2PC) locks because they stall the whole system. Instead, use the Transactional Outbox Pattern combined with the Saga Pattern in two steps: (1) within a single local ACID transaction on the main database, the system writes the entry to ledger_entries and an event record to the outbox_events table; (2) a background process (CDC — Change Data Capture, such as Debezium) reads from outbox_events and pushes to Apache Kafka to trigger the following steps (crediting the partner bank, sending balance-change notifications), with automatic refunds (compensating transactions) if a later step fails.

How does the core system scale without losing consistency?

The post names three scaling techniques, each solving a different bottleneck:

  • Separating read and write flows (CQRS): statement, history, and spending-statistics queries are asynchronously synced to a read-specialized system (such as Elasticsearch or read-replicas), so they do not affect the performance of the core database cluster handling writes.
  • Database sharding by account_id: when the ledger grows to tens of terabytes, data is partitioned by account_id. The post's caveat: a transfer between two accounts on different shards becomes a distributed transaction, requiring an intermediate coordination mechanism to preserve atomicity.
  • Hot account splitting: for a corporate master account receiving thousands of transactions per second, instead of funneling everything into one row and causing lock contention, the system splits the account into many sub-accounts (e.g., 100 sub-accounts), randomly distributes transactions across them, and periodically aggregates the balance.

The post's conclusion: designing a banking transaction system is a problem that puts data integrity and auditability above every speed metric — by combining an immutable double-entry model, strict contention control through lock ordering, idempotency from the network edge, and flow separation through the Outbox Pattern, the system can run stably and with absolute accuracy under any load pressure and infrastructure failure.

Frequently asked questions

1. Why can't banking systems use eventual consistency like social networks? Because money must never be "approximately right": according to the post, financial systems require absolute accuracy down to the last cent, with money never disappearing or appearing from nothing, so strong ACID consistency is mandatory.

2. How does double-entry bookkeeping differ from adding and subtracting a balance column directly? Double-entry records each transaction as at least two entries (debit/credit) summing to zero in an append-only ledger, so every movement is traceable and reconcilable. Adding or subtracting a single balance column cannot trace why a balance moved and is error-prone at reconciliation.

3. How are mistakes corrected in an immutable ledger if UPDATE is forbidden? With a reversal entry: a new entry is written that cancels out the wrong one, instead of editing or deleting the old line. History therefore stays complete and auditable.

4. How does an Idempotency Key prevent duplicates on a flaky network? The client sends a unique key (UUIDv4) in the header; the backend stores it under a UNIQUE constraint. A repeated request carrying the same key immediately receives the first transaction's result without being debited again.

5. When should pessimistic locking be used, and when optimistic locking? According to the post's analysis, pessimistic locking gives absolute safety but reduces throughput when an account stays locked; optimistic locking performs well with few conflicts but retries skyrocket on hot accounts. The choice depends on each account's actual conflict level.

Source: post by Duy Anh Bui in the Golang Backend group (18/11/2026). The designs and interpretations in this article belong to the post's author and have not been independently verified by Wotech.