Back to Home
Reading:Realtime Market Lab
Overview
This is a simulator project, not a trading product. A Python MarketSimulator fakes an entire exchange so the frontend is always the bottleneck under test. Push the Simulation Workload sliders to 5,000–10,000 instruments at 5,000+ updates/sec and compare rendering strategies side-by-side.
Key focus areas:
- Simulator heartbeat that scales to 10k symbols without choking
- Server coalescing + 50ms batched frames
- rAF-aligned drains + 1 commit per flush
- Selective subscriptions + virtualization + Worker offload
Simulator — How Fake Counts Are Generated
backend/app/simulator.py uses one heartbeat loop, not one task per instrument:
- Wake every 20ms (
TICK_SECONDS = 0.02), addupdates_per_sec * 0.02to fractional credit (_owed) - Convert credit to integer
n_eventsowed this tick — O(events/tick), not O(instruments) - Sample
random.choice(instruments)that many times, group byburst_sizeto fake bursty fills - Mutate via
_mutate(inst): price jitter±volatility(default 0.06%), update high/low/volume, emit by mix — quote 70% / trade 20% / book 10% put_nowaitto boundedasyncio.Queue; onQueueFulldrop, never block heartbeat — producer-side backpressure
Instruments are synthetic (TECH00001 …): random base $5–$2,000, spread 1–8 bps. Quotes carry bid/ask + change%, trades carry qty/side, books carry 5 bids + 5 asks. Same normalized schema as the live Binance adapter, so frontend code never changes.
Impact & Results
Features
Technical Challenges
Event Flow
Performance Techniques Used
Performance Optimizations
Lessons Learned
- Batching: Server coalescing + client rAF drain beats per-message setState by an order of magnitude.
- Isolation: Separate market / config / metrics stores prevents perf-panel re-renders from polluting measurements.
- Virtualization: DOM cost must be viewport-bound, not data-bound.
- Workers: Even fast sorts hurt at 5k/sec — move them off-thread.
Try It
frontend/
├── app/ Next.js App Router entry
├── components/ Dashboard panels
├── hooks/ useMarketConnection, usePerfMetrics
├── lib/ marketStore, configStore, metricsStore, wsClient
└── workers/
└── stats.worker.ts Top-movers ranking off main threadLiked this project?
Check out more of my work or get in touch.