Inferensys

Glossary

IP Hash

IP hash is a deterministic load balancing algorithm that uses a hash function on the client's source IP address to map requests to a specific backend server, ensuring session persistence.
Product team prototyping AI features on laptops, mockups on screens, collaborative ideation session.
LOAD BALANCING ALGORITHM

What is IP Hash?

IP hash is a deterministic load balancing method used to ensure session persistence by mapping client requests to specific servers.

IP hash is a load balancing algorithm that uses a hash function on the client's source IP address to determine which backend server will handle the request. This creates a deterministic, sticky mapping, ensuring all requests from that specific IP are sent to the same server for the duration of a session. It is a form of session persistence that simplifies state management for applications where user session data is stored locally on a server.

The algorithm calculates a hash value from the client's IP address, often combined with the port number, and uses modulo arithmetic against the number of available healthy servers. While effective for sticky sessions, it can cause uneven load distribution if client IPs are not uniformly distributed, such as when traffic originates from a few large network gateways. It is commonly implemented in Layer 4 (transport layer) load balancers and is a foundational technique within broader consistent hashing schemes used in distributed systems.

LOAD BALANCING ALGORITHM

Key Features of IP Hash

IP Hash is a deterministic, session-aware load balancing method that uses a hash of the client's source IP address to assign requests to backend servers. This ensures consistent routing for a given client.

01

Deterministic Session Persistence

IP Hash provides session persistence (sticky sessions) by guaranteeing that all requests from a specific client IP are routed to the same backend server for the duration of a session. This is critical for stateful applications where user session data is stored locally on a server.

  • Mechanism: A hash function (e.g., MD5, SHA-1) is applied to the client's source IP address.
  • Output: The hash output maps to a specific server in the pool.
  • Result: The same client IP always produces the same hash, directing it to the same server.

Example: An e-commerce shopping cart application relies on IP Hash to ensure a user's cart items, stored in a server's local memory, remain accessible throughout their browsing session.

02

Stateless Load Balancer Operation

Unlike methods that track server connection counts, IP Hash enables the load balancer itself to remain stateless. The routing decision is made anew for each request based solely on the client IP, simplifying the load balancer's logic and reducing its memory footprint.

  • No Session Table: The load balancer does not need to maintain a lookup table of client-to-server mappings.
  • Pure Calculation: Each request is independently evaluated using the hash function.
  • Scalability: This statelessness allows the load balancer to handle massive connection rates efficiently, as it performs a simple computation per request.
03

Handling of IP Address Changes & Asymmetry

A significant limitation of IP Hash is its vulnerability to changes in the client's apparent IP address. This can lead to broken sessions and is a key consideration for deployment.

Common Causes of IP Change:

  • Mobile Networks: A user switching from cellular to Wi-Fi.
  • Corporate NAT/PROXY Pools: Outbound requests from a large organization may rotate through multiple public IPs.
  • Load Balancer Chains: If traffic passes through multiple tiers of load balancers, the source IP seen by the final balancer may be the previous balancer's IP.

Consequence: The hash input changes, routing the client to a different server, which likely lacks their session state, causing errors.

04

Uneven Traffic Distribution

IP Hash can lead to skewed load distribution across servers, as it depends on the randomness of incoming IP addresses. It does not account for server load or capacity.

Scenarios Causing Imbalance:

  • Geographic Concentration: A large number of requests originating from a single corporate network (sharing a public IP) will all hash to the same server, overloading it.
  • Non-Uniform Hash Ranges: Even with a good hash function, real-world IP address distribution is not perfectly uniform, leading to some servers receiving more traffic.
  • Static Server Pool: The algorithm does not dynamically adjust for servers with different processing capabilities (unlike Weighted Least Connections).

Mitigation: Using a consistent hashing variant can improve distribution when servers are added or removed.

05

Configuration and Tuning Parameters

Implementing IP Hash involves configuring specific parameters on the load balancer to control its behavior and resilience.

Key Parameters:

  • Hash Function: Choice of algorithm (e.g., src_ip, src_ip+port, src_ip+dst_ip+port). Using source port can help differentiate multiple connections from the same NAT device.
  • Server Weight: Some implementations (e.g., NGINX's ip_hash) support weighted IP hash, where server weights are considered in the hash distribution.
  • Failover Behavior: Definition of what happens when the target server is unhealthy. Typically, the hash is recalculated (using a modulus) to find the next available server, breaking session persistence.

Example NGINX Directive:

nginx
upstream backend {
    ip_hash;
    server backend1.example.com weight=3;
    server backend2.example.com;
}
06

Use Cases vs. Alternatives

IP Hash is a specialized tool. Understanding when to use it versus other algorithms is crucial for system design.

Ideal for:

  • Legacy Stateful Applications: Where session data cannot be easily externalized to a shared cache or database.
  • Simple Persistence Needs: Environments with stable client IPs (e.g., internal corporate VPN access).
  • Computational Efficiency: When the load balancer must make ultra-fast routing decisions with minimal overhead.

Consider Alternatives When:

  • Client IPs are Unstable: Use cookie-based persistence (e.g., app_cookie, insert_cookie) for web applications.
  • Server Load Varies: Use Least Connections or Least Response Time for optimal resource utilization.
  • Dynamic Infrastructure: Use Consistent Hashing to minimize reshuffling when servers are added/removed frequently.
  • Geographic Routing: Use Latency-Based Routing or GeoDNS to direct users to the nearest data center.
ALGORITHM COMPARISON

IP Hash vs. Other Load Balancing Algorithms

A technical comparison of key characteristics between IP Hash and other common load balancing algorithms, focusing on their behavior in heterogeneous fleet orchestration and dynamic task allocation scenarios.

Feature / MetricIP HashRound Robin / Weighted Round RobinLeast Connections / Weighted Least ConnectionsLeast Response Time

Primary Routing Determinant

Hash of client source IP address

Sequential order in server list

Current active connection count

Combined metric of active connections and latency

Session Persistence Guarantee

High (for consistent client IP)

None

None

None

Traffic Distribution Evenness

Deterministic but can be uneven with few client IPs

Perfectly even (or weighted)

Dynamic, aims for even load

Dynamic, aims for optimal performance

Handling of Server State Changes (Add/Remove)

Poor (causes major hash ring reshuffle, breaking persistence)

Good (seamlessly integrates new servers into rotation)

Good (new connections distributed based on updated counts)

Good (new connections use updated metrics)

Algorithmic Complexity & Overhead

Low (simple hash calculation)

Very Low (increment pointer)

Medium (requires tracking and comparing connection counts)

High (requires active latency probing and connection tracking)

Ideal Use Case

Stateful applications requiring session stickiness (e.g., shopping carts, real-time control sessions for specific robots)

Stateless services, homogeneous server pools (e.g., API endpoints, sensor data ingestion)

Long-lived connections, variable server capacities (e.g., WebSocket streams, file transfers)

Latency-sensitive applications with variable backend performance (e.g., user-facing APIs, real-time dashboards)

Impact on Fleet Health & Battery-Aware Scheduling

Low adaptability; may route to an unhealthy or low-battery agent if its IP is hashed

High adaptability when combined with health checks

High adaptability; naturally avoids overloaded or struggling agents

Highest adaptability; actively routes to the most performant agent

Support for Dynamic Task Priority

None (routing is IP-bound, not task-bound)

Indirect (via server weighting)

Indirect (via connection weighting)

Indirect (latency may reflect current load from high-priority tasks)

IP HASH

Frequently Asked Questions

IP hash is a deterministic load balancing algorithm used to distribute client requests across a server pool. These questions address its core mechanics, use cases, and trade-offs within modern distributed systems and fleet orchestration.

IP hash is a deterministic load balancing algorithm that uses a hash function on the client's source IP address to determine which backend server will handle its requests. The algorithm takes the client's IP address (IPv4 or IPv6), applies a hash function (like MD5 or a simple modulo operation), and maps the resulting value to a specific server in the pool. This ensures that all requests from the same client IP are consistently routed to the same server for the duration of the session, providing session persistence without requiring server-side state sharing. For example, a request from 192.168.1.100 might hash to server index 2, guaranteeing it always reaches that server while the pool membership remains stable.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.