All Insights
Backend Architecture2026-11-15 · 5 min read

Scaling Laravel & MySQL Ledgers Past 30,000+ Active Users Without Locking Bottlenecks

Practical database indexing, transactional wallet updates, and cron batching patterns learned from high-volume commission systems.

1. Why Commission & Wallet Tables Slow Down First

In MLM, affiliate, and SaaS billing systems, the first bottlenecks rarely happen in PHP itself — they happen inside unindexed relational joins and unguarded wallet balance updates.

When multiple referral events or daily cron payouts write to the same parent wallet row simultaneously, row-level lock contention spikes latency.

2. Atomic Transactions + Idempotency Keys

Every payout or balance mutation should execute inside a database transaction with a deterministic idempotency key so retries never double-credit a user.

DB::transaction(function () use ($userId, $amount, $cycleKey) {
    $alreadyPaid = Ledger::where('idempotency_key', $cycleKey)->exists();
    if ($alreadyPaid) return;

    $wallet = Wallet::where('user_id', $userId)->lockForUpdate()->firstOrFail();
    $wallet->increment('balance', $amount);

    Ledger::create([
        'user_id' => $userId,
        'amount' => $amount,
        'idempotency_key' => $cycleKey,
    ]);
});

3. Composite Indexes & Chunked Cron Workers

Add composite indexes aligned with your WHERE + ORDER BY clauses (for example `(sponsor_id, status, created_at)`), and process daily ROI or rank rollups in deterministic 500-row chunks rather than loading thousands of Eloquent models into memory at once.

Need help building a system like this?

Book a Call