TIL - Double Entry Ledgers
TIL: Understanding Double-Entry Ledgers
Double-entry ledgers are a fundamental pattern for tracking resource movement in any system where accuracy is critical. Every movement must be recorded with two balanced entries - representing where the resource came from and where it went.
The core system comprises three parts:
- Accounts: Represent places resources can exist
- Entries: Record individual movements, always marked as either credit or debit
- Transactions: Group related entries that must balance (total credits = total debits)
Key implementation principles:
- Never store balances as properties - calculate them by summing entries
- Avoid negative numbers - use explicit credit/debit types instead
- Entries are immutable except for status changes (pending → posted)
- Transactions provide atomicity and rollback capability
- Keep ledger separate from domain models
This pattern prevents common issues like missing funds, simplifies auditing since every movement is traceable, and makes reconciliation straightforward. While it requires more upfront design work, it's far easier to implement initially than to retrofit later.
The same principles apply beyond financial systems - this pattern works for tracking any resource that needs accurate accounting and auditability.