Design a high-concurrency distributed counter system that supports incr(key, delta) and get(key) operations with the following requirements: 1. Accurate counting under high concurrency; 2. Support distributed deployment; 3. Eventual consistency is acceptable; 4. Minimize single-point pressure. Please explain the core design ideas, potential issues, and solutions.
分类: technical
难度: medium
标签:
答题技巧
Common approaches: 1. Redis INCR (simple but hotspot issue); 2. Sharding + local aggregation + periodic sync; 3. Database + message queue async write; 4. Consistent hashing + multiple replicas. Key points: distributed consistency, hotspot key handling, trade-off between performance and accuracy.
参考答案
Use consistent hashing to shard keys. Deploy multiple stateless counter nodes per shard. Clients route via consistent hashing. Each node maintains in-memory count + async persistence to Redis/database. incr updates memory first, then batches to persistent layer. get returns current memory value + last synced value. For hotspot keys, use multi-replica + read diffusion. Bloom filter or pre-allocation can reduce invalid requests.