Building Balances I: Low-latency reads

May 22, 2025
Vignesh Vaidyanathan
Software Engineer

FRAGMENT's Balance API supports both transactional and analytical workloads, without making complex trade-offs.

Transactional Balances

FRAGMENT's Balance API supports strongly-consistent balance reads and conditional writes.

Every time a Ledger Entry is posted over the API, the system synchronously updates the total balance of every consistent Ledger Account in the Ledger Entry. This write uses DynamoDB concurrency control mechanisms to support conditional updates that prevent scenarios like double-spend.

Transactional balances are fast to read since they are computed on write. This lets users pull from FRAGMENT to display balances in their product and use FRAGMENT for transactional workloads such as card authorization.

Historical Balances

FRAGMENT's Balance API supports reading the balance of a Ledger Account at any point in time down to the hour.

Storing balance deltas is the most efficient implementation of a real-time historical balance system. Computing historical balances on read is inefficient since the number of Ledger Entries affecting a Ledger Account's balance is unbounded. Computing historical balances on write leads to cascading updates when posting a backdated Ledger Entry.

FRAGMENT stores balance deltas at the hour, day, and month granularities. When a Ledger Entry is posted, the system takes the entry's timestamp and updates the balance deltas for its corresponding year, month, day, and hour. This creates a fixed write cost, regardless of the timestamp the Ledger Entry is posted to.

When a user needs to read a balance at a given point in time, the system reads the corresponding year, month, day, and hour records and computes the historical balance. This results in a fixed upper-bound latency when reading historical balances.

By storing balance deltas, FRAGMENT's balance system has predictable performance characteristics at scale.

Closing remarks

Decoupling transactional balances from historical balances allows them to be optimized independently. In production, FRAGMENT's API has a consistent latency of 30ms for transactional balance reads and 130ms for historical balance reads.

If building mission-critical financial infrastructure powering the next generation of financial products excites you, we're hiring.