The problem

Follow-up from: #1488630: Who's online block doesn't work with swappable session backends and lazy session creation and #40545: Improve speed by avoiding unnecessary updates in sess_write().

UserRequestSubscriber updates the {user_field_data}.access column for every auth user, every three minutes by default.

This means a write to a high-read table, invalidating the query cache for it.

Additionally we invalidate the persistent entity cache, which means several queries and a cache set on the next request if the current user account is loaded every request.

If you have a site with lots of authenticated users, this can mean a lot of database churn and cache invalidations.

Then we only use the information on user profile rendering to admins, and admin views by default, which is very rarely viewed compared to the number of times account objects get loaded (Ev

Both last access and last login are really activity logging, not properties of the user.

Proposed solution

Add a new service, maintaining its own storage, that handles getting and setting last access.

For backwards compatibility, call that service from UserStorageController - but leave out the field tables and entity cache.

Also needs views integration for the new table in a way that will work.

Remaining tasks

Agree this is a good idea. Possibly profiling to show how bad things are currently.

Comments

moshe weitzman’s picture

Sounds reasonable to me. It would be ideal if we preserved the ability to track last access time on a 'write once every n minutes' basis without going for the full 'track every page view' thats statistics module does.

Damien Tournoud’s picture

I think it makes sense to keep the {user}.access too, it's an important information for auditing purposes. But that can be implemented by the user module directly instead of being in the session code.

catch’s picture

I don't feel that {users}.access is that important for auditing, there's still {users.login}. If there's a use-case that really wants up-to-the-(5 by default)-minute access recording then is that important enough that core needs to cater for it by default?

sun’s picture

I'm not sure... I think I had quite some use-cases for {users}.access in the past, but not really for Statistics module.

However, I'd certainly agree that all of them were advanced, and all of them were custom to begin with. Having the value there provided by core was merely a nice convenience.

I'm also relatively sure that the last site access tracking could possibly be re-implemented in a better way. To begin with, avoiding recurring writes on the user table (which are not represented in cached entity loads as they are direct table writes).

E.g., if keyvalue_expirable had better query abilities (joins and filtering on values), then I would have suggested that, since old values wouldn't have to be stored forever, but that doesn't seem to be a good match.

I wonder whether it might make most sense to move the functionality into an own separate core module? I almost wonder whether it wouldn't make sense to combine {users}.access + .login with #1120928: Move "history" table into separate History module in such a new module that focuses on user activity tracking?

andypost’s picture

I think this one is a good candidate for #1029708: History table for any entity
Changing users.access means changing entity that does not actually happens, this makes cache for user entities useless so better get rid of the field in favor of history table record

catch’s picture

Title: Move who's online to statistics module, remove {users}.access » Factor {users_field_data}.access out of the table and entity caches
Version: 8.0.x-dev » 8.2.x-dev
Issue summary: View changes
Issue tags: +Performance

Saw this write and cache invalidation again when profiling another issue, re-titled and updated the issue summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.