Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

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

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Low-Level Design & Design Patterns

OOP Fundamentals & SOLID Principles

  • Object-Oriented Design Refresher
  • Single Responsibility & Open/Closed
  • Liskov Substitution & Interface Segregation
  • Dependency Inversion

Creational Design Patterns

  • Singleton Pattern
  • Factory & Abstract Factory
  • Builder Pattern
  • Prototype Pattern

Structural Design Patterns

  • Adapter & Facade
  • Decorator Pattern
  • Composite & Proxy
  • Bridge & Flyweight

Behavioral Design Patterns

  • Strategy Pattern
  • Observer Pattern
  • Command Pattern
  • Template Method
  • State Pattern
  • Chain of Responsibility
  • Iterator & Mediator
  • Visitor Pattern

Machine Coding Exercises

  • Parking Lot — Requirements & Class Design
  • Parking Lot — Implementation
  • Elevator System — Requirements & Class Design
  • Elevator System — Implementation & Scheduling
  • Library Management System
  • Tic-Tac-Toe / Board Game Design
  • Chess Engine Design
  • Vending Machine Design
  • ATM System Design
  • In-Memory Rate Limiter
  • Thread-Safe LRU Cache
  • Connection Pool Design
  • Producer-Consumer Class Design
  • Movie Ticket Booking System
  • Hotel Reservation System
  • Airline Booking / Seat Selection System
  • Splitwise / Expense Sharing System
  • Design a Logging Framework
  • Design a Notification/Observer-Based Pub-Sub
  • Design a Cache with Pluggable Eviction Policy
  • Design Snake and Ladder
  • Design a Card Game Framework
  • Design an Inventory Management System
  • Restaurant Management System
  • Food Delivery Order Matching
  • Design a Shopping Cart & Checkout Flow
  • Design a Recommendation Engine
  • Design a Job Scheduler
  • Design a Circuit Breaker
  • Design a Distributed ID Generator
  • Design a Pub-Sub Message Broker
  • Design Twitter/X
  • Multi-Pattern Problem: Design a Ride Booking Class Model
  • Multi-Pattern Problem: Design a Food Ordering Class Model
  • Design an Authentication/Authorization Module
  • Design a Form/Survey Builder
  • Design a Meeting Room / Calendar Booking System
  • Design a Search/Filter Engine for an E-Commerce Catalog
  • Design a Voting/Polling System
  • Design a Retry Mechanism with Backoff
  • Design a Health Check Aggregator
  • Design a Feature Flag System
  • Design a Config Management Client
  • Design a Leader Election Algorithm
  • Design a Distributed Counter
  • Design a Bloom Filter
  • Design a Consistent Hashing Ring
  • Design a Distributed Tracing Library
  • Design a Metrics Collection Library
  • Design an Audit Logging Framework
  • Design a Plugin/Extension System
  • Design a Workflow Engine
  • Multi-Pattern Capstone: Design a Food Delivery App
Chaturmind
← Low-Level Design & Design Patterns

OOP Fundamentals & SOLID Principles

  • Object-Oriented Design Refresher
  • Single Responsibility & Open/Closed
  • Liskov Substitution & Interface Segregation
  • Dependency Inversion

Creational Design Patterns

  • Singleton Pattern
  • Factory & Abstract Factory
  • Builder Pattern
  • Prototype Pattern

Structural Design Patterns

  • Adapter & Facade
  • Decorator Pattern
  • Composite & Proxy
  • Bridge & Flyweight

Behavioral Design Patterns

  • Strategy Pattern
  • Observer Pattern
  • Command Pattern
  • Template Method
  • State Pattern
  • Chain of Responsibility
  • Iterator & Mediator
  • Visitor Pattern

Machine Coding Exercises

  • Parking Lot — Requirements & Class Design
  • Parking Lot — Implementation
  • Elevator System — Requirements & Class Design
  • Elevator System — Implementation & Scheduling
  • Library Management System
  • Tic-Tac-Toe / Board Game Design
  • Chess Engine Design
  • Vending Machine Design
  • ATM System Design
  • In-Memory Rate Limiter
  • Thread-Safe LRU Cache
  • Connection Pool Design
  • Producer-Consumer Class Design
  • Movie Ticket Booking System
  • Hotel Reservation System
  • Airline Booking / Seat Selection System
  • Splitwise / Expense Sharing System
  • Design a Logging Framework
  • Design a Notification/Observer-Based Pub-Sub
  • Design a Cache with Pluggable Eviction Policy
  • Design Snake and Ladder
  • Design a Card Game Framework
  • Design an Inventory Management System
  • Restaurant Management System
  • Food Delivery Order Matching
  • Design a Shopping Cart & Checkout Flow
  • Design a Recommendation Engine
  • Design a Job Scheduler
  • Design a Circuit Breaker
  • Design a Distributed ID Generator
  • Design a Pub-Sub Message Broker
  • Design Twitter/X
  • Multi-Pattern Problem: Design a Ride Booking Class Model
  • Multi-Pattern Problem: Design a Food Ordering Class Model
  • Design an Authentication/Authorization Module
  • Design a Form/Survey Builder
  • Design a Meeting Room / Calendar Booking System
  • Design a Search/Filter Engine for an E-Commerce Catalog
  • Design a Voting/Polling System
  • Design a Retry Mechanism with Backoff
  • Design a Health Check Aggregator
  • Design a Feature Flag System
  • Design a Config Management Client
  • Design a Leader Election Algorithm
  • Design a Distributed Counter
  • Design a Bloom Filter
  • Design a Consistent Hashing Ring
  • Design a Distributed Tracing Library
  • Design a Metrics Collection Library
  • Design an Audit Logging Framework
  • Design a Plugin/Extension System
  • Design a Workflow Engine
  • Multi-Pattern Capstone: Design a Food Delivery App
HomeLearnSystem DesignLow-Level Design & Design PatternsMachine Coding Exercises
✓ FreeAdvanced· 8 min read

Design a Consistent Hashing Ring

Node placement, key lookup, and virtual nodes for balance, walking through adding/removing a node with minimal key remapping, and the direct line to both distributed cache node placement and database sharding.

Published September 23, 2026


Design a Consistent Hashing Ring

Core implementation: node placement and key lookup

class ConsistentHashRing {
    TreeMap<Long, String> ring = new TreeMap<>(); // hash position -> node ID, sorted

    void addNode(String nodeId) {
        ring.put(hash(nodeId), nodeId);
    }

    String getNode(String key) {
        long keyHash = hash(key);
        Map.Entry<Long, String> entry = ring.ceilingEntry(keyHash); // first node clockwise from the key
        return (entry != null) ? entry.getValue() : ring.firstEntry().getValue(); // wrap around the ring
    }
}

A TreeMap (sorted by hash value) IS the ring, conceptually — nodes are placed at their hash positions around it, and a key is assigned to the FIRST node found going clockwise from the key's own hash position (ceilingEntry, wrapping around to the first node if the key's hash is past every node's position). This structure is what makes lookups fast (O(log n) via the tree) and makes the "assign key to nearest node clockwise" rule simple to implement directly.

Walking through adding/removing a node: minimal remapping

Before: Node A at position 10, Node B at position 50, Node C at position 90
  (ring wraps at, say, 100)
  Keys hashing to 11-50 -> Node B; 51-90 -> Node C; 91-10 (wrapping) -> Node A

Add Node D at position 30:
  Only keys that PREVIOUSLY mapped to positions 11-30 now remap (to D instead of B)
  Everything else (51-90 -> C, 91-10 -> A) is COMPLETELY UNAFFECTED

This is consistent hashing's entire value proposition, made concrete: adding or removing ONE node only affects the keys in the hash-range NEIGHBORING that node — a naive hash(key) % nodeCount scheme, by contrast, would remap NEARLY EVERY key the moment nodeCount changes at all, since the modulus itself changes. Consistent hashing's minimal-remapping property is precisely why it's the standard technique anywhere nodes are added/removed dynamically without wanting to trigger a massive, disruptive full-cluster rebalance.

Virtual nodes: solving the hotspot problem

void addNode(String nodeId, int virtualNodeCount) {
    for (int i = 0; i < virtualNodeCount; i++) {
        ring.put(hash(nodeId + "#" + i), nodeId); // multiple ring positions, ALL mapping back to the same physical node
    }
}

Without virtual nodes, removing ONE physical node dumps its ENTIRE key range onto exactly ONE neighbor (whichever node is next clockwise) — creating an immediate, severe hotspot on that single neighbor. Giving each PHYSICAL node MULTIPLE positions on the ring ("virtual nodes," each independently hashed) spreads that same removed node's key range across MANY different neighbors instead of dumping it all onto one — this is a genuinely essential refinement, not an optional optimization; a real consistent-hashing implementation without virtual nodes has a real, predictable hotspot problem baked in.

Direct connection to distributed caching and database sharding

This exact ring structure IS the node-placement mechanism underlying Distributed Cache's scaling story (assigning cache keys to cache nodes) — building it here from scratch is what makes that earlier design's "consistent hashing" reference concrete rather than a name-dropped buzzword. The SAME technique, applied to DATABASE SHARDS instead of cache nodes, is hash-based database sharding — Database Scaling Specifics' sharding discussion and this ring are the identical underlying mechanism, just applied to a different kind of node (a database shard rather than a cache server).

Follow-up questions this topic invites — and their answers

Q: How many virtual nodes per physical node is typically reasonable? A: Commonly somewhere in the range of 100-200 per physical node in real systems — more virtual nodes give smoother load distribution (closer to perfectly even) at the cost of more ring entries to maintain and search through; this is a genuine tunable trade-off between balance quality and ring-management overhead, not a fixed universal number.

Q: Does consistent hashing guarantee PERFECTLY even key distribution even with virtual nodes? A: No — it guarantees APPROXIMATELY even distribution with high probability, improving as virtual-node count increases, but true perfect evenness isn't mathematically guaranteed by the hashing approach itself; for workloads needing very tight load balancing, monitoring actual per-node load (Metrics & Monitoring) and adjusting virtual node counts per physical node capacity is still a real operational practice.

Q: What happens to REPLICATION in a consistent hashing ring — does each key live on only one node? A: In practice, a key is typically replicated to the NEXT N NODES clockwise from its primary position (not just the first one) for fault tolerance — this is a direct extension of the same ring-walking lookup logic, just continuing past the first match to collect N distinct physical nodes for replica placement.

Q: How does adding a node interact with in-flight requests during the rebalance? A: A production implementation needs to handle the transition window carefully — requests for keys that are REMAPPING need their data to actually be present at the new node before being served from there (a data-migration step, not just a ring-metadata update), which is a genuinely nontrivial operational concern beyond the pure hashing algorithm covered here.

Previous

Design a Bloom Filter

Next

Design a Distributed Tracing Library

AI Tutor

Lesson: Design a Consistent Hashing Ring

Quick actions

AI responses can be inaccurate. Verify critical information.