Consistent Hashing in System Designing
advance · System Designing
The concept you are describing is Consistent Hashing , which is the foundational algorithm used to scale distributed systems dynamically without crippling performance. When building large-scale distributed caches (like a multi-node Redis cluster) or distributed databases (like Apache Cassandra or Amazon DynamoDB), you need a way to determine exactly which machine stores a particular piece of data. Standard hashing falls apart when you change the cluster size, but consistent hashing solves this elegantly. 1. The Core Problem with Naive Hashing In a standard distributed database with $N$ servers, you typically map a data key to a server using the modulo operator: $\text{Server ID} = \text{Hash}(\text{key}) \pmod N$ While simple, this approach introduces a catastrophic flaw when scaling. If one server crashes, or if you add a new server to handle a traffic spike, your pool size changes from $N$ to $N \pm 1$ . Because the divisor in your formula has changed, the mathematical result for nearly every single key changes instantly. * In a cache system: This triggers a near-100% cache miss storm, forcing your backend databases to handle all traffic simultaneously, which usually crashes the system. In a database system: This forces you to move up to 90% of your data across the network to re-locate them to their new server IDs. 2. The Consistent Hashing Solution Consistent hashing decouples data routing from the total number of servers by mapping both the servers and the keys to a conceptual geometric circle called a Hash Ring . The Hash Ring: The ring represents a fixed numeric range (e.g., from $0$ to
^{32}-1$ ). Mapping Servers: Each physical server is passed through a hash function based on its IP address or name, mapping it to a specific point along the perimeter of the ring. Mapping Keys: When a data key needs to be written or read, its key string is hashed using the exact same function, placing it at a specific point on the ring. Data Routing (The Clockwise Rule): To find out which server owns a key, the system locates the key on the ring and travels clockwise until it encounters the very first server. That server is the data owner. 3. Adding and Removing Nodes ( $\sim1/N$ Shift) Because data routing is based on walking clockwise to the next server, adding or removing nodes has a highly localized impact: Adding a Server: If you insert a new Server X onto the ring, it will only intercept keys that fall between it and its counter-clockwise neighbor. The rest of the ring remains completely unaffected. Removing a Server: If a server crashes, its keys simply roll over clockwise to the next available server on the ring. On average, when the cluster size changes, only $\frac{1}{N}$ of the total keys need to be remapped or moved (where $N$ is the total number of nodes). This turns a system-wide re-sharding disaster into a minor background task. 4. Virtual Nodes: Optimizing Load Distribution In a pure consistent hashing ring, servers are mapped randomly based on their hashes. This introduces two major real-world scaling problems: Hotspots: Servers might wrap unevenly around the ring, creating massive data gaps where one server is forced to hold 70% of the keys while another holds only 5%. Heterogeneous Hardware: A massive bare-metal server with 128 cores will sit on the ring with the exact same presence as a tiny virtual machine instance with 2 cores. To solve this, systems implement Virtual Nodes (Vnodes) . Instead of mapping a physical machine ( Server A ) to a single point on the ring, the system generates hundreds of virtual tokens for it (e.g., Server A-1 , Server A-2 , Server A-100 ) and scatters them randomly all over the ring. The Benefits of Virtual Nodes: Uniform Distribution: By breaking physical servers into hundreds of virtual points, the data keys get distributed across the physical hardware with near-perfect uniformity, eliminating hotspots. Proportional Scaling: If Server B is twice as powerful as Server A , you can allocate 200 virtual nodes to Server B and only 100 to Server A . The powerful machine will naturally absorb a proportional workload. Blazing Fast Recovery: If a physical server dies, its virtual nodes are scattered everywhere. Instead of a single neighbor server getting slammed with 100% of the dead server's traffic, the load is distributed evenly among all remaining servers on the ring. 5. Industry Footprint Consistent hashing is the hidden engine behind some of the most resilient distributed architectures globally: Amazon DynamoDB & Apache Cassandra: Use it to determine which partition nodes hold specific primary keys in a masterless distributed cluster. Memcached & Redis Clusters: Use it to distribute cached keys evenly across a farm of memory servers to prevent cache stampedes during node recycles. Akamai CDN: Uses it to ensure web requests for specific content always land on the same edge proxy server, maximizing local cache hit ratios.
Careeroza — One-stop Zone for Aspirants
Study material, Careeroza mentorship, tech jobs, and career guidance on careeroza.com.
Public study materials
- Node.js — Server-side JavaScript (nodejs)
- Getting started · basic
- JavaScript on the server · basic
- CommonJS modules · basic
- ES modules (ESM) · basic
- npm & package management · basic
- Asynchronous JavaScript in Node · basic
- The event loop · basic
- Essential core utilities · basic
- process & configuration · basic
- File system basics · basic
- HTTP & HTTPS servers · medium
- Streams · medium
- Events & EventEmitter · medium
- Advanced filesystem · medium
- crypto · medium
- Compression & encoding · medium
- Child processes · medium
- net, dgram & DNS · medium
- readline, timers & scheduling · medium
- Testing & diagnostics (intro) · medium
- Worker threads · advance
- cluster & multi-process scaling · advance
- Performance & tuning · advance
- Debugging & observability · advance
- Security hardening · advance
- Native addons & N-API · advance
- Architecture patterns · advance
- Graceful shutdown · advance
- 100 Questions · interview questions
- Django (Django)
- What is Django · basic
- Installing Django · basic
- Features of Django · basic
- MVT Architecture · basic
- Django vs Flask · basic
- Creating Project & Creating App · basic
- Django Project Structure · basic
- URL Routing · basic
- Views · basic
- Templates · basic
- Static & Media Files · medium
- Models · medium
- ORM (Object Relational Mapping) · medium
- Model Relationships · medium
- Migrations · medium
- Django Admin · medium
- Forms · medium
- Authentication · medium
- Authorization · medium
- Middleware · medium
- Signals · medium
- Class Based Views Deep Dive · advance
- Generic Views · advance
- File Handling · advance
- Django REST Framework (DRF) · advance
- Advanced ORM · advance
- Caching · advance
- Asynchronous Django · advance
- Background Tasks · advance
- Interview Questions · interview-questions
- Python (Python)
- Python Fundamentals · basic
- Control Flow · basic
- Strings · basic
- Collections / Data Structures · basic
- Functions · basic
- Modules and Packages · basic
- File Handling · basic
- Exception Handling · basic
- Object-Oriented Programming (OOP · medium
- Advanced Python Concepts · advance
- Functional Programming · advance
- Multithreading & Multiprocessing · advance
- Async Programming · advance
- JavaScript (JavaScript)
- JS Introduction · basic
- Variables & Data Types · basic
- Operators · basic
- Control Flow · basic
- Functions · basic
- Scope & Execution · basic
- Closures · basic
- Objects · basic
- Arrays · basic
- Strings · basic
- DOM Manipulation · basic
- Browser APIs · medium
- Asynchronous JavaScript · medium
- Fetch & APIs · medium
- ES6+ Features · medium
- OOP in JavaScript · medium
- Prototype & Inheritance · advance
- Advanced Functions · advance
- Memory Management · advance
- Error Handling · advance
- Modules · advance
- Advanced Async Concepts · advance
- Functional Programming · advance
- JavaScript Internals · advance
- Performance Optimization · advance
- System Designing (System Designing)
- Day-1 : What is system Designing ? · basic
- Day-2 : Vertical vs. Horizontal Scaling · basic
- Day-3:How to do vertical scaling ? · basic
- Day4:How to do horizaontal scaling ? · basic
- Day:5TCP vs UDP · basic
- Day6:IP & DNS · basic
- Day7:Client-Server Model · basic
- Day8:HTTP & HTTPS · basic
- Databases (SQL vs NoSQL) · medium
- Caching · medium
- Day9:Latency & Throughput · basic
- Load Balancing · medium
- Indexes & Query Optimization · medium
- CDN · medium
- Proxies · medium
- Message Queues · medium
- Horizontal vs Vertical Scaling · medium
- Database Replication · advance
- Database Sharding · advance
- Consistent Hashing · advance
- CAP Theorem · advance
- Rate Limiting · advance
- Service Discovery · advance
- Event-Driven Architecture · advance
- API Gateway · advance
- Distributed Consensus · expert
- Microservices · expert
- Observability · expert
- Idempotency · expert
- PACELC Theorem · expert
- Two-Phase Commit · expert
- Back-of-Envelope Estimation · expert
- Designing for Failure · expert
- Angular (Angular)
- Angular Fundamentals · basic
- Project Structure · basic
- Components & Templates · basic
- Data Binding · basic
- Directives · basic
- Pipes · basic
- Component Communication · basic
- Lifecycle Hooks · basic
- Routing Basics · basic
- Routing · basic
- API Calls · basic
- Forms · medium
- Routing · medium
- Services & Dependency Injection · medium
- RxJS & Observables · medium
- Authentication & Security · medium
- Component Interaction · medium
- State Management Basics · medium
- Error Handiling · medium
- Perfomance Basic · medium
- Real World Features · medium
- Advanced Angular Architecture · advance
- Change Detection · advance
- Advanced RxJS · advance
- State Management · advance
- Dynamic Rendering · advance
- Perfomance Optimization · advance
- Modern Angular · advance
- Express.js — Web APIs & middleware (expressjs)
- Application setup · basic
- Routing deep dive · basic
- 1. MVC / Layered Architecture · medium
- Validation · medium
- File uploads · medium
- Sessions & auth (stateful) · medium
- Passport & strategies · medium
- Templating & SSR · medium
- WebSockets & SSE · medium
- Security middleware · advance
- Reverse proxies & trust · advance
- Performance · advance
- API design & versioning · advance
- Testing with Supertest · advance
- GraphQL & tRPC (overview) · advance
- Deployment checklist · advance
- Middlewares · basic
- Request & Response · basic
- SQL (SQL)
- SQL Fundamentals · basic
- Database Operations · basic
- Table Operations · basic
- CRUD Operations · basic
- Filtering & Operators · basic
- SQL Functions · basic
- GROUPING Data · basic
- Joins · medium
- Constraints · basic
- Subqueries · medium
- Set Operators · medium
- Views · medium
- Indexes · medium
- Normalization · advance
- Transactions · advance
- Stored Procedures & Functions · advance
- Triggers · advance
- Advanced SQL · advance
- Query Optimization · advance
- Database Design · advance
- SQL Security · advance
- Backup & Recovery · advance
- Questions · interview questions
- STAR (Situation, Task, Action, and Result) (Situation Based Questions)
- System Architecture (System Architecture)
- Fundamentals of System Architecture · basic
- Distributed System Basics · basic
- System Reliability Concepts · basic
- Scaling Concepts · basic
- Networking Basics · basic
- Web Communication · basic
- API Communication · basic
- Proxy & Delivery Systems · basic
- Web Architecture Basics · basic
- Rendering Architectures · basic
- Frontend Advanced Concepts · basic
- Message Queue Basics · medium
- What is Load Balancer · medium
- Load Balancing Algorithms · medium
- API Design Basics · medium
- API Protection · medium
- Authentication Basics · medium
- Security Tokens · medium
- Security Threats · medium
- Encryption & Security · medium
- SQL Database Basics · medium
- SQL Scaling Concepts · medium
- NoSQL Databases · medium
- Database Optimization · medium
- Replication Strategies · medium
- Caching Basics · medium
- Cache Storage Systems · medium
- Cache Strategies · medium
- Event-Driven Systems · advance
- Queue Reliability · advance
- Microservices Basics · advance
- Microservice Communication · advance
- Distributed Transactions · advance
- DevOps Basics · advance
- Automation Tools · advance
- Deployment Strategies · advance
- Monitoring Basics · advance
- Monitoring Tools · advance
- Distributed System Concepts · advance
- Distributed Algorithms · advance
- MongoDB — Documents & data modeling (MongoDB)
- Introduction · basic
- Shell, Compass & tools · basic
- Databases & collections · basic
- CRUD operations · basic
- Indexes deep dive · medium
- Explain plans & performance · medium
- Aggregation framework · medium
- Schema design patterns · medium
- Mongoose basics · medium
- Mongoose advanced · medium
- Drivers & connection · medium
- Operators for updates & arrays · medium
- Replication & read preferences · advance
- Write concern & read concern · advance
- Multi-document transactions · advance
- Change streams · advance
- Sharding (overview) · advance
- Atlas Search & full-text · advance
- GridFS & large files · advance
- Backup, restore & ops · advance
- AWS Crash Course (AWS)
- What is Cloud ? · basic
- What is AWS ? · basic
- If not cloud ? · basic
- Cloud Computing · basic
- AWS Pricing · basic
- AWS Shared Responsibility Model · basic
- AWS Management Console · basic
- AWS SDKs · basic
- AWS IAM · medium
- Users, Groups, Roles · medium
- Policies · medium
- AWS Organizations · medium
- AWS Cognito · medium
- AWS Directory Service · medium
- AWS KMS (Key Management Service) · medium
- AWS Secrets Manager · medium
- AWS Shield · medium
- AWS WAF · medium
- AWS Inspector · medium
- AWS GuardDuty · medium
- EC2 · advance
- Launching EC2 Instances · advance
- EBS Volumes · advance
- Security Groups · advance
- Key Pairs · advance
- Elastic IP · advance
- User Data Scripts · advance
- Auto Scaling · advance
- Load Balancers · advance
- ALB · advance
- NLB · advance
- Serverless Compute ,AWS Lambda, Lambda Layers · advance
- Event-Driven Architecture · advance
- ECS · advance
- EKS · advance