In high-availability environments with multiple application nodes, the reverse proxy acts as a load balancer across backend servers. In these cases, managing a sticky session (or session affinity) is essential to keep the user bound to the same node throughout their session.
Each backend should identify itself with a unique route ID (e.g., xs22001) and include it in the session cookie:
Set-Cookie: XSTORESESSION=abcdef123456.xs22001; Path=/; HttpOnly; SameSite=Lax
In Delphi, the logic might look like this:
function GetRouteSuffix: string;
begin
Result := 'xs' + IntToStr(Settings.porta_http);
end;
procedure SetSessionCookie(Response: TWebResponse; SessionID, RouteSuffix: string);
begin
Response.SetCookieField(
'',
'XSTORESESSION',
SessionID + '.' + RouteSuffix,
'/',
Now + 1,
True, // Secure
True, // HttpOnly
'Lax' // SameSite
);
end;
The SameSite attribute is crucial when setting the session cookie:
<Proxy "balancer://xstorecluster">
BalancerMember http://10.30.0.23:22001 route=xs22001
BalancerMember http://10.30.0.23:22002 route=xs22002
ProxySet stickysession=XSTORESESSION
</Proxy>
ProxyPass "/" "balancer://xstorecluster/"
ProxyPassReverse "/" "balancer://xstorecluster/"
upstream xstorecluster {
ip_hash;
server 10.30.0.23:22001;
server 10.30.0.23:22002;
}
server {
listen 443 ssl;
server_name tecnomodelcar.com;
location / {
proxy_pass http://xstorecluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
When implementing sticky sessions, ensure that:
Redis is a blazing-fast open source in-memory store, ideal for temporary data like user sessions.
Instead of sticky sessions, you can centralize session storage in a Redis instance shared by all backend servers:
Advantages of Redis over sticky session:
Redis requires additional infrastructure and security measures, but it is the best choice in enterprise or microservice architectures.
Author: Ivan Revelli – Synaptica