Live URL: 👉 https://order-engine.netlify.app/
This project implements a high-throughput order execution engine tailored for the Solana ecosystem, focusing on:
- Intelligent decentralized exchange (DEX) routing
- Real-time order lifecycle updates via WebSockets
- Scalable & fault-tolerant processing using distributed workers
The engine emulates core components of real trading systems used in modern DEX platforms.
The goal was to build a robust, scalable, and fault-tolerant execution engine that can:
- Process a large number of parallel trading orders
- Intelligently route each order to the best execution venue (Raydium or Meteora)
- Ensure each user receives instant, live status updates through a dedicated WebSocket channel
- Maintain transparent, predictable execution state across Redis, Workers, and PostgreSQL
Below is the high-level architecture used to achieve this:
This engine implements three essential trading order types:
- Market Orders — Execute immediately at the best available price
- Limit Orders — Execute only when the target price condition is reached
- Sniper Orders — Execute instantly when a token becomes available or a liquidity condition is met
The system was incrementally designed:
- Market Orders (base pipeline)
- Limit Orders (Redis buckets + conditional triggers)
- Sniper Orders (liquidity/availability triggers)
Each order type runs on its own dedicated worker, ensuring scalability and clean separation of concerns.
To focus deeply on architecture, reliability, and real-time behavior, the engine uses a Mock Execution Mode:
- DEX quotes returned with realistic delays (2–3 seconds)
- Prices vary randomly by 2–5%
- Execution simulates real settlement with slippage, routing, and confirmation stages
This allowed validating:
- Queue behavior
- Worker concurrency
- WebSocket streaming
- Redis caching
- Failover & fallback logic
without requiring on-chain execution.
-
Order Submission User submits an order via:
POST /api/orders/execute -
Validation & WebSocket Upgrade API validates input → returns
orderId→ upgrades to WebSocket. -
DEX Routing System fetches quotes from mock Raydium & Meteora → selects best venue.
-
Execution Progress (Live Streaming) User receives real-time updates such as:
pendingroutingbuildingsubmittedconfirmedfailed
-
Settlement Simulation Engine simulates the transaction, slippage, final price, and mock
txHash.
| Technology | Purpose |
|---|---|
| Node.js + TypeScript | Strong, type-safe backend runtime ensuring reliability and maintainable code. |
| Fastify | Ultra-fast web framework with built-in WebSocket support for real-time updates. |
| BullMQ + Redis | Distributed job queues enabling high-throughput processing, retries, DLQs, and rate-limiting. |
| PostgreSQL + Prisma | Robust transactional database with type-safe ORM for persistent order storage. |
| Redis | High-speed in-memory cache for active orders, price feeds, and instant matching. |
| Pino | Production-grade structured logging with extremely low overhead. |
The engine is built using a microservice-like worker model, each running independently:
- API Server
- Order Execution Worker
- DBSync Worker
- Stream Worker
- Limit Worker
- Sniper Worker
This separation allows linear scaling by simply increasing worker replicas.
Each worker focuses on a single responsibility:
- Order Worker → Executes all market + triggered orders
- Limit Worker → Monitors price buckets & triggers executions
- Sniper Worker → Watches liquidity / token availability events
- Stream Worker → Fetches & caches Raydium/Meteora quotes
- DBSync Worker → Persists order results to PostgreSQL
This ensures reliability, throughput, and resilience.
Limit orders are stored in Redis using an O(1) lookup model:
limit_bucket:{pair}:{price} → Set(orderIds)
limit:{pair}:{orderId} → OrderDetails
When the Stream Worker updates the BEST price, the Limit Worker instantly checks bucket keys and dispatches all matching orders.
This design enables instant triggering for multiple orders at the same price, even under heavy load.
The engine continuously compares quotes from:
- Raydium
- Meteora
It calculates BEST = max(price - fee) and stores it in Redis as:
quotes:{tokenIn}-{tokenOut}:BEST
If live price streaming fails, the system automatically:
➡️ Falls back to cached quotes ➡️ Or fetches fresh quotes directly from the DEX router
This ensures uninterrupted execution reliability.
Each order establishes a dedicated WebSocket channel:
ws://server/ws/{orderId}
The user receives live updates as the order moves through:
pendingroutingbuildingsubmittedconfirmedfailed
Powered by BullMQ’s QueueEvents, ensuring immediate push updates.
The system uses:
- Exponential Backoff
- Jitter
- Auto-retries (≤3 attempts)
- Dead Letter Queue (DLQ) for failed jobs
- Stalled job recovery
- Job idempotency via jobId = orderId
The trading engine is designed using a highly decoupled, distributed microservices-style architecture, powered by BullMQ for resilient task orchestration and Redis for ultra-fast in-memory state management.
This architecture enables:
- High throughput
- Fault isolation
- Horizontal scalability
- Real-time feedback loops
-
🧑💻 Client Initiates orders and maintains a WebSocket connection for real-time execution updates.
-
🛣️ API Server (Fastify) Acts as the API gateway. Handles WebSocket upgrades, validates orders, stores early state, and dispatches jobs to workers.
-
⚙️ Workers (BullMQ) Individually responsible for routing, executing, syncing, and monitoring orders.
-
🗄️ PostgreSQL The source of truth for all completed, historical, or audited trades.
-
⚡ Redis Powers BullMQ queues and stores active order states, cached quotes, buckets, and transient metadata.
The Fastify server is the primary entry point of the system.
Provides production-grade logs with metadata, timestamps, and worker-context — essential for debugging high-throughput systems.
- Creates initial order entries in PostgreSQL (via Prisma)
- Stores immediate state in Redis for fast retrieval
- Dispatches jobs to BullMQ queues
Upon receiving /api/orders/execute:
- Validate input
- Create a preliminary DB record
- Push job → order queue
- Open WebSocket channel
- Return
orderIdinstantly
Streams every stage update:
pendingroutingbuildingsubmittedconfirmedfailed
The heart of the engine — responsible for executing all Market orders and final execution for triggered Limit/Sniper orders.
Uses orderId as jobId → ensures idempotency and prevents duplicates.
Ensures only one worker processes a given order job at a time.
- Retries with Exponential Backoff + Jitter
- Max 3 attempts
- Failed jobs → Dead Letter Queue (DLQ)
- Fetch BEST price
- Build mock DEX transaction
- Simulate slippage
- Mark Redis state
- Notify DBSync Worker
This worker guarantees correctness and integrity under load.
The data backbone for pricing, event broadcasting, and real-time routing.
Every 500ms, fetch simulated Raydium + Meteora quotes:
- Caches them in Redis as HASHES
- Computes and saves
BESTprice
Redis keys include:
quotes:{tokenIn}-{tokenOut}:RAYDIUM
quotes:{tokenIn}-{tokenOut}:METEORA
quotes:{tokenIn}-{tokenOut}:BEST
If API fetch fails → use last known cached quote.
Pushes live updates (from Order Worker) into the WebSocket layer.
Ensures database integrity by moving finalized orders from Redis → PostgreSQL.
Decoupling DB writes prevents:
- Slow disk I/O blocking real-time execution
- Queue congestion
- Backpressure issues
- Reads finalized order state from Redis
- Constructs a complete DB object
- Inserts into PostgreSQL (Prisma)
This ensures high throughput + strong consistency.
These workers handle logic that must run continuously or be event-triggered.
Uses Redis buckets for instant execution:
orderKey = limit:{pair}:{orderId}
bucketKey = limit_bucket:{pair}:{targetPrice}
- Watches BEST price
- If BEST ≥ targetPrice → move all orderIds in bucket → Order Worker queue
- Guarantees instant trigger under load
Designed for:
- Token launch snipes
- Liquidity events
- Volume spikes
It triggers an order the moment required conditions are met.
A rigorous testing workflow was implemented to ensure the reliability, correctness, and high-performance behavior of the engine.
Comprehensive tests were written to validate all critical subsystems:
Ensures the engine always selects the optimal DEX (Raydium/Meteora) based on quotes, liquidity, and timestamps.
Verified:
- Correct job distribution
- Concurrency safety
- Retry logic with Exponential Backoff + Jitter
- Automatic fallback to the Dead Letter Queue (DLQ) for failed jobs
Ensures every order stage (pending → routing → building → submitted → confirmed) is pushed to the connected client in real-time.
End-to-end scenarios for Market, Limit, and Sniper orders were validated using Postman/Insomnia.
This helped ensure:
- Input validation
- WebSocket status delivery
- Correct queue routing
- Accurate Redis + DB sync behavior
K6 tests were executed to benchmark throughput, concurrency, latency, and worker stability.
- 100 orders/min sustained
- ≤ 10 concurrent executions
Market, Limit, Sniper & WebSocket channels tested independently.
| Test Scenario | Goal |
|---|---|
| Market Order Throughput | Validate sustained market execution under load |
| Limit Order Throughput | Stress-test O(1) bucket model + trigger logic |
| Sniper Order Throughput | Validate instant execution when triggers fire |
| WebSocket Stability | Verify 1000+ status message broadcasts |
The entire system is deployed using Docker-based microservice separation, highlighting production standards and real-world engineering practices.
To ensure small and secure production images:
-
Builder stage compiles TypeScript → optimized JS
-
Runtime stage runs only the compiled dist (no dev dependencies)
-
Images include:
- API Server
- Order Worker
- Limit Worker
- Sniper Worker
- Stream Worker
- DBSync Worker
- PostgreSQL
- Redis
This demonstrates clean separation of concerns similar to a distributed microservices environment.
All Dockerized components were deployed to an AWS EC2 instance using Docker Compose.
- Hardened environment
- Multi-container orchestration
- Network isolation between services
- External access only for API & WebSocket server
- Persistent volumes for PostgreSQL & Redis
- Created a secure HTTPS tunnel to the EC2 backend using Ngrok.
- Exposed the EC2 HTTP server to a public HTTPS URL for easy access.
Using Pino structured logging, enabling:
- Timestamped event tracking
- Worker-by-worker logs
- Failure analysis
- Performance debugging
A minimal frontend was created to visualize real-time order updates.
Live URL: 👉 https://order-engine.netlify.app/
This UI demonstrates:
- WebSocket live stages
- Market/Limit/Sniper order flows
- End-to-end testing in the browser
Thank you for reviewing the Order Execution Engine.





