A high-performance, scalable distributed counter system built in Java that distributes a single counter's value across multiple shards for massive throughput and fault tolerance.
- High Performance: 100x+ throughput improvement over traditional databases
- Fault Tolerant: No single point of failure
- Horizontally Scalable: Add more shards for unlimited growth
- Deterministic Routing: Consistent hashing for predictable performance
- Persistent Storage: RocksDB for durability and fast recovery
- In-Memory Caching: Hot data in memory for maximum speed
- HTTP API: RESTful interface for easy integration
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Coordinator β β Coordinator β β Coordinator β
β (Routing & β β (Routing & β β (Routing & β
β Aggregation) β β Aggregation) β β Aggregation) β
βββββββββββ¬ββββββββ βββββββββββ¬ββββββββ βββββββββββ¬ββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Shard Node β β Shard Node β β Shard Node β
β (Storage & β β (Storage & β β (Storage & β
β Processing) β β Processing) β β Processing) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
| Aspect | Traditional DB | Distributed Sharded Counter |
|---|---|---|
| Write Throughput | ~1K writes/sec | ~100K+ writes/sec |
| Read Throughput | ~10K reads/sec | ~50K+ reads/sec |
| Scalability | Vertical only | Horizontal scaling |
| Fault Tolerance | Single point of failure | Multiple nodes |
| Lock Contention | High (single counter) | None (distributed) |
- Java 11 or higher
- Gradle 7.0 or higher (or use the included wrapper)
-
Clone the repository
git clone https://github.com/yourusername/DistributedCounter.git cd DistributedCounter -
Build the project
./gradlew build
-
Run the demo
./sharded_demo.sh
# Start shard nodes on different ports
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardNode 8081 ./data/shard1
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardNode 8082 ./data/shard2
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardNode 8083 ./data/shard3# Start coordinator with shard addresses
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardedCounterCoordinator 8080 localhost:8081 localhost:8082 localhost:8083# Run the client demo
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.client.ShardedCounterClient http://localhost:8080curl -X POST http://localhost:8080/sharded \
-H "Content-Type: application/json" \
-d '{
"counterId": "global_likes",
"operationType": "INCREMENT",
"delta": 5
}'curl -X POST http://localhost:8080/sharded \
-H "Content-Type: application/json" \
-d '{
"counterId": "global_likes",
"operationType": "DECREMENT",
"delta": 2
}'curl -X POST http://localhost:8080/sharded \
-H "Content-Type: application/json" \
-d '{
"counterId": "global_likes",
"operationType": "GET_TOTAL"
}'curl -X POST http://localhost:8080/sharded \
-H "Content-Type: application/json" \
-d '{
"counterId": "global_likes",
"operationType": "GET_SHARD_VALUES"
}'- β High-traffic counters (social media likes, views)
- β Real-time analytics (IoT, monitoring)
- β E-commerce metrics (product views, purchases)
- β Gaming leaderboards (scores, achievements)
- β High-availability requirements (99.9%+ uptime)
- β Simple applications (low traffic)
- β Strong consistency required (financial transactions)
- β Limited resources (cannot afford multiple servers)
- β Simple architecture preferred (minimal complexity)
# Port: HTTP server port
# Data Directory: RocksDB storage location
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardNode <port> <data_directory># Port: HTTP server port
# Shard Addresses: List of shard node addresses
java -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardedCounterCoordinator <port> <shard1> <shard2> <shard3>- Deterministic routing using consistent hashing
- In-memory operations for maximum speed
- Asynchronous persistence to RocksDB
- No lock contention between shards
- Aggregation from all shards for total count
- Parallel queries to all shards
- Cached responses for frequently accessed data
- Fault tolerance with automatic failover
- Horizontal scaling by adding more shards
- Independent scaling of reads and writes
- Load distribution across multiple nodes
- Unlimited growth potential
DistributedCounter/
βββ src/
β βββ main/java/com/distributedcounter/
β β βββ ShardNode.java # Shard node implementation
β β βββ ShardedCounterCoordinator.java # Coordinator implementation
β β βββ client/
β β β βββ ShardedCounterClient.java # Client library
β β βββ hashing/
β β β βββ ConsistentHash.java # Consistent hashing
β β βββ model/
β β β βββ ShardedCounterOperation.java
β β β βββ ShardedCounterResponse.java
β β βββ replication/
β β β βββ ReplicationManager.java # Read replica support
β β β βββ ReplicationClient.java
β β βββ storage/
β β βββ RocksDBStorage.java # Persistence layer
β βββ test/java/com/distributedcounter/
β β βββ ConsistentHashTest.java
β β βββ RocksDBStorageTest.java
β βββ resources/
β βββ logback.xml # Logging configuration
βββ docs/
β βββ distributed_sharded_counter.md # Architecture documentation
βββ build.gradle # Build configuration
βββ gradle.properties # Gradle properties
βββ gradlew # Gradle wrapper
βββ sharded_demo.sh # Demo script
βββ README.md # This file
./gradlew test./gradlew test --tests ConsistentHashTestcurl http://localhost:8080/healthcurl http://localhost:8081/health
curl http://localhost:8082/health
curl http://localhost:8083/health-
Port already in use
# Check what's using the port lsof -i :8080 # Kill the process or use a different port
-
RocksDB permission errors
# Ensure data directory is writable chmod 755 ./data -
Out of memory errors
# Increase JVM heap size java -Xmx2g -cp build/libs/DistributedCounter-1.0.0.jar com.distributedcounter.ShardNode 8081 ./data/shard1
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- RocksDB: High-performance key-value store
- Netty: Asynchronous event-driven network framework
- Consistent Hashing: For deterministic routing
- Gradle: Build automation tool
For questions, issues, or contributions:
- Create an issue on GitHub
- Check the documentation in
docs/ - Review the test cases for usage examples
Built with β€οΈ for high-performance distributed systems