Monzo: How We Calculate Balances
Transcription of handwritten notes on Monzo's ledger and balance calculations.
Source reference: [1]
Source: monzo.com
How we do calculate balances#
Ce blog source monzo plan to explain How balances are calculated not just app balances but beyond That, other balances That makes it more complicated Than it looks like relatively ofcourse.
- So it promises an explaination of why old approaches needed improvement and The changes That were maid.
So it explain Things we already seen such as Ledger wich is The source of Truth for money moving by The bank on behalf of customers.
So the ledger is built as a double-entry bookkeeping system where every movement of money has equal and opposite movement. The collection of all entries of a single movement is refered to as a 'EntrySet'.
So There is also a microservice called 'service.ledger' That handles The operations in The backend of The banc.
Address#
address is entity That lives on the ledger service. it's like a URL on webpages, it represent The movement of [illegible] from an entity to The other.
example of a user's main account:
Address
com.monzo.account : main : monzo.uk : GBP : acc_123
namespace name legal currency acc-Id
entityThat was an illustration of an address in a ledger service.
An address is consisted on a main five components:
- Namespace: The namespace tells us is a unique domain That informs us. example:
com.monzo.accounttells us That The address is a monzo account orcom.monzo.mastercardto inform us That The address is a mastercard account. - Name: is The type of The account example The main account is The account containing The balance or like The overdraft which is The account containing bellow zero or like 'monzo flex' which are purchase products which are buy now, pay later in which a credit is added let's say a 50$ you use it. The pay it later with interests like a loan.
- Legal entity: tells us which entity is responsible for That account. a UK account monzo UK is responsible The legal registred companie in UK.
- Currency: The currency That The account who The address belongs to is using. MAD, USD ...
- Account Id: The unique identifier number That distinguishes every monzo account. The address contain The Id to know which account The address belongs to.
Balance#
Types of balances#
Customer-facing balance#
available balance which reflect The real-Time transactions you've made using your account.
Interest chargable balance#
money in a customer account excluding pending Transactions yet to settle.
In The ledger's EntrySet we store The committed Time-stamps, which is when The entry was stored in The ledger. The committed timestamp is used to calculate fees interests chargeable balance.
So The primary purpose of This balance is to calculate overdraft charges. overdraft charges are not charged till The pending Transaction has been settled. example: spend 50$ now balance is -30$ you top up The account +30$ while The Transaction is pending not yet committed so nothing is charged.
Axis of service.ledger#
- Committed: The time when the entry was stored in The ledger. recorded.
- Reporting: when The entry has an accounting impact or more like when Transaction was created.
- flake: is a unique identifier that records the timestamps encode and record it for lexicographical time ordering for overall set of entries.
flake {
timestamp component,
machine or worker IDs,
a sequence / random compo[nen]t
}Lexicographic Time ordering: c'est le fait de stock or storing The timestamps as alphabets That we can know The timestamps from it. sequentially like ordering it.
The blog here explains The endpoints like Calling /balance with an account-I[d] and a list of addresses and Time axis of 'committed' to calculate a balance.
So The endpoint will retrieve The list of amounts individual amounts related to This or Those addresses Then summing it up or ... or like Calling it with reported in general using The API.
- a need for one source of Truth (data centralisation) So balances are spreaded across multiple codebases and syncing is not automatic so it's difficult to talk about balances company-wide and reporting balances That aren't consistent yet to regulators.
- Another thing is The overall balance for a giving account, so we often do need to have an overall balance for a giving Account [committed/reporting] across all ledgers. so calculating a customer balance should be done by calling some endpoint Then passing The Time axes [committed/reporting] then retrieving The balance. So it should be an overall balance not a sum of calling each ledgers balance.
- So an ease of reconciliation of amounts in different systems: different systems may use or rely on different data for calculation different Time axis or like different ledgers ...
4- The flake probleme: Calculating timestamps using flake The expose it will expose The internal implementation. So using flakes is not the best option.
a Visualization of a Balance Calculation#
Address config file Balance def config
holds hard coded map holds hard coded map
of address names to of balance names
balance definition to balance def details struct
Address name Balance def list Balance name Balance def detail
This would be a This is a list of e.g "actual balance" This includes a
string name balance definition description for
(e.g names (e.g The balance
com.monzo.account: "actual_balance") and Time-axis
main) Type e.g:
reported/committedAll of This together The Balance Definition.
Note: it's not necessarily calculating The balance.
ex:
we have Two files:
- address.config.ts: defining each address to wich balance def is associated to
- Balance-def.config.ts: defining what each balance does actually means
file 1: TypeScript
export const address_config = {
"com.monzo.account:main": [
"actual_balance",
"available_balance",
"interest_chargeable_balance"
],
"com.monzo.account:savings": [
"actual_balance",
"interest_chargeable_balance"
]
}
export const balance_def_config = {
actual_balance: {
definition: "The actual balance of [cut off]",
time_axis: "committed"
},
available_balance: {
definition: "Balance available in acc[cut off]",
time_axis: "reported"
}
}So The diagram showed an example of getting The balance definition in a given address. along with That mock implementation.
- Now for The calculation of a balance, first you look in for The balance-name if it exist The it return The address.
- Then we get The list of The entrySet for that account meaning The data recorded in The Transactions.
- The we sum up The entries amounts and we exclude what's out of The timestamps range. and The Time axis will be declared by The balance definition either committed or reported.
So This help in:
- accuracy: the data now is accurate
- Consistency: The data is consistent now between The backend and The warehouse.
- The abstraction: the isolation in which The balance can be calculated without exposing The whole implementation.
References
- 1How we calculate balancesMonzo · 2022