-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add a cookbook entry on how to prevent unnecessary session writes #3017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ Sessions | |
proxy_examples | ||
locale_sticky_session | ||
sessions_directory | ||
php_bridge | ||
php_bridge | ||
limit_metadata_writes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
.. index:: | ||
single: Limit Metadata Writes; Session | ||
|
||
Limit session metadata writes | ||
============================= | ||
|
||
.. versionadded:: 2.4 | ||
The ability to limit session metadata writes was added in Symfony 2.4. | ||
|
||
The default behaviour 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: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
framework: | ||
session: | ||
metadata_update_threshold: 120 | ||
|
||
.. code-block:: xml | ||
|
||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:session metadata-update-threshold="120" /> | ||
</framework:config> | ||
|
||
</container> | ||
|
||
.. code-block:: php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'session' => array( | ||
'metadata_update_threshold' => 120, | ||
), | ||
)); | ||
|
||
.. info:: | ||
|
||
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, that | ||
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. | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this too, I'm not sure it's relevant - misconfiguring GC values can always have consequences and it's not really part of this feature so it really doesn't belong here. You could just delete this caution entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think users should be warned that this feature "alters" the default GC behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont see how it affects GC: GC is set as a probability of being called by PHP and is a PHP ini configuration value. Your PR does nothing to affect that. what it does affect is the accuracy of the metadata so anything that relies on that metadata could be affected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, if my session data isn't saved each time, then (using the PdoSessionHandler as an example) when garbage collection is called (whenever it happens to be called), my session may appear to be older than it really is (e.g. the sess_time column shows 1 hour ago, even though I've been surfing around actively during the last hour). This would cause my session to get deleted earlier than it might have been. You could certainly write a session handler that doesn't use this time metadata during its garbage collection process, but most handlers would use it, wouldn't they? So if I understand it correctly. @adrienbrault's warning here makes sense. GC isn't triggered more often, but if it is, your session may be deleted earlier than it would have been otherwise.
Is that accurate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@weaverryan - what I am saying is it does not makes sense here. Triggering of GC is nothing to do with this feature so the caveat must not appear here or it is both confusing and in the wrong place meaning people would not see it unless browsing this particular entry.
If there is any place for a note (and I would say note not warning), it would be here: https://github.com/symfony/symfony-docs/blob/master/components/http_foundation/session_configuration.rst#configuring-garbage-collection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Drak Is the
saved time
updated on every request when you use theframework.session.metadata_update_threshold
option?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I'm wondering. It seems that if you use the threshold, then your saved time isn't updated on each request, so when garbage collection is randomly triggered, your session time may look old (and therefore could be deleted) when it is really not old. Because the saved time is not updated, my session may look like it was last saved 20 minutes ago, triggering cleanup if my lifetime is 15 minutes. But in reality, I've been surfing the site actively. If I understand this correctly, then this seems like an issue that's entirely specific to the threshold feature.
So @Drak is right that this doesn't affect the triggering of GC, but it does seem like the threshold will affect the behavior once the GC has been triggered. Do you agree? Perhaps it's just a wording issue and this would be better:
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I think that addresses the problem properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@weaverryan merge this - I will make a separate PR when I get time. There are a few things that need to be clarified properly. Better to have this documented now and I can tweak it afterwards, even if it's not 100% accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Drak sounds good :) - I've merged this in