System architecture
Opinix Trade is a real-time opinion trading platform built as a monorepo using Turborepo. The architecture is designed to handle high-frequency order processing with asynchronous workflows and real-time updates.The system uses a queue-based architecture to handle order matching asynchronously, ensuring the main API remains responsive even under heavy load.
Core components
The platform consists of five main components working together:Client (Next.js)
Frontend application where users place orders and receive real-time updates via WebSocket connections.
Backend API (Express)
RESTful API server that validates orders and pushes them to the Redis queue for async processing.
Queue (Redis)
Message broker that stores pending orders and enables async communication between services.
Worker (Engine)
Background worker that processes orders from the queue using the matching engine to execute trades.
Technology stack
Frontend
- Next.js 14 (App Router)
- TypeScript
- Server Actions
Backend
- Express.js
- Node.js
- TypeScript
Real-time
- WebSocket (ws)
- Redis Pub/Sub
- Subscription Manager
Infrastructure
- Redis (Queue & Cache)
- PostgreSQL (Database)
- Turborepo (Monorepo)
Data flow
Order placement flow
Order placement flow
- User submits an order through the Next.js client
- Frontend calls the backend API
/order/initiateendpoint - Backend validates the order and pushes it to the
ORDER_QUEUEin Redis - Backend immediately responds to the client with “Order placed successfully”
- Worker polls the queue and processes the order using the Engine
- Engine executes the matching algorithm and updates balances
- Engine publishes updates to Redis channels for WebSocket distribution
- WebSocket server broadcasts updates to all subscribed clients
Real-time updates flow
Real-time updates flow
- Clients subscribe to specific channels (e.g.,
depth@bitcoin-event,trade@bitcoin-event) - Engine publishes messages to Redis channels after order execution
- WebSocket server’s SubscriptionManager receives messages via Redis Pub/Sub
- WebSocket server broadcasts messages only to clients subscribed to that channel
- Clients receive updates and re-render the order book UI
Monorepo structure
The project uses Turborepo with the following workspace organization:All packages are written in TypeScript and share types from
@opinix/types to ensure type safety across the entire system.Key architectural decisions
Async order processing
Orders are processed asynchronously using a Redis queue to decouple the API from the computationally expensive matching logic. This ensures:- Fast API responses: The API responds immediately without waiting for order execution
- Scalability: Multiple workers can process orders in parallel
- Reliability: Orders are persisted in Redis and won’t be lost if a worker crashes