Issue #2079, Add cluster support for strict sessions and lazy write #2264
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
( Tests pass locally )
The purpose of these changes are to improve the efficiency of the RedisCluster session handler under high traffic. Strict session support has also been added as a side effect of implementing PS_MOD_UPDATE_TIMESTAMP.
session.lazy_write supports the notion that we do not attempt to write session data if it has not been modified, however with redis cluster sessions we always write session on close.
This patch modifies this behaviour to issue SETEX only if data has changed, and EXPIRE otherwise to bump the session TTL
The following performance improvements are observed when running over the network:
Performance improvements for small payload sizes are negligible, however as the session payload size increases and the session read to write ratio increases we see a marked improvement in latency.
We can also further improve performance by eliminating an extra roundtrip through the use of GETEX when the session is opened, rather than updating the TTL at the end:
Comparing GETEX vs EXPIRE in the patched version

Comparing GETEX to develop:

This has less of an impact on large payload requests, but can greatly reduce the response time of lighter session sizes
The big caveat here is that GETEX requires redis 6.2, which is why I put it behind a config option ( it causes the session TTL to be counted from session start, rather than the end - which might impact long running scripts ). If you would prefer to maintain compatibility with older versions, then I can refactor this PR and remove early refresh, and just include support for SET-EXPIRE. If, however, you are happy to include this change, then it may be worth while to port support for this feature into standalone redis sessions too ( happy to attempt this aswell ).
Please see here for details on how session transactions differ between different modes
Comments welcome