When building a real-time trading system for Polymarket, I assumed strategy and execution would be the hardest parts.
I was wrong.
The real issue was state drift – the system slowly stopped matching reality.
- What state drift looked like
- Incorrect positions
- Duplicate exposure
- Mismatch between bot state and real market state
There was no crash, only silent inconsistency.
- Why it happens
- WebSocket missing updates
- Delayed API responses
- Partial order fills
- Duplicate events
- Missing events
-
In-memory state assumptions
-
Core problem
The system trusted runtime memory instead of reality. -
Solution: Event Sourcing
Store events instead of state:
- trade events
- order events
- fill events
State is always rebuilt from history.
- Architecture
Event Log → State Rebuild → Current State
This makes the system deterministic and replayable.
- Reconciliation layer
Periodically:
- fetch real positions
- compare with internal state
- correct drift
- Result
- No silent inconsistencies
- Easier debugging
- System can rebuild from scratch
-
Reliable state tracking
-
Key lesson
State in trading systems is not stored – it is reconstructed.