Skip to main content

Command Palette

Search for a command to run...

Load Balancing

Published
2 min read

When we have multiple servers working together, the first thing we need to do is load balancing, so that no single server gets overloaded. The load balancer distributes incoming requests across all the servers based on different strategies like round-robin, least-connections, or server capacity. It also performs health checks to make sure only healthy servers receive requests, and in case one server goes down, the traffic is redirected to the others. This helps maintain availability and improves response times.

To distribute data or requests more efficiently across servers, we use hashing techniques, mainly traditional hashing and consistent hashing.

In traditional hashing, a simple hash function assigns data to a server based on the hashed value. This works fine when the server count is fixed, but if any server goes down or we add a new one, we have to rehash almost all the data, which is expensive and time-consuming.

In consistent hashing, servers are arranged on a logical ring, and data is placed based on its hashed position on the ring. If a server goes down or a new one is added, we only need to rehash the data between the affected servers. This makes it more efficient, especially for distributed systems where servers change frequently.

The main difference is that traditional hashing breaks when the number of servers changes, while consistent hashing handles server additions and failures smoothly. Traditional hashing also tends to create uneven load distribution, whereas consistent hashing provides better balancing, especially when combined with virtual nodes.

Difference Between Traditional Hashing and Consistent Hashing

FeatureTraditional HashingConsistent Hashing
RehashingRehash almost all data on server changesRehash only affected data
ScalabilityLess scalable, not practical for dynamic systemsHighly scalable for dynamic systems
Server ChangesHard to add/remove serversEasy to add/remove servers
Load DistributionCan become unevenMore balanced with virtual nodes
Use CaseSmall systems with fixed serversDistributed systems, caches, NoSQL