← All Projects
SYSTEMS · 2024 · 10★ · Rust Microservices · Redpanda + ClickHouse

Polymarket Distributed Microservices Engine

Rust-Based High-Throughput Prediction Market & Order Book Engine

End-to-end distributed prediction market exchange engineered with Rust microservices, gRPC inter-service communication, Redpanda streaming, NATS message queues, ClickHouse OLAP analytics, and Bloom filters for low-latency order matching and real-time execution.

Stack: Rust · Microservices · gRPC · Redpanda · ClickHouse · NATS Streaming · Redis · WebSockets · PostgreSQL

Polymarket Distributed Microservices Engine

Polymarket Distributed Microservices Engine is an end-to-end, high-throughput prediction market and order book exchange engineered in Rust. It integrates gRPC inter-service RPCs, Redpanda event streaming, NATS message queuing, ClickHouse OLAP analytics, and Bloom filters to achieve low-latency order matching and real-time trade settlement.


1. System Overview & Architecture

Building a prediction market exchange requires reconciling high-frequency order placement with distributed consensus, deterministic order matching, and immutable audit logs. The system is split across decoupled Rust microservices communicating through gRPC for low-latency point-to-point calls and Redpanda (Kafka-compatible event streaming) for durable event distribution.

Loading diagram...

2. Key Architecture Components

High-Performance In-Memory Matching Engine

  • Central Limit Order Book (CLOB): Implemented using memory-contiguous structures and standard BTreeMap indexed by price and timestamp for strict Price-Time priority matching.
  • Zero-Allocation Deserialization: Leveraged zero-copy deserialization buffers for hot order paths, maximizing CPU cache locality.
  • Deduplication with Bloom Filters: Sub-microsecond detection of repeated order IDs to prevent replay attacks and duplicate fills before hitting the storage layer.

Durable Event Streaming via Redpanda

  • All validated orders are published to an append-only Redpanda topic (market.orders).
  • Matches emit dual events (trade.executed and order.updated), guaranteeing that state can be replayed from genesis to reconstruct exact order book state at any tick.

ClickHouse OLAP Analytics & Candle Aggregation

  • Massive trade volume ingestion with columnar compression.
  • Materialized views in ClickHouse compute 1m, 5m, 1h, and 1d candlestick intervals, open-interest ratios, and volume distributions on-the-fly without overloading operational Postgres databases.

Low-Latency Real-Time Feeds

  • NATS message queues broadcast trade fills, depth-of-book updates, and price changes to connected WebSockets clients within sub-10ms delivery windows.

3. Technology Stack

  • Core Language: Rust (Tokio asynchronous runtime, Rayon, Tonic gRPC, Prost)
  • Messaging & Streaming: Redpanda (Kafka protocol), NATS Streaming
  • Data Stores: PostgreSQL (account balances & user states), ClickHouse (OLAP time-series trades), Redis (session caching & rate-limiting)
  • Transport Protocols: gRPC / Protocol Buffers, WebSockets, HTTP/2
  • Deployment & Observability: Docker Compose, Prometheus metrics, OpenTelemetry structured tracing

4. Key Engineering Achievements

  • Achieved sub-millisecond order matching latency in simulated high-frequency trading workloads.
  • Eliminated state desynchronization by strictly separating event sequencing (Redpanda) from deterministic matching logic.
  • Built horizontal scalability for market feeds using NATS fanout to push real-time order book diffs to thousands of concurrent WebSocket subscribers.