Definition – Source IP Affinity
Source IP Affinity, also known as IP hash or sticky sessions, is a method used in load balancing to ensure that requests from the same client are directed to the same server within a group of servers as long as the session persists. This technique is commonly used in scenarios where session persistence is critical, such as in web applications where the session state is saved locally on a server.
How Source IP Affinity Works:
- Client Request: A client (like a web browser) makes a request to a service that is hosted on multiple servers. The client’s request reaches a load balancer before it gets directed to the back-end servers.
- IP Hashing: The load balancer uses a hash function on the source IP address of the incoming request to determine which server to forward the request to. The hash function typically maps the IP address to a number that corresponds to one of the servers.
- Directing Traffic: Based on the result of the hash function, the load balancer forwards the request to a specific server. All subsequent requests from the same client (with the same IP address) will be directed to the same server, assuming no changes in the server pool (like adding or removing servers).
- Session Persistence: This ensures that the client can maintain a session with the specific server for the duration of their interaction, which is crucial for applications where the user’s state or session data is stored locally on a server.

Example
Let’s consider an online shopping website with three servers handling user sessions. The website uses Source IP Affinity to manage user sessions:
- Server A, Server B, Server C – Three servers available to handle requests.
- User 1 (IP: 192.168.1.1) – Makes a request to the website.
- Load Balancer – Applies a hash function to the IP address 192.168.1.1. Assume the hash function results indicate Server A for this IP.
- User Session: User 1 continues to browse the website, adding items to their cart. All requests from User 1 are directed to Server A, maintaining the session state where the cart items are stored.
- Repeat Visits: If User 1 returns to the website later that day without changing their IP address, they will be directed to Server A again, where their previous session and cart items can be resumed.
Benefits of Source IP Affinity
- Consistency: Users experience a consistent session without losing their data as they interact with the application.
- Efficiency: Reduces the need to replicate session data across servers, which can be resource intensive.
- Simplicity: Easy to implement as it relies only on the client’s IP address and does not require any additional tracking mechanisms.
Limitations
- IP Changes: If a user’s IP address changes (e.g., moving from cellular data to WiFi), they may be directed to a different server, losing their session state.
- Scalability: Adding or removing servers can disrupt the hash distribution, potentially causing many clients to lose session persistence.
- Load Imbalance: Not all servers may receive equal traffic, especially if certain IP ranges hash to the same server frequently.
Summary
Source IP affinity, also known as “sticky sessions” or “session persistence,” is a load balancing technique where requests from the same client IP address are consistently directed to the same backend server. Here’s how it works:
- Initial Request: When a client makes an initial request, the load balancer routes it to one of the available backend servers based on the load balancing algorithm.
- Affinity Mapping: The load balancer records the mapping between the client’s IP address and the chosen server.
- Subsequent Requests: Any subsequent requests from the same client IP are directed to the same server, maintaining session continuity.
Benefits
- Consistency: Ensures that all requests from a particular client are handled by the same server, which is important for applications that store session-specific information on the server.
- Session Management: Simplifies session management and state maintenance, improving the user experience by avoiding issues like frequent logins or session resets.
Use Cases
- Web Applications: Common in web applications requiring consistent user experience.
- E-commerce: Useful in e-commerce platforms where session data, like shopping carts, need to be persistent.
Source IP affinity helps maintain a consistent user experience and simplifies session management by ensuring that a client’s requests are always handled by the same backend server.
Useful Links
https://sanchitgurukul.com/proxy
https://sanchitgurukul.com/load-balancer
