A production-style, real-time market data streaming server
Built with Go Β· WebSockets Β· Redis Pub/Sub Β· Binance Streams
WebSocket clients subscribe to crypto symbols and receive live market updates with automatic feed management, reconnection handling, metrics, and horizontal scalability.
|
|
ββββββββββββββββββββ
β Binance WS Feed β
βββββββββββ¬βββββββββ
β live trade stream
βΌ
ββββββββββββββββββββββββββββββ
β Binance Ingestion Service β
β β
β Β· Dynamic Feed Management β
β Β· Reconnection Logic β
βββββββββββββββ¬βββββββββββββββ
β publish
βΌ
Redis Pub/Sub Channel
β subscribe
βΌ
βββββββββββββββββββββββββββββ
β Redis Subscriber β
βββββββββββββββ¬ββββββββββββββ
β broadcast
βΌ
βββββββββββββββββββββββββββββ
β Hub β
β Β· Register / Unregister β
β Β· Broadcast Events β
β Β· Symbol Tracking β
ββββββββββ¬βββββββββββ¬ββββββββ
β β
βΌ βΌ
Client A Client B
BTCUSDT ETHUSDT
stock-sim/
βββ cmd/
β βββ main.go # Entry point
β
βββ internal/
β βββ domain/
β β βββ binance.go
β β βββ client.go
β β βββ feed_command.go
β β βββ market_event.go
β β βββ stock.go
β β βββ subscription.go
β β
β βββ hub/
β β βββ hub.go # Client registration & broadcasting
β β
β βββ market/
β β βββ binance.go # Binance WS ingestion
β β βββ subscriber.go # Redis subscriber
β β
β βββ metrics/
β β βββ metrics.go # Custom HTTP metrics
β β
β βββ redis/
β β βββ client.go # Redis client wrapper
β β
β βββ websocket/
β βββ pumps.go # Read/write pumps per client
β
βββ Dockerfile
βββ docker-compose.yml
βββ .env
βββ README.md
Client βββΊ WebSocket βββΊ Hub
β
SymbolCount == 1?
β
ββββΊ FeedCommand(subscribe) βββΊ Binance Feed
Binance βββΊ Ingestion Service βββΊ Redis Publish
β
Redis Subscribe
β
Hub
β
Subscribed Clients
| Environment | URL |
|---|---|
| Local | ws://localhost:8080/ws |
| Production | wss://your-domain/ws |
{
"type": "subscribe",
"symbol": ["BTCUSDT", "ETHUSDT"]
}{
"type": "unsubscribe",
"symbol": ["ETHUSDT"]
}{
"symbol": "BTCUSDT",
"price": 63542.12
}GET /metrics
Example Response:
{
"active_symbols": 2,
"binance_reconnects_total": 0,
"connected_clients": 5,
"messages_received_total": 1245,
"messages_sent_total": 9860,
"top_symbols": [
{ "symbol": "BTCUSDT", "count": 3 },
{ "symbol": "ETHUSDT", "count": 2 }
]
}The Stock Simulator is optimized for high concurrency and has been load-tested with 10,000 concurrent WebSocket clients to verify message delivery and system stability under high-throughput conditions.
- Concurrently Connected Clients: 10,000
- Subscription: Every client successfully subscribes to the live
BTCUSDTstream. - Heartbeat & Connection Management: Keeps connections active without drops, responding to server-initiated pings within the timeout threshold.
- Implementation: Driven by the custom load-test script in internal/test/load.go.
A visualization of the WebSocket load test running against the dashboard:
load_testing.mp4
| Variable | Default | Description |
|---|---|---|
PORT |
8080 |
HTTP/WS server port |
REDIS_ADDR |
localhost:6379 |
Redis connection address |
Render example:
REDIS_ADDR=<render-redis-host>:6379# Build the images
docker compose build
# Start services
docker compose upRedis and the Go server both start automatically via Docker Compose.
# Install dependencies
go mod tidy
# Start the server
go run ./cmd/main.go| Endpoint | URL |
|---|---|
| Server | http://localhost:8080 |
| Metrics | http://localhost:8080/metrics |
| WebSocket | ws://localhost:8080/ws |
If the Binance connection drops:
- Reconnect automatically
- Restore the WebSocket connection
- Re-subscribe to all active symbols
- Continue streaming β zero server restarts needed
On SIGINT / SIGTERM:
- Stop accepting new requests
- Cancel background goroutines
- Close WebSocket connections
- Close Redis connections
- Exit cleanly β
| Layer | Technology |
|---|---|
| πΉ Language | Go |
| π WebSockets | Gorilla WebSocket |
| π Market Data | Binance Streams |
| π¨ Message Broker | Redis Pub/Sub |
| β‘ Concurrency | Goroutines + Channels |
| π³ Containerization | Docker |
| βοΈ Deployment | Render |
| π Metrics | Custom HTTP Metrics |
Built to learn and demonstrate real-world event-driven backend architecture using Go.
β Star this repo if you found it helpful!