Square: Books and Double-Entry Accounting
Transcription of handwritten notes on Square's Books accounting database.
Source reference: [1]
Source: developper.squareup.com
note: it's an 2019 blog
Books, an immutable double-entry accounting database service#
Tracking financial transactions at scale#
- The entreprise talks about it progress and grow in scale incrementation of products That needed a scalable and flexible infrastructure because of it movement beyond simple payment processing.
- Speaks again about settling payment to merchants which became increasingly complexe and also The inconsistency of data That results to customers inquiries (reporting from customers) and inconsistency of data.
- So The entreprise invested in a scalable infrastructure to make it simpler specifically for tracking financial transactions to ensure That every penny is accounted and The infrastructure is scalable to adapte to diverse products without putting The heavy dutty on engineers.
- So a new kind of databases was introduced which are books The entreprise relied on google cloud spanner and google kubernet engine to make it possible. The data is inherently consistent as a result of applying double-entry accounting.
The previous#
ledger-entries#
| id | merchant | amount | Txn-Type | payout-Id |
|---|---|---|---|---|
| 100 | ABC123 | $100 | PAYMENT | 5 |
| 101 | ABC123 | $80 | PAYMENT | 5 |
| 102 | ABC123 | -$98 | REFUND | 5 |
Payouts#
| id | merchant | amounts |
|---|---|---|
| 5 | ABC123 | $82 |
So it work by making adjustements of accounting by running it against The table when it's payed out it adds The payout id That flags it payed out.
- The strategie is not scalable.
- So to calculate a balance an expensive group by aggregate for each merchant, storing The balance and executing The ACH transfer in a timely fashion. The Thing That makes it struggle for millions of daily payouts and more There is some product That can create O(# payment) and additional entries may depends on if The merchants has loans.
- The impossibility of making a split of a single payment to multiple transactions since it's a 1→n relation from The payments to payout which is limited. a single $100,000 transaction may be considered risky so a decision of block either The entire transaction or none of it.
- Another thing is SQL liability to rely on it for sensitive infos such as payout-Id That can be nulled.
it mention issues such as The growing bussiness.
So They had to improvise like using sharding or like building risky model on top of The ledger to mark "unsettled" entries. which lead to cross-shard transactions or marchand exchanging values wich is a very bad design can be corrupted and is inconsistent.
Solution#
The Books services offers the following properties:
- flexibility: should be able to scale arbitrary complicated model and relationship between customer and square.
- Consistency: The financial data has to be consistence ex: a $100 paid out ledger entry but only a $50 payout it should be impossible.
- Scale: must support massive scale in terms of individual account size and totale data stored.
The BLOG explains double entry we already did it so There is no need.
Modeling Double Entry accounting in SQL#
At a Higher level
books shelves
shelf_id STRING shelf_id STRING
book_id STRING name STRING
group_id STRING
creator_id STRING journal_entries
idempotence_key STRING shelf_id STRING
sum_to INT64 journal_entry_id STRING
sum_from INT64 journal_entry_type STRING
type STRING idempotence_key STRING
owner STRING metadata BYTES
version INT64 request_body BYTES
commit_time TIMESTAMP
cursor_shard INT64
book_entries
shelf_id STRING
journal_entry_id STRING
book_id STRING
book_version INT64
book_entry_id STRING
to_amount INT64
from_amount INT64
book_entry_type STRING
book_sum_to INT64
book_sum_from INT64
metadata BYTES- it can be seen like a Book is something holding balance ex an account, Then a shelve is a container holding all books of a customer.
- and The index is what prevents us from scanning all books to find The intended, secondary index is an index of something other Than The main-ID.
References
- 1Books, an immutable double-entry accounting database serviceŁukasz Strzałkowski · 2019