0
0
Nginxdevops~10 mins

IP hash for session persistence in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - IP hash for session persistence
Client sends request
Extract client IP
Calculate hash from IP
Map hash to backend server
Forward request to selected server
Server responds
Client gets consistent server
The client IP is used to create a hash that decides which backend server handles the request, ensuring the same client goes to the same server.
Execution Sample
Nginx
upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
This config uses ip_hash to send requests from the same client IP to the same backend server for session persistence.
Process Table
StepClient IPHash CalculationSelected ServerAction
1192.168.1.10hash(192.168.1.10) = 1backend1.example.comRequest forwarded to backend1
2192.168.1.11hash(192.168.1.11) = 2backend2.example.comRequest forwarded to backend2
3192.168.1.10hash(192.168.1.10) = 1backend1.example.comRequest forwarded to backend1 (same as step 1)
4192.168.1.12hash(192.168.1.12) = 1backend1.example.comRequest forwarded to backend1
5192.168.1.11hash(192.168.1.11) = 2backend2.example.comRequest forwarded to backend2 (same as step 2)
6192.168.1.13hash(192.168.1.13) = 2backend2.example.comRequest forwarded to backend2
7192.168.1.14hash(192.168.1.14) = 1backend1.example.comRequest forwarded to backend1
8192.168.1.15hash(192.168.1.15) = 2backend2.example.comRequest forwarded to backend2
9192.168.1.10hash(192.168.1.10) = 1backend1.example.comRequest forwarded to backend1 (session persistence)
10192.168.1.16hash(192.168.1.16) = 1backend1.example.comRequest forwarded to backend1
💡 Requests continue to be routed based on client IP hash, ensuring session persistence.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10
Client IP-192.168.1.10192.168.1.11192.168.1.10192.168.1.12192.168.1.11192.168.1.13192.168.1.14192.168.1.15192.168.1.10192.168.1.16
Hash Result-1211221211
Selected Server-backend1.example.combackend2.example.combackend1.example.combackend1.example.combackend2.example.combackend2.example.combackend1.example.combackend2.example.combackend1.example.combackend1.example.com
Key Moments - 3 Insights
Why does the same client IP always go to the same backend server?
Because the IP hash function maps the client IP to a specific backend server consistently, as shown in execution_table rows 1, 3, and 9 where the same IP 192.168.1.10 always selects backend1.
What happens if a new client IP appears?
The new IP is hashed and mapped to one of the backend servers based on the hash result, as in row 10 where 192.168.1.16 maps to backend1.
Can the distribution be uneven between servers?
Yes, because the hash depends on IP values, some servers may get more clients if IPs hash to them more often, as seen in variable_tracker where backend1 gets more selections.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. Which server handles the request from IP 192.168.1.11?
Abackend3.example.com
Bbackend1.example.com
Cbackend2.example.com
DNo server selected
💡 Hint
Check the 'Selected Server' column at step 5 in the execution_table.
At which step does the client IP 192.168.1.10 appear for the third time?
AStep 9
BStep 3
CStep 7
DStep 10
💡 Hint
Look at the 'Client IP' column in the execution_table and count occurrences of 192.168.1.10.
If a new backend server is added, how would the hash mapping change?
AAll client IPs would map to the new server only
BThe hash function would distribute IPs among all servers including the new one
CThe existing mapping would not change
DThe IP hash would stop working
💡 Hint
Consider how hashing distributes keys among available servers as shown in the concept_flow.
Concept Snapshot
ip_hash directive in nginx upstream block
- Uses client IP to calculate hash
- Routes requests from same IP to same backend
- Helps keep session persistence
- Simple config: ip_hash; inside upstream
- Works best with stable client IPs
- May cause uneven load distribution
Full Transcript
This visual execution shows how nginx uses the ip_hash method to keep session persistence. When a client sends a request, nginx extracts the client IP and calculates a hash value from it. This hash determines which backend server will handle the request. The same client IP always maps to the same backend server, ensuring the session stays on one server. The execution table traces multiple requests from different IPs, showing consistent server selection for repeated IPs. The variable tracker shows how client IPs, hash results, and selected servers change step-by-step. Key moments clarify why the same IP maps to the same server and what happens when new IPs or servers appear. The quiz tests understanding of server selection and hash behavior. The snapshot summarizes the ip_hash directive usage and benefits.