Expire the pending to-do count instead of recounting on write
What does this MR do and why?
TodoService recounts the acting user's cached pending to-do count inside every resolve and restore (resolve_todos, resolve_todo, restore_todos and restore_todo). It does this through User#update_todos_count_cache, which calls todos_pending_count(force: true). The count runs right away, in the same request or job as the write, so it goes to the primary database, whether or not anyone will read it.
Many common actions resolve the acting user's to-do items for a target: adding an award emoji, commenting, replying in or resolving a thread, pushing to a merge request, retrying a build (for every merge participant), approving, publishing a review, and closing or merging. Usually the user has no pending to-do item on that target, so nothing changes, but the count still runs. Users who never look at the count, such as bots, pay for it on every one of these actions.
This MR expires the cached count instead (User#invalidate_todos_cache_counts), so it's recounted the next time it's read: by the navigation badge, GET /user_counts, or the mobile badge push. The cache is still cleared on every resolve and restore, including the ones that change nothing, so the count is exactly as fresh as it is today.
The cache is expired only after the surrounding transaction commits, so a read in between can't cache the old count. The user is then stuck to the primary database (ApplicationRecord.sticking.stick(:user, user.id)), so their next request doesn't recount from a lagging replica. This matters for resolves that run in Sidekiq, which don't otherwise stick the user's web session.
The change is behind the todos_pending_count_invalidate_on_write feature flag (gitlab_com_derisk, user actor, disabled by default). With the flag disabled, nothing changes.
Why the count is recomputed on write
The count was first cached in 2016 (gitlab-foss!4438 (merged)), to keep page loads, which show it in the navigation, from running it. In review, recomputing right away was preferred over expiring the cache, so that "the cache exists whenever the user loads a page, removing any impact caching might have on the request time" (gitlab-foss!4438 (comment 12222945)).
At the time the count was a plain todos.pending.count. Since then:
- It moved to
TodosFinder(gitlab-foss!5789 (merged)). - It hides pending to-do items authored by banned users, which joins
banned_users(!159542 (merged)). - It excludes snoozed to-do items (!175163 (merged)).
- More actions resolve to-do items, for example closing or merging a merge request (!229010 (merged)).
So the count no longer reads only an index, and writes that trigger it are now often more frequent than the reads. Expiring on write moves the cost to the reads. Active users pay about what they do today: one count on their next page load after a change. Users who never read the count stop paying for it.
Alternative considered
Keep the old value and recount in a deduplicated background job. That keeps page loads from ever running the count, but it adds a worker, shows a stale count for a few seconds after a change, and still counts for users who never read it. Expiring is simpler and removes the cost for those users completely.
References
- https://gitlab.com/gitlab-org/gitlab/-/work_items/635768
- https://gitlab.com/gitlab-org/gitlab/-/work_items/636572
- Rollout: #639664
Screenshots or screen recordings
Not applicable, no UI change.
How to set up and validate locally
- In the Rails console, enable the flag:
Feature.enable(:todos_pending_count_invalidate_on_write) - Add an award emoji to an issue where you have no pending to-do item. In
log/development.log, the request no longer runs a count of your pending to-do items. - Load any page. The navigation shows the correct to-do count, and the count runs once on that page load, not again on the next one.
- Mark a pending to-do item as done and reload. The to-do count in the navigation goes down by one.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.