Sessions
PingGateway uses sessions to group requests from a user-agent or other source and to collect and share information across the requests.
-
For stateful sessions, PingGateway stores the session data (default). It sets a session cookie on the user-agent that references the session.
-
For stateless sessions, the user-agent stores the session data, and PingGateway puts the session data in a JWT stored as one or more session cookies on the user-agent.
Handlers and filters can access session data through the SessionContext.
Session sharing is not thread-safe, so it is not suitable for concurrent exchanges.
Feature | Stateful sessions | Stateless sessions |
---|---|---|
Cookie size |
Unlimited |
The maximum size of the JWT session cookie for a user-agent is 4 KBytes. If the cookie exceeds this size, PingGateway automatically splits it into multiple cookies. |
Default session cookie name |
|
|
Data types the session can store |
All types |
JSON-compatible types, such as strings, numbers, booleans, null, and arrays, lists, and maps containing only JSON-compatible types. |
Session sharing between PingGateway servers for load balancing and failover |
Possible using session stickiness |
Possible because the session is a cookie on the user-agent; PingGateway servers must share any encryption keys. |
Risk of data inconsistency when simultaneous requests modify the content of a session |
Low because PingGateway stores the session content for all exchanges. Processing is not thread-safe. |
Higher because the session content is reconstructed from the JWT for each request. Concurrent exchanges don’t have the same content. |
Stateful sessions
PingGateway uses stateful sessions by default.
To configure sessions explicitly, use an InMemorySessionManager as the "session"
in the AdminHttpApplication (admin.json
) for administrative requests
and the GatewayHttpApplication (config.json
) or individual Route for other requests.
The default session duration is 30 minutes. Even if the session is empty, the session remains usable until the timeout.
PingGateway can store any object type in a stateful session.
Because PingGateway stores stateful session content and shares it across all exchanges, simultaneous requests can update the session with low risk of the data becoming inconsistent. Sessions aren’t thread-safe, however; different requests can simultaneously read and modify a shared session.
Stateless sessions
To configure stateless sessions, use a JwtSessionManager for the "session"
in the AdminHttpApplication (admin.json
) for administrative requests
and the GatewayHttpApplication (config.json
) or individual Route for other requests.
PingGateway serializes session information as a JWT that is encrypted using authenticated encryption and optionally compressed. PingGateway stores the JWT in one or more session cookies on the user-agent. The JWT contains session attributes as JSON with a marker for the session timeout:
-
PingGateway can only serialize JSON-compatible types in JWT session cookies.
-
The maximum size of the JWT session cookie for a user-agent is 4 KBytes. If the cookie exceeds this size, PingGateway automatically splits it into multiple cookies.
-
When PingGateway serializes an empty session, it marks the supporting cookie as expired, so the user-agent effectively discards it.
To prevent PingGateway from cleaning up empty session cookies, add information to the session context with an AssignmentFilter.
PingGateway manages stateless sessions as follows:
-
When a request enters a route using stateless sessions, PingGateway creates the SessionContext, verifies the cookie signature, decrypts the content of the cookie, and checks the current date is before the session timeout.
-
As the request passes through the filters and handlers in the route, the filters and handlers can read and modify the session content.
-
When the request returns to the point where the session was created, such the entrance to a route or
config.json
, PingGateway updates the cookie as follows:-
If the session content has changed, PingGateway serializes the session, creates one or more new JWT session cookies with the new content, encrypts the cookies using authenticated encryption, assigns them an appropriate expiration time, and returns them in the response.
-
If the session is empty, PingGateway deletes the session, creates a new expired JWT session cookie, and returns the cookie in the response.
-
If the session content has not changed, PingGateway does nothing.
-
Because the session content is stored on the user-agent, PingGateway servers can easily share stateless sessions. The user-agent returns the JWT cookies and any PingGateway server can unpack and use the session content.
When PingGateway updates stateless sessions in simultaneous requests, there is a high risk of the data becoming inconsistent. PingGateway reconstructs the session content for each exchange. Concurrent exchanges don’t have the same content. PingGateway doesn’t share sessions across requests. Each request has its own session objects that it modifies as necessary, writing its own session to the session cookie regardless of what other requests do.
Session stickiness
Session stickiness helps ensure a client request goes to the server holding the original session data. With session stickiness, a load balancer in front of multiple PingGateway servers sends all requests from the same client session to the same server.
How you configure session stickiness depends on your load balancer.
Configure session stickiness whenever PingGateway stores session data attached to a server-side context. For example, configure session stickiness when using stateful sessions and multiple PingGateway servers.