Course contentsShow
← System Design
Lesson 89 of 1,9193. Load BalancingFree lesson

IP Hash Algorithm

Uses client IP address hash to consistently route requests from same client to same server.

IP Hash Algorithm

What you'll learn: How IP Hash routing ensures a specific client always reaches the same server by hashing their IP address.

What Is IP Hash?

The IP Hash algorithm is a load balancing strategy that uses the client's IP address to determine which backend server handles their request. The load balancer takes the client's IP address, runs it through a hash function (a mathematical operation that produces a consistent numerical value), and uses the result to select a server.

How It Works

Imagine a post office that always assigns the same mail carrier to your street based on your house number. No matter when you mail a letter, it's always the same carrier who picks it up.

Similarly, IP Hash works like this:

  1. Client with IP 192.168.1.100 sends a request
  2. Load balancer computes: hash(192.168.1.100) = 42
  3. Server assigned: 42 % number_of_servers = server_index
  4. Every future request from 192.168.1.100 repeats this calculation
  5. Result: same client → same server (as long as server pool doesn't change)

Why Use IP Hash?

This algorithm is valuable when you need session affinity or sticky sessions without maintaining state in the load balancer itself. The hash function naturally creates consistency—no lookup tables or memory required.

Use cases:

  • Applications that store session data locally on servers
  • Caching scenarios where you want cache hits for repeat visitors
  • Systems that haven't yet migrated to stateless architectures

Limitations

If a server fails or you add/remove servers, the hash calculations change, potentially routing clients to different servers and breaking their sessions.

Key Takeaway: IP Hash uses the client's IP address as input to a hash function, ensuring the same client consistently reaches the same server without the load balancer storing session state.