Backend & Full Stack Engineer Guide: Two-Sided Marketplaces, Escrow Ledgers & Split Payouts

Two-sided marketplaces—whether connecting patients with specialists, shippers with carriers, or enterprises with service providers—cannot store balances as a single mutable `wallet_balance` float column. Concurrent booking cancellations, partial refunds, platform commission splits, and multi-currency payouts require an immutable double-entry accounting ledger in PostgreSQL paired with idempotent payment orchestration. As a Senior Backend Engineer and Full Stack Engineer, here is how I architect financial-grade marketplace backends.

1. Double-Entry Escrow Ledger with Deterministic Lock Ordering

Every funds movement (buyer authorization capture, escrow hold, platform fee deduction, and vendor payout release) records balanced debit and credit journal lines whose sum strictly equals zero. When transferring funds between two accounts inside a database transaction, sorting account IDs before executing `SELECT ... FOR UPDATE` prevents AB-BA deadlocks under high concurrency:

from decimal import Decimal
from django.db import transaction
from apps.ledger.models import LedgerAccount, JournalEntry, JournalLine

def record_escrow_release(
    escrow_account_id: int,
    vendor_account_id: int,
    platform_fee_account_id: int,
    gross_amount: Decimal,
    fee_amount: Decimal,
    idempotency_key: str,
) -> JournalEntry:
    net_vendor_amount = gross_amount - fee_amount
    if net_vendor_amount < Decimal("0.00"):
        raise ValueError("Fee cannot exceed gross escrow amount.")

    ordered_ids = sorted({escrow_account_id, vendor_account_id, platform_fee_account_id})
    with transaction.atomic():
        # Lock accounts in deterministic ascending ID order to prevent deadlocks
        locked = {
            acc.id: acc
            for acc in LedgerAccount.objects.select_for_update().filter(id__in=ordered_ids).order_by("id")
        }
        entry, created = JournalEntry.objects.get_or_create(
            idempotency_key=idempotency_key,
            defaults={"description": "Escrow release and platform commission split"},
        )
        if not created:
            return entry

        JournalLine.objects.bulk_create([
            JournalLine(entry=entry, account=locked[escrow_account_id], amount=-gross_amount),
            JournalLine(entry=entry, account=locked[vendor_account_id], amount=net_vendor_amount),
            JournalLine(entry=entry, account=locked[platform_fee_account_id], amount=fee_amount),
        ])
        return entry

2. Hold-Until-Fulfillment & Automated Dispute Reconciliation

By separating payment capture from vendor disbursement via an escrow holding account, marketplaces protect buyers until service milestones are verified while giving finance teams a complete, auditable trail for every cent.

3. Hire a Senior Backend & Full Stack Marketplace Engineer

Building a two-sided B2B/B2C marketplace, booking engine, or escrow payout system? Explore my Backend Engineer Pillar Guide, review my Payment Gateway & Billing Architecture Guide, or book a 1-Day Marketplace Architecture Sprint.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs