This project implements a high-performance cryptocurrency exchange system focusing on scalability, concurrency, and real-time data updates. The architecture leverages modern technologies like Redis, TimescaleDB, and WebSockets to deliver efficient trading capabilities.
We welcome contributions from all developers and would love to connect with like-minded individuals who are passionate about cryptocurrency, high-performance systems, and real-time data.
The diagram illustrating the high-level architecture of the system.
The system is divided into the following services:
- API: Handles HTTP requests from users.
- Engine: Maintains market orderbooks and user balances (stored in memory for high-speed access).
- WebSocket: Streams real-time events to clients.
- DB Processor: Processes messages from the Engine and persists them into the database.
- Frontend: A Next.js application that provides the user interface.
- Market Maker (MM): Generates random orders to maintain market liquidity.
- Redis: Used for queueing and Pub/Sub operations.
- TimescaleDB: Manages time-series data and creates Kline data buckets for analytics.
- PostgreSQL is suggested as the primary database to store critical data such as user information, orders, trades, etc.
The system supports the following WebSocket streams:
- Trades:
trades@MARKET
- Orderbook Depth:
depth@MARKET
- Ticker Updates:
ticker@MARKET
The workflow ensures that all operations are sequential rather than concurrent, guaranteeing data consistency and preventing race conditions.
A single engine is responsible for maintaining the orderbook and user balances, ensuring data integrity throughout the system.
- Time series data stores trade and order events, which are later transformed into Kline (Candlestick) data for analytics.
- A locking mechanism ensures secure operations, such as fund creation and updates, preventing inconsistent states in concurrent environments.
This function ensures that funds are locked securely during critical operations. It uses precise technical and mathematical reasoning to prevent race conditions and inconsistent states.
Follow the instructions below to set up the project locally.
-
Navigate to the
docker
directory:cd docker
-
Start the services:
docker-compose up
-
If PostgreSQL is running, disable and stop it:
sudo systemctl disable postgresql sudo systemctl stop postgresql
-
API Server:
cd api npm install npm run dev
-
Engine:
cd engine npm install npm run dev
-
WebSocket Server:
cd ws npm install npm run dev
-
Frontend:
cd frontend npm install npm run dev
-
market makers:
cd mm npm install npm run dev
- Orderbook and User Balances: These are stored in memory for fast access and processing.
- Locking Balances: Ensures secure fund management during operations like placing orders or withdrawals.
-
In-Memory Operations: Critical data (like balances and orderbooks) is stored in memory for high performance. The DB Processor ensures eventual consistency by persisting this data into TimescaleDB and PostgreSQL.
-
Sequential Workflow: Despite the system's concurrency at the service level, individual operations follow a sequential workflow to ensure reliability and avoid data inconsistency.
We encourage you to contribute to this project! If you find any issues, have suggestions for improvement, or want to add new features, feel free to open a pull request. Your contributions are always welcome.
We'd love to connect with developers who are passionate about building scalable, real-time systems. Let's grow this project together!
- If you encounter issues with database connections or WebSocket streaming, ensure that all services are running and Docker is correctly configured.
- For issues with the API server or engine, check the logs for detailed error messages.