This project simulates a cryptocurrency exchange with a modular design for scalability and real-time updates. It focuses on the backend architecture and core exchange logic.
-
Frontend (Client):
- Web interface for users to place orders and view market data.
- Communicates with the API Server using HTTP for order placement and WebSockets (or polling) for real-time updates.
-
API Server (Node.js with Express.js):
- Handles incoming HTTP requests from the frontend for order placement.
- Performs request validation and authorization.
- Routes order placement requests to the Engine via a Redis queue.
-
Engine (Node.js):
- Core logic of the exchange.
- Manages the order book .
- Implements the order matching algorithm .
- Executes the pseudo market maker logic (placing buy and sell orders).
- Publishes trade confirmations and order book updates to Redis pub/sub channels.
- Sends trade data to the TimescaleDB via a Redis queue and a Database Processor
-
WebSocket Server (Socket.io or ws - Optional):
- Handles real-time communication with connected clients.
- Subscribes to relevant Redis pub/sub channels.
- Forwards messages to connected clients, providing real-time market data updates.
-
Redis:
- In-memory data store used for asynchronous communication and message queuing.
- Used for:
- API Server to Engine communication (using Redis queues).
- Engine to WebSocket Server communication (using Redis pub/sub).
- Engine to TimescaleDB communication (using Redis queues).
-
TimescaleDB:
- Time series database used to store trade data.
-
Database Processor:
- Takes data from redis queue and sends it to timescaleDB.
graph LR
subgraph Browser
A[Frontend]
end
A -- POST /api/order --> B{API}
B -- Redis Queue --> C{Engine}
C -- Redis PubSub --> D{Websocket}
C -- Redis Queue --> E{Database Processor}
E --> F[Timescale DB]
D --> A
Open a terminal and navigate to the root directory of your project.
Ensure your docker-compose.yml
file is in the appropriate directory. Use the following commands:
cd /docker # Adjust this path if your docker-compose.yml file is located elsewhere
docker-compose up -d # Starts all services in detached mode
It's important to install dependencies and start services in a specific order.
cd api
npm install
npm run dev
cd .. # Move back to project root
This installs dependencies and starts the API service in development mode.
cd engine
npm install
npm run dev
cd .. # Move back to project root
Similar to the API service, this installs dependencies and starts the engine service.
cd ws
npm install
npm run dev
cd .. # Move back to project root
Installs dependencies and starts the WebSocket service.
cd frontend
npm install
npm run dev
cd .. # Move back to project root
Installs dependencies and starts the frontend service.
cd marketmaker
npm install
npm run dev
cd .. # Move back to project root
Installs dependencies and starts the marketmaker service.
After completing the above steps, your entire application stack should be running in separate Docker containers. Access the application using the ports defined in your docker-compose.yml
file.
cd /docker # Adjust this path if necessary
docker-compose down
cd /docker # Adjust this path if necessary
docker-compose up -d
- Paths: Update the paths in the commands if your project structure or the location of your
docker-compose.yml
file differs. - Environment Variables: Ensure all required environment variables are set before starting the services.
project-root/
├── docker/
│ └── docker-compose.yml
├── api/
├── engine/
├── ws/
├── frontend/
└── marketmaker/
- Container Not Starting: Check logs using
docker-compose logs
. - Dependency Issues: Ensure all
npm install
commands complete without errors.