A production-grade, scalable real-time leaderboard system built with Go, gRPC, Redis, and Kafka. This project demonstrates best practices in distributed systems design, including clean architecture, JWT authentication, event-driven architecture, and comprehensive testing.
I have designed the system to be modular and event-driven. Here is the high-level flow:
- Clients connect via high-performance gRPC.
- Auth Middleware secures endpoints using JWT tokens.
- The Service Layer orchestrates business logic.
- Redis powers the leaderboards for fast reads and writes.
- Kafka streams score updates asynchronously to other services (like Monitor service).
┌─────────────┐
│ Client │
└──────┬──────┘
│ gRPC
▼
┌─────────────────────────────────────┐
│ gRPC Server │
│ ┌───────────────────────────────┐ │
│ │ Auth Middleware (JWT) │ │
│ └───────────┬───────────────────┘ │
│ ▼ |
│ ┌───────────────────────────────┐ │
│ │ Service Layer │ │
│ └─────┬────────────────┬────────┘ │
│ │ │ │
│ │ (Fast Write) │ (Event) │
│ ▼ ▼ │
│ ┌────────┐ ┌──────────┐ │
│ │ Redis │ │ Kafka │ │
│ └────────┘ └─────┬────┘ │
└──────────────────────────┼──────────┘
│
▼
┌──────────────┐
│ Monitor │
│ Service │
└──────────────┘
- Go 1.23+: High-performance backend language
- gRPC: Efficient RPC framework for API communication
- Redis: In-memory data store for leaderboards and user data
- Kafka: Distributed event streaming for real-time updates
- JWT: Secure token-based authentication
- Docker: Containerization and orchestration
- Protocol Buffers: Efficient data serialization
Ready to dive in? You can have the full system running locally in just a few minutes.
- Go 1.23 or higher
- Docker & Docker Compose
protoc(Protocol Buffers compiler)
The easiest way to run the system is using the provided Makefile commands which handle Docker orchestration for you.
-
Clone the repo:
git clone https://github.com/DevanshTomar/real-time-scoreboard.git cd real-time-scoreboard -
Set up dependencies:
make setup
-
Start all services with Docker
make docker-up
-
Run the demo client
make run-client
If you prefer running services individually (great for debugging):
- Start Infrastructure:
docker-compose up -d redis kafka zookeeper - Build & Run Server:
make run-server - Run Monitor:
make run-monitor(in a new terminal) - Run Client:
make run-client(in a new terminal)
I didn't just build a scoreboard; I built a system ready for the real world.
- ✅ Secure by Default: JWT authentication and bcrypt password hashing ensure user data is safe.
- ✅ Real-Time Performance: Redis Sorted Sets allow it to update rankings instantly, even with millions of users.
- ✅ Event-Driven: Kafka integration means the leaderboard service updates can trigger achievements, push notifications, or analytics pipelines without slowing down the main API.
- ✅ Clean Code: The project follows strict Clean Architecture principles, making it testable and maintainable.
- ✅ Observability: A dedicated Monitor service logs events as they happen, giving you visibility into the system.
The system exposes a gRPC API. The main interactions are:
- Register: Create a new account.
- Login: Exchange credentials for a JWT.
- SubmitScore: Post a new score. The system automatically updates your high score and global rank.
- GetGlobalLeaderboard: See who's on top across all games.
- GetGameLeaderboard: Filter rankings by specific game.
- GetUserRank: Check your own standing.
(See proto/leaderboard.proto for the exact service definitions.)
- Run All Tests:
make test - Integration Tests:
make test-integration(Requires Docker containers to be running) - Check Coverage:
go test -cover ./...
Everything is configurable via environment variables. Check out .env.example.
Common settings:
GRPC_PORT: Port for the main server (Default:50051)REDIS_ADDR: Connection string for Redis.KAFKA_BROKERS: Kafka broker list.
| Variable | Description | Default |
|---|---|---|
GRPC_PORT |
gRPC server port | 50051 |
REDIS_ADDR |
Redis address | localhost:6379 |
REDIS_POOL_SIZE |
Connection pool size | 10 |
KAFKA_BROKERS |
Kafka broker addresses | localhost:9092 |
KAFKA_TOPIC |
Event topic name | leaderboard-updates |
JWT_SECRET |
JWT signing secret | (must be set) |
JWT_EXPIRATION_HOURS |
Token expiration | 24 |
The monitor service consumes Kafka events and logs them in real-time. View logs with:
docker-compose logs -f monitorExample output:
{
"time": "2024-01-15T10:30:45Z",
"level": "INFO",
"msg": "score update received",
"user_id": "abc123",
"username": "alice",
"game_id": "game1",
"score": 100,
"timestamp": "2024-01-15T10:30:45Z"
}To test the system with concurrent users, run:
make run-loadtestThis will stress test the system with 500 concurrent users submitting scores.
- "Server won't start": Check if Redis and Kafka are running (
docker-compose ps). Ensure port50051isn't taken. - "Tests are failing": Integration tests need real infrastructure. Make sure you ran
docker-compose upbefore testing. - "gRPC Connection Refused": Verify the server is up and your firewall isn't blocking the connection.