← All insights

2026-11-28 · INSIGHT

Crypto Exchange Matching Engine & Wallet Custody Architecture Guide

Inside a resilient CEX core: single-writer order book matching, liquidation risk isolation, decimal ledgers and hot/cold wallet custody.

On this page

A digital-asset exchange rarely fails because its UI chart is missing a widget. Production incidents almost always come down to three deep invariants: order execution ordering, balance conservation, and recoverability under timeout or partial failure.

Here is how ffex structures the matching engine, risk isolation and multi-chain wallet custody across both its Java and Go lines.

1. Single-writer in-memory matching

In ffex, each spot or derivatives market has one logical writer owning its in-memory order book with strict price-time priority:

  • No cross-table row locking on the match path — matching executes in order in memory, and persistence follows the fill as an ordered event.
  • Pre-trade balance and risk gates happen before an order hits the active book, preventing invalid orders from poisoning match state.
  • Snapshot + increment market data distributes order-book depth and trades with per-subscriber backpressure so one slow client never slows the engine.

2. Isolating risk and forced liquidation

Perpetual futures liquidation creates bursty workload spikes precisely when spot and contract order books are busiest. Mixing heavy liquidation loops directly into the matching thread risks tail-latency spikes across all users.

ffex splits mark-price risk evaluation, forced liquidation and insurance fund accounting into dedicated risk and clearing services communicating through typed contracts and ordered Kafka events. Matching stays predictable even during sharp market moves.

3. Journaled decimal ledgers & explicit UNKNOWN handling

Financial state mutations follow strict ledger discipline:

  • Zero floating-point arithmetic — amounts, prices, fees and margin remain exact decimal strings/types end to end.
  • Append-only balance journals — available and frozen balance transitions stay conserved and auditable.
  • Idempotency keys + UNKNOWN convergence — when a downstream custody or chain RPC call times out, the operation enters an explicit UNKNOWN state and converges via verified reconciliation rather than blind automatic retries that cause duplicate withdrawals.

4. Multi-chain wallet custody & approvals

Wallet and custody isolate on-chain risk from trading hot paths:

  • Per-asset deposit/withdrawal address management and chain confirmation tracking.
  • Hot / cold segregation with low hot-wallet thresholds and multi-step back-office approvals (backed by TOTP and audit trails) before cold sweeps.
  • Automated internal-to-on-chain reconciliation catching drift before it compounds.

Because OpenTelemetry trace context flows from the HTTP/WebSocket edge across gRPC, Kafka batches and MySQL/Redis/MongoDB calls, operators can jump straight from an alert metric to the exact failing downstream call without grepping dozens of pods.

See all domain details on the capabilities map, review our observability stack, or contact our engineers to schedule an NDA architecture walkthrough.

Frequently Asked Questions

Why isolate risk and forced liquidation from the matching hot path?

During volatile markets, liquidation waves spike CPU and ledger work. Keeping liquidation computation and margin checks in a dedicated domain keeps live order books deterministic instead of stalling order flow.

How does a single-writer matching engine recover after a pod restart?

State changes follow deterministic ordered events paired with snapshots and persistent trade logs, allowing a restarted market writer to replay cleanly from its committed offset without duplicating fills.

How are uncertain (UNKNOWN) withdrawal or transfer results handled?

Network or provider timeouts never trigger blind retries that risk double payouts. Uncertain writes remain locked as UNKNOWN and converge strictly through idempotent query and reconciliation flows.

Can custody and matching run independently on an existing exchange?

Yes. Both domains communicate via versioned Protobuf contracts and ordered events, so operators can adopt either domain modularly without sharing database tables.