Classic design · Staff
Digital wallet (with a parallel to Pix)
Classic interview design. Key points: ledger and reconciliation.
Short answer
Classic interview design. Key points: ledger and reconciliation. In Square's account, triggering a bank settlement required computing the merchant's balance, which meant an expensive group-by aggregation per merchant. In addition, because it was just a SQL database, nothing prevented payments from becoming inconsistent.
01 · CONTEXT
From aggregated balance to ledger
In Square's account, triggering a bank settlement required computing the merchant's balance, which meant an expensive group-by aggregation per merchant. In addition, because it was just a SQL database, nothing prevented payments from becoming inconsistent.
Stripe, for its part, built the Ledger, an immutable and auditable log, as the system of record for its financial data.
02 · DOUBLE ENTRY
Entries that net to zero
Double-entry accounting requires stating not only which financial state change occurred, but also why. By the accounting equation Square uses, every transaction, called a journal entry, must net to zero: every cent lost is offset by a cent gained.
The stated requirement is consistency: it must not be possible to transact with the database in a way that results in an illogical financial transaction.
| Square Books table | Papel |
|---|---|
| books | One row per book; holds a balance and may have a type, owner, and unique identifier. |
| journal_entries | A row is added each time the books change value in an accounting transaction. |
| book_entries | Each row has foreign keys to a journal entry and to a book and holds a debit (+) or a credit (-). |
Each book holds a balance that summarizes that book's changes. In Square's example, computing how much to pay a merchant is a single row in the database and needs no complex grouping or aggregation.
The diagram starts at the wallet app, connected to the wallet service.
The diagram highlights the service and the journal entry storage: each entry declares what changed and why, and a row is added each time the books change value.
Journal entries and the debit and credit entries only receive inserts.
The book holds a balance that summarizes its changes, and that current balance is updated on every operation.
03 · IMMUTABILITY
Correcting without deleting
At Square, the sets of journal entries and book entries are effectively append-only and immutable once written.
At Stripe, the Ledger is, at its core, an immutable log of events. Transactions that have been posted cannot be deleted or modified, and past state can be reconstructed by processing the events up to that point.
Faced with an error, Square does not update the data: it writes a new entry that corrects the previous one. The book's balance is adjusted, but there is always a trace of what happened in the system.
| Aspecto | Square Books | Stripe Ledger |
|---|---|---|
| Data already written | Effectively append-only and immutable once written. | Posted transactions cannot be deleted or modified. |
| Error correction | Writes a new entry that corrects the previous one. | Reverts and reprocesses earlier operations. |
04 · RECONCILIATION
Verifying that the balances close
Stripe represents the producing systems as state machines and describes their behavior as a logical flow of funds, the movement of balances (events) between accounts (states). The Ledger is based on double entry, the standard method for ensuring that all the money in a system is accounted for by balancing credits and debits.
The diagram shows a producing service with its database and the event ledger.
The producing systems are modeled as state machines, and the Ledger describes their behavior as a flow of funds.
The clearing metric measures the fraction of the ledger that is zeroed out as it should be in steady state.
The completeness check ensures that every ID in the producer's database has a corresponding event in the ledger.
For timeliness, it measures the difference between data entering the platform and arriving at the ledger.
The Ledger relies on proactive alerts to surface problems and proposed solutions.
| Stripe metric | Question it answers |
|---|---|
| Clearing | Did the flow of funds complete correctly? |
| Timeliness | Did the data arrive on time? |
| Completeness | Do we have a complete representation of the underlying data system? |
If the release event is never published or has the wrong id, the Ledger does not offset the associated account's balance. Even with billions of transactions, a single missing, late, or incorrect transaction immediately creates an accuracy problem detectable by a simple query.
05 · SCALE AND INTERVIEW
Ledger storage and scale
Square reports considerable effort on a SQL database sharding scheme and says it had to deal with cross-shard transactions, in which two merchants could exchange value.
For Books, Square chose Google Cloud Spanner, a fully managed, globally distributed SQL database, which freed it from worrying about sharding and from the burden of maintaining a database.
Frequently asked questions
What is Digital wallet (with a parallel to Pix)?
Classic interview design. Key points: ledger and reconciliation.
What does the Digital wallet (with a parallel to Pix) design cover?
It is organized in these sections: From aggregated balance to ledger, Entries that net to zero, Correcting without deleting, Verifying that the balances close, and Ledger storage and scale.
How long is Digital wallet (with a parallel to Pix) and what level is it?
It takes about 17 minutes to read and is rated Staff level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.