Skip to content

Limit Session Metadata Writes

Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.2 (the current stable version).

The default behavior of PHP session is to persist the session regardless of whether the session data has changed or not. In Symfony, each time the session is accessed, metadata is recorded (session created/last used) which can be used to determine session age and idle time.

If for performance reasons you wish to limit the frequency at which the session persists, this feature can adjust the granularity of the metadata updates and persist the session less often while still maintaining relatively accurate metadata. If other session data is changed, the session will always persist.

You can tell Symfony not to update the metadata "session last updated" time until a certain amount of time has passed, by setting framework.session.metadata_update_threshold to a value in seconds greater than zero:

1
2
3
framework:
    session:
        metadata_update_threshold: 120

Note

PHP default's behavior is to save the session whether it has been changed or not. When using framework.session.metadata_update_threshold Symfony will wrap the session handler (configured at framework.session.handler_id) into the WriteCheckSessionHandler. This will prevent any session write if the session was not modified.

Caution

Be aware that if the session is not written at every request, it may be garbage collected sooner than usual. This means that your users may be logged out sooner than expected.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version