Skip to main content
    listerineh.dev
    • About
    • Experience
    • Projects
    • Skills
    • Blog
    • Talks
    • Contact
    • Why
    • About
    • Experience
    • Projects
    • Skills
    • Blog
    • Talks
    • Contact
    • Why

    © 2026 Sebastian Alvarez

    All projects

    Payments Ledger

    A double-entry payments ledger in Go and Postgres: each transfer posts exactly once per idempotency key, balances always match their entries, and events leave through a transactional outbox.
    Source Code
    Payments Ledger
    Role
    Backend Engineer
    Timeline
    November 2026
    Status
    Completed

    In numbers

    Transaction from 50 simultaneous retries with the same key
    11
    Deadlocks with 32 workers moving money both ways
    00
    Median post across 1,000 accounts, 8 concurrent clients
    2.42.4ms
    p99 when 32 clients fight over 10 hot accounts
    207207ms

    About the Project

    Payments Ledger is the core of a payments system: accounts, transfers and the guarantees that keep money from appearing, disappearing or moving twice. Every transaction is a set of entries that sum to zero per currency, posted atomically and exactly once per idempotency key.

    A transfer is one Postgres transaction. It first inserts the idempotency key, so a concurrent retry with the same key waits and then gets the original result back. It locks the accounts involved in id order, so two opposite transfers can never wait on each other. It checks currency and overdraft, which a database constraint also enforces, writes the entries, updates the balances and inserts an event into an outbox table. Relay workers claim outbox rows with FOR UPDATE SKIP LOCKED and publish each one. It is Go 1.27 with the standard library router, pgx, errgroup and slog, and about 300 lines of hand-written SQL instead of an ORM.

    The tests prove the guarantees against a real Postgres, with the race detector on: 50 simultaneous retries with one key produce exactly one transaction, 32 workers moving money in both directions never deadlock and never lose a cent, and no outbox event is published twice. CI fails if any of those tests is skipped. I built it in November 2026 to show correctness under concurrency, the part of backend work that a demo never shows.

    Technologies
    1. API & backend

      • Go
    2. Data

      • PostgreSQL
      • pgx
    3. Cloud & infra

      • Docker

    Relevance

    Correctness under concurrency, proven with tests: idempotency, deadlock-free locking, invariants that hold under load, and reliable events through an outbox.

    How it works

    The path a request takes, step by step.

    1. 01 · PersonClientPOST /transactions with an Idempotency-Key
    2. 02 · ServiceAPIGo net/http, entries must sum to zero
    3. 03 · DataPostgresKey, ordered row locks, entries and balances
    4. 04 · QueueOutboxThe event, in the same transaction
    5. 05 · ServiceRelay workersFOR UPDATE SKIP LOCKED
    6. 06 · ResultEventPublished once
    A retry with the same key waits for the first one and gets its result back marked as a replay; the same key with a different body is rejected.

    Decisions

    The choices that shaped it, and what each one cost.

    1. 01

      Lock accounts in id order

      Why
      Every transfer locks its accounts sorted by id, so two opposite transfers can never wait on each other.
      What it cost
      Pessimistic locking: on 10 hot accounts, more concurrency adds no throughput and the p99 jumps to 207 ms.
    2. 02

      Claim the idempotency key first

      Why
      The key is the first insert of the transaction, so a concurrent retry waits on it and then replays the result. 50 simultaneous retries posted once.
      What it cost
      A failed post must not burn the key, which is one more path to handle and test.
    3. 03

      Publish through a transactional outbox

      Why
      The event is written in the same transaction as the money, so there is never a transfer without its event or an event without its transfer.
      What it cost
      One more table and a relay to run, and consumers still need to dedupe if a relay dies between publishing and marking.
    4. 04

      Hand-written SQL instead of an ORM

      Why
      About 300 lines of SQL keep every lock and its order visible in the code.
      What it cost
      Every query is written and mapped by hand.

    Next project

    Vector Search in GoAn HNSW vector index in pure Go, with a parallel graph build and lock-free search on every core.Next project
    listerineh.dev

    Full-stack engineer building AI agents, cloud platforms and web products. Based in Quito, working remotely.

    Open to new projects

    Work

    • Projects
    • Experience
    • Skills
    • Blog
    • Talks

    About me

    • About
    • Why Listerineh?
    • Contact

    Connect

    • GitHub
    • LinkedIn
    • Instagram
    • Email

    © 2026 Sebastian Alvarez · All rights reserved.

    • Privacy Policy
    • Terms of Use
    • Unsubscribe from the newsletter
    • Component Library