System Design: Crypto Trading Gateway via Logically Asynchronous, Synchronous APIs
Context
You are designing a trading gateway that brokers client orders to multiple third-party crypto exchanges. The external exchanges expose synchronous HTTP endpoints for order submission/cancel (request–response), but trade execution and order updates are delivered asynchronously (e.g., via WebSocket streams or polling). The system must present a clean client-facing API, ensure correctness under failures, and scale through volatile, spiky market conditions.
Requirements
Design the system and cover:
-
High-level architecture and request flow for placing and canceling orders using a synchronous client API while handling asynchronous downstream behavior.
-
A complete order state machine and state-transition diagram (e.g., NEW → PARTIALLY_FILLED → FILLED / CANCELED / EXPIRED), including intermediate and failure states.
-
How to detect and recover from third-party exchange timeouts or failures, including idempotency, retries, reconciliation, and circuit breaking.
-
Strategies to handle spiky traffic and protect downstream dependencies (queueing, autoscaling, back-pressure, prioritization, rate limiting).