LOAD-BEARINGfree
Low-level design dojoTier 13 · unit 23 of 31

Splitwise ledger

Track shared expenses in a group. An expense is paid by one or more people and split among several, equally, by exact amount, or by percentage. The app shows who owes whom.

Equal, exact and percentage splits all exist and more will be added. Which structure?

  • An enum plus a switch in Expense.split()
  • A SplitStrategy interface with one implementation per type
  • Three subclasses of Expense
  • A nullable field per split type
Why

Strategy again — this is the highest-frequency pattern in low-level design. The Expense holds amount, payer and participants; how the amount divides is a separate, extensible concern.

Where should the rule "exact splits must sum to the total" be enforced?

  • In the UI form
  • Inside ExactSplitStrategy, rejecting invalid input
  • In a nightly reconciliation job
  • In the database schema
Why

The strategy that defines the split owns its own validity rule. Push it to the UI and the API accepts corrupt data; push it to a nightly job and you find out a day late.

Assign each responsibility.

Store amount, payer, participants and description
Expense
Divide an amount across participants
SplitStrategy
Maintain net owed between every pair
BalanceSheet
Reduce the debt graph to fewest transfers
SettlementService
Validate that shares sum to the total
SplitStrategy
Why

Net balances and settlement simplification are different problems: the balance sheet is a running state, the settlement is an algorithm run on demand over that state. Keeping them apart means you can change the minimisation algorithm without touching how balances accumulate.

A owes B ₹500, B owes C ₹500. Minimum transfers to settle?

  • Two: A→B and B→C
  • One: A→C for ₹500
  • Three
  • Cannot be simplified
Why

Net every participant to a single position, then greedily match the largest creditor with the largest debtor. B nets to zero and drops out entirely. This is why balances are stored as net positions rather than as a list of raw debts.