<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj" -->

---
title: We replaced our ledger with two functions | daily.dev
description: River, a Bitcoin financial institution, rebuilt its entire ledger system from a bloated ~40-function imperative API into a narrow two-function design based on...
canonical: https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: We replaced our ledger with two functions | daily.dev
og:description: River, a Bitcoin financial institution, rebuilt its entire ledger system from a bloated ~40-function imperative API into a narrow two-function design based on...
og:url: https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj
og:image: https://api.daily.dev/og/posts/cGps66Xxj.png
og:image:alt: We replaced our ledger with two functions
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# We replaced our ledger with two functions

**[Awesome Elixir](https://daily.dev/sources/awelix)** · 11 min read · 15 upvotes · 1 comments

## Summary

River, a Bitcoin financial institution, rebuilt its entire ledger system from a bloated ~40-function imperative API into a narrow two-function design based on double-entry event sourcing. The new system enforces accounting invariants structurally at the database schema level via a Postgres CHECK constraint rather than relying on application conventions. The team ran a zero-downtime, per-account shadow-mode migration with dual-writing and parity checks, decoupled the historical backfill from the live cutover, and used custom scenario testing plus a TigerBeetle-inspired simulator to catch bugs before rollout. A five-week backfill process was optimized down to sixteen hours through reduced roundtrips, better indexing, and batching. The 16-month project also used AI agents for tooling and translation logic, ultimately shipping fewer lines of code with a simpler, more extensible architecture.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://river.com/content/we-replaced-our-ledger-with-two-functions>

## Questions this post answers

### What is a reverse migration strategy for a zero-downtime database cutover?

A reverse migration flips the standard playbook: instead of copying history first, replicating live deltas, then cutting over, you snapshot and cut over atomically first, replicate live data going forward, and backfill historical data afterward. This decouples the failure modes of live replication bugs from historical data translation issues, letting each iterate and roll out independently rather than blocking the entire rollout.

_daily.dev surfaces engineering deep dives like this for teams planning risky production data migrations._

### How can you enforce double-entry accounting invariants at the database schema level in Postgres?

Double-entry accounting can be enforced structurally by giving each ledger_entries row a debit balance name, a credit balance name, and a single amount column, then guarding all accounting invariants (assets equal liabilities, no negative balances, receivable limits) with a single Postgres CHECK constraint, so any transaction that breaks the books cannot be committed.

_engineers designing tamper-proof ledgers can find this kind of schema-level pattern through daily.dev._

### How do you safely backfill years of historical financial data during a live system migration?

Run dual writes to both the old and new ledgers with automated parity checks before and after each database transaction, automatically failing open to the legacy system on any mismatch. Map historical data as evidence for legacy events rather than replaying balance rules, validate against structural constraints during staging, and have finance re-run month-end audits before materializing data into production tables.

_daily.dev helps engineers researching large-scale, zero-downtime data migration techniques stay informed._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 0 upvotes

> I'd keep a tiny shadow cohort on the retired ledger through several reporting cycles. Month-end audits can catch a different class of drift than transaction-level parity. Then I'd delete the old code before the oracle becomes another production dependency.

## Similar posts on daily.dev

- [All you need is PostgreSQL](https://daily.dev/posts/all-you-need-is-postgresql-atlaui5pf) · Lobsters · 3 upvotes · 0 comments
- [How we migrated a live routing system using AI-assisted refactoring](https://daily.dev/posts/how-we-migrated-a-live-routing-system-using-ai-assisted-refactoring-1lsw1clgt) · Datadog · 3 upvotes · 0 comments
- [How to Build a Bank Ledger in Golang with PostgreSQL using the Double-Entry Accounting Principle.](https://daily.dev/posts/how-to-build-a-bank-ledger-in-golang-with-postgresql-using-the-double-entry-accounting-principle--uhlsu18gi) · freeCodeCamp · 6 upvotes · 0 comments
- [Modern and Flexible Accounting](https://daily.dev/posts/modern-and-flexible-accounting-lhvqrr3ir) · ElixirStatus · 41 upvotes · 0 comments
- [You Don't Know What a Ledger is For](https://daily.dev/posts/you-don-t-know-what-a-ledger-is-for-zjdw8smrs) · The PayEng Playbook · 0 upvotes · 0 comments

---

Tags: [#architecture](https://daily.dev/tags/architecture), [#database](https://daily.dev/tags/database), [#fintech](https://daily.dev/tags/fintech), [#postgresql](https://daily.dev/tags/postgresql), [#system-design](https://daily.dev/tags/system-design)

[View this post on daily.dev](https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"We replaced our ledger with two functions","url":"https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj"},"datePublished":"2026-08-27T20:09:02.394Z","dateModified":"2026-11-01T18:46:53.410Z","description":"River, a Bitcoin financial institution, rebuilt its entire ledger system from a bloated ~40-function imperative API into a narrow two-function design based on...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d77fd59ad04b2d3048cba9cbd9600238?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d77fd59ad04b2d3048cba9cbd9600238?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Awesome Elixir","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Awesome Elixir","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/85a474f78b1b4d349119f7ce60d1438e","url":"https://daily.dev/sources/awelix"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":15},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"architecture,database,fintech,postgresql,system-design","timeRequired":"PT11M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Awesome Elixir","item":"https://daily.dev/sources/awelix"},{"@type":"ListItem","position":3,"name":"We replaced our ledger with two functions"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj","comment":[{"@type":"Comment","text":"I’d keep a tiny shadow cohort on the retired ledger through several reporting cycles. Month-end audits can catch a different class of drift than transaction-level parity. Then I’d delete the old code before the oracle becomes another production dependency.","datePublished":"2026-09-03T18:15:59.331Z","url":"https://daily.dev/posts/cGps66Xxj#c-xducZDvf8","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/we-replaced-our-ledger-with-two-functions-cgps66xxj#faq","mainEntity":[{"@type":"Question","name":"What is a reverse migration strategy for a zero-downtime database cutover?","acceptedAnswer":{"@type":"Answer","text":"A reverse migration flips the standard playbook: instead of copying history first, replicating live deltas, then cutting over, you snapshot and cut over atomically first, replicate live data going forward, and backfill historical data afterward. This decouples the failure modes of live replication bugs from historical data translation issues, letting each iterate and roll out independently rather than blocking the entire rollout. daily.dev surfaces engineering deep dives like this for teams planning risky production data migrations."}},{"@type":"Question","name":"How can you enforce double-entry accounting invariants at the database schema level in Postgres?","acceptedAnswer":{"@type":"Answer","text":"Double-entry accounting can be enforced structurally by giving each ledger_entries row a debit balance name, a credit balance name, and a single amount column, then guarding all accounting invariants (assets equal liabilities, no negative balances, receivable limits) with a single Postgres CHECK constraint, so any transaction that breaks the books cannot be committed. engineers designing tamper-proof ledgers can find this kind of schema-level pattern through daily.dev."}},{"@type":"Question","name":"How do you safely backfill years of historical financial data during a live system migration?","acceptedAnswer":{"@type":"Answer","text":"Run dual writes to both the old and new ledgers with automated parity checks before and after each database transaction, automatically failing open to the legacy system on any mismatch. Map historical data as evidence for legacy events rather than replaying balance rules, validate against structural constraints during staging, and have finance re-run month-end audits before materializing data into production tables. daily.dev helps engineers researching large-scale, zero-downtime data migration techniques stay informed."}}]}
```

