Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← System Design Fundamentals

Scalability Fundamentals

  • Horizontal vs Vertical Scaling
  • Load Balancing
  • Caching Strategies

Databases at Scale

  • CAP Theorem
  • Database Sharding
  • Replication & Consistency
  • Consistent Hashing
Chaturmind
← System Design Fundamentals

Scalability Fundamentals

  • Horizontal vs Vertical Scaling
  • Load Balancing
  • Caching Strategies

Databases at Scale

  • CAP Theorem
  • Database Sharding
  • Replication & Consistency
  • Consistent Hashing
HomeLearnSystem DesignSystem Design FundamentalsDatabase Scaling
✓ FreeAdvanced· 12 min read

Consistent Hashing

Implement consistent hashing with virtual nodes to distribute data evenly and minimize resharding.

Published April 18, 2025


Consistent Hashing

Consistent hashing distributes data across nodes such that when nodes are added or removed, only a minimal fraction of keys need to be remapped. It's used in distributed caches (Redis Cluster), CDNs, and database sharding.

The Problem with Simple Hash

// Simple modulo hashing
int shard = hash(key) % N;

// Adding a server (N becomes N+1):
// hash(key) % 3 vs hash(key) % 4 — almost ALL keys remap!
// This causes a thundering herd on the new node

Consistent Hashing — the solution

Map both servers and keys onto a circular ring (0 to 2³²). Each key is served by the first server encountered going clockwise.

         0
        / \
   S_A /   \ S_B
      /     \
    2^32   2^10
      \     /
   S_C \   /
        \ /
        2^31

Key hash at 2^5 → next server clockwise = S_B
class ConsistentHash {
    private final TreeMap<Long, String> ring = new TreeMap<>();
    private final int virtualNodes;

    public ConsistentHash(List<String> servers, int virtualNodes) {
        this.virtualNodes = virtualNodes;
        for (String server : servers) addServer(server);
    }

    public void addServer(String server) {
        for (int i = 0; i < virtualNodes; i++) {
            long hash = hash(server + "#" + i);
            ring.put(hash, server);
        }
    }

    public void removeServer(String server) {
        for (int i = 0; i < virtualNodes; i++)
            ring.remove(hash(server + "#" + i));
    }

    public String getServer(String key) {
        if (ring.isEmpty()) return null;
        long hash = hash(key);
        Map.Entry<Long, String> entry = ring.ceilingEntry(hash); // first server >= hash
        if (entry == null) entry = ring.firstEntry(); // wrap around
        return entry.getValue();
    }

    private long hash(String key) {
        // MurmurHash or similar 32/64-bit hash
        return Math.abs(key.hashCode()) % Long.MAX_VALUE;
    }
}

Virtual Nodes — solving uneven distribution

With few servers, the ring is unevenly distributed. Virtual nodes map each physical server to multiple ring positions, achieving more uniform distribution.

Without virtual nodes (3 servers):
  S_A: 0-30%, S_B: 30-80%, S_C: 80-100% ← very uneven!

With 100 virtual nodes per server (300 positions on ring):
  S_A: ~33%, S_B: ~33%, S_C: ~33% ← much more even

Adding a Server — minimal resharding

Before: S_A, S_B, S_C
Keys between S_A and S_B → served by S_B

Add S_D between S_A and S_B:
Keys between S_A and S_D → move from S_B to S_D
All other keys: unchanged!

Total keys moved = 1/(N+1) of total keys

Real-World Usage

  • Redis Cluster: 16384 hash slots distributed with consistent hashing
  • Amazon DynamoDB: consistent hashing for partition key distribution
  • Apache Cassandra: token-based ring partitioning
  • CDN: route requests to nearest cache server

Interview Tips

  1. When asked about sharding, consistent hashing is almost always the right answer — it minimizes resharding cost.
  2. Always mention virtual nodes — without them, consistent hashing has hot spots.
  3. Draw the ring during the interview — it communicates the concept far better than words alone.

Previous

Replication & Consistency

AI Tutor

Lesson: Consistent Hashing

Quick actions

AI responses can be inaccurate. Verify critical information.