⚡ A simple distributed load balancer with consistent hashing and Redis-powered persistence for smart user-server mapping.
Mimics distributed system routing, supports server updates without full remapping.
loadbalancer/
├── src/ # Java source files
│ ├── Main.java # Entry point
│ ├── Router.java # Routes HTTP requests
│ ├── Server.java # TCP server logic
│ ├── ServerHandler.java# /addServer, /replaceServer endpoints
│ ├── DataHandler.java # /registerUser, /cachereport endpoints
│ └── Redis.java # Redis client setup via Redisson
├── Tester.sh # Simulate server/user operations
├── request_test.sh # Basic CURL testing
├── build.sh # Compile + run helper
├── client_requests.log # Logs requests
├── pom.xml # Maven config
└── README.md # This file
- Java 17+
- Maven
- Redis (local or Docker)
- Internet (to download dependencies)
You can install Redis or use Docker:
Option A: System Install
sudo apt update
sudo apt install redis-server
redis-serverOption B: Using Docker
docker run -d -p 6379:6379 --name redis-server redischmod +x build.sh
./build.sh
# Server starts on localhost:4000chmod +x Tester.sh
./Tester.sh
# Adds 3 servers, registers users, replaces a server, prints Redis cache reports, saves logs to report-x.txt- ServerCache: HashMap of
<userHash, ServerNode> - ServerList: Redis list of all active servers (for recovery after restart)
| Method | Endpoint | Description |
|---|---|---|
| POST | /addServer | Add a new backend server |
| POST | /replaceServer | Replace an existing server |
| POST | /registerUser | Assign user to a server |
| GET | /cachereport | Get all user-to-server mappings |
Use
userIdin header when calling/registerUser
# Add a server
curl -X POST http://localhost:4000/addServer -H "Content-Type: text/plain" \
-d "127.0.0.1:3001,Server1,user1,pass1"
# Register a user
curl -X POST http://localhost:4000/registerUser -H "userId: 101" -d ""
# View current cache report
curl http://localhost:4000/cachereport- 🗃️ Redis stores all mappings persistently.
- On server restart, the consistent hashing ring is rehydrated from Redis
ServerList. - Ensures user mappings remain stable across server crashes and restarts.
- Logs are printed to the console
- Each test run using
Tester.shgenerates areport-x.txtunder the root directory - Use
redis-clito view actual Redis data:
redis-cli
> HGETALL ServerCache
> LRANGE ServerList 0 -1- RedisInsight (GUI for Redis)
- Postman or HTTPie for API testing
- ✅ Consistent Hashing
- ✅ Fault-tolerant Redis mapping
- ✅ Server-aware routing
- ✅ Persistent recovery without database
MIT – Feel free to build on this foundation.
Prem Sai – Backend & Distributed Systems Enthusiast
💡 For any questions, improvements or collabs – feel free to reach out!