The platform is private, so this page describes patterns and decisions, not product code or names. I've been on its backend team from the start, as a core developer of the transaction and money flow and the partner commission engine. The five decisions below I designed and built myself. I've rebuilt the same patterns from scratch, with fake payment gateways and concurrency tests, in an open-source Laravel project: laravel-wallet-ledger on GitHub.
The problem
Users hold a wallet balance, top it up through two external payment providers (one crypto, one fiat, both behind a payment aggregator), move funds inside the platform, and withdraw. Every one of those steps crosses a boundary we don't control: a network call, a webhook that may arrive late, twice, or never, and users who double-click "Withdraw".
Around 100,000 transactions a day is only the visible part. Each one also writes its double-entry ledger entries and can trigger bonus, commission and affiliate entries, so the system records about 1 million money entries a day, and every one of them has to land exactly once and agree with the others. At that volume, "rare" edge cases happen every day, like a transaction that settles while one of its follow-on entries fails, or a retry that writes the ledger twice. The goal was simple to say and hard to do: every unit of money is accounted for, exactly once, even when something fails halfway.
Constraints
- Untrusted timing. Provider webhooks can be delayed, duplicated or lost.
- Concurrency. The same user can fire several requests at once, from several devices.
- No downtime. Changes shipped into a live system with real balances.
- Small team. The design had to be understandable and testable by everyone, not clever.
Ground rules for every money operation
Before any of the bigger pieces, every wallet operation follows the same rules, so duplicates and races are stopped at the database rather than hoped away:
- Read and check state under a row-level lock, never before it. A withdrawal checks the balance only after it holds the lock.
- Lock rows in a fixed order, so two transfers between the same accounts can't deadlock each other.
- Unique keys on provider transaction IDs, so the same deposit can't be credited twice.
- A short Redis per-user submit lock stops double-clicks before they reach the database.
- A balance-cap circuit breaker and one pending request per payment method limit how far a bug or abuse can go.
Five design decisions
1 A double-entry, append-only ledger
Problem. A single balance column tells you what a user has, not why. When support asks "where did this amount come from?", you need history you can trust.
Choice. Every movement is written as a balanced pair of debit and credit entries. Entries are immutable: corrections are new entries, never edits. Amounts use fixed-point decimals with 8 places, and a unique key on reference + account + direction makes each write idempotent. Opening balances are checked with exact decimal comparison, not floating point.
Trade-off. More writes per transaction and more storage, in exchange for a full, provable history.
2 Ledger writes that fail safely
Problem. The ledger was added to a live system. If a ledger write failed, should the user's deposit fail too?
Choice. No. The ledger writer catches its own failures and stores them for replay, so a ledger bug can never block a user's money movement. Failed writes are visible and can be replayed once fixed.
Trade-off. The ledger can briefly lag the wallet, which is exactly why reconciliation matters (see "What I'd do next" below).
3 Withdrawals that can't strand funds
Problem. A withdrawal has two halves: debit the wallet locally and ask the provider to send money. If one half succeeds and the other fails, money is either lost or paid twice.
Choice. The provider request is made inside the database transaction. If the provider rejects it, the local debit rolls back. If the provider accepts it but the local commit then fails, the platform sends a cancel request upstream for the orphaned withdrawal.
Trade-off. The row lock is held during a network call, so timeouts must be tight. It was the simplest design that closed both failure windows; an outbox pattern is the next step (below).
4 Webhook settlement that never drops a payment
Problem. Deposits are confirmed by provider webhooks. A webhook that fails to process is a user who paid and didn't get credited.
Choice. Each webhook is authenticated with a constant-time signature check and its encrypted payload is decrypted before anything is trusted. If processing fails, the endpoint still answers 202 Accepted and records the failure for retry, so the provider doesn't hammer us and nothing is lost. Settlement is idempotent by provider transaction ID, so retries and duplicates are harmless.
Trade-off. A failed webhook is our responsibility to retry, not the provider's, so failures have to be visible.
5 Automatic deposit matching every 30 seconds
Problem. Some webhooks never arrive at all. Without a backstop, those deposits sit "pending" until a user complains.
Choice. A scheduled job runs every 30 seconds and matches pending deposits against the gateway's own records. Anything the gateway has completed gets settled through the same idempotent path as a webhook.
Trade-off. Extra API calls to the gateway, in exchange for a missed webhook costing seconds instead of a support ticket.
The partner commission engine
Growth on the platform runs through partners: people who refer customers and can recruit sub-partners of their own. Every partner earns a share of the net revenue their customers generate, and a share of what their network earns. This is the part of the system with the most money rules, so it got the most care.
A A commission chain that can't overpay
Problem. A partner network is a tree of unknown depth. Paying every ancestor a fixed percentage would let total payouts grow with every level.
Choice. Each partner has a rate, and a sub-partner's rate must sit at least 5 points below its parent's. When a customer generates revenue, the direct partner earns their rate, and each ancestor earns only the spread between its rate and its child's. The whole chain together never pays more than the top partner's rate, and the rate rules keep the tree shallow.
Trade-off. Recruiting is limited by the rate floor, which is a business rule the product team had to agree to.
B Revenue share with clawbacks
Problem. Revenue from a customer can go negative, for example after a large promotional payout. A commission system that only ever credits partners pays out money the platform never earned.
Choice. Commission is calculated on net revenue. When it turns negative, the same rate is debited from the partner, their balance can go below zero, and the deficit carries into the next cycle.
Trade-off. Partners can see a negative balance, so the dashboard has to explain it clearly.
C Exactly-once commissions from large batches
Problem. Commissions are calculated from large batches of transaction records from product partners. A crash halfway through must not pay anyone twice or skip anyone.
Choice. Records are processed in chunks of 500, with everything a chunk needs loaded up front to avoid per-row queries. Each row runs in its own savepoint, and the row is marked as processed in that same savepoint, so one bad row rolls back alone and a rerun skips finished rows. Every wallet change goes through one helper that takes a row lock and writes the matching ledger entries.
Trade-off. Savepoints add some database overhead, which is cheap compared with reconciling a half-applied batch by hand.
D Partner tiers and weekly payouts
Problem. Partners move between tiers with different rates, and they're paid weekly. Both need to be automatic, fair and auditable.
Choice. A job runs every 3 minutes and moves a partner up one tier only when they meet both targets: active customers and peak commission. The peak is a high-water mark, so spending earnings never causes a demotion. Downgrades depend only on active customers and never go below a floor. Every change is logged, and repair commands can run as a dry run first. A daily job pays every partner whose weekly payout is due. Each partner is paid in its own transaction, with a compliance check and a minimum amount (smaller balances carry over). Payouts are approved automatically or by an admin, and a reversal posts its own refund entries.
Trade-off. A 3-minute delay on tier changes, in exchange for a simple job that can't run twice at once.
Status
Wallet rules, withdrawals, webhook settlement and 30-second deposit matching are live in production.
What I'd do next
- Schedule automated wallet-to-ledger reconciliation.
- Store live balances as fixed-point decimals, matching the ledger.
- Move provider calls out of the database transaction with an outbox, so no lock waits on the network.
- Accept client idempotency keys on money endpoints, on top of the server-side guards.
- Enforce per-user daily and monthly limits, not only the balance cap.
- Expand test coverage for the ledger, commission and tier rules.
- Add a unique key on commission records as a second line of defence.