Read the temporarily unapproved key once per check
What does this MR do and why?
The approval mergeability check asks whether a merge request is temporarily unapproved from cacheable?, from execute and from approved?, and the detailed merge status asks again for not_approved merge requests. Each call was a Redis EXISTS on the same key, so one approval check made three round trips.
Behind approval_check_fewer_lookups, when the check uses its results cache, cacheable? reads the key once and the later calls reuse that read until the key is set or deleted. Merges run the check without the cache, so they still read Redis on every call, as on master.
That saves 2 Redis round trips per merge request (3 for not_approved ones). The estimated gain is about 10% of the approval check, or under 1% of GET /groups/:id/merge_requests on GitLab.com. Polling clients always miss the check's 60 second results cache, so they pay for these lookups on every request.
References
- Investigation: #631601
- Rollout issue: #634668 (closed)
Screenshots or screen recordings
Not a UI change.
How to set up and validate locally
- Enable the flag in a Rails console:
Feature.enable(:approval_check_fewer_lookups) - Open a merge request with an approval rule that needs approval and check that it reports
not_approved:curl --header "PRIVATE-TOKEN: <token>" "http://gdk.test:3000/api/v4/projects/<id>/merge_requests/<iid>" | jq .detailed_merge_status - Set the temporary state in the Rails console, and check the merge request reports
approvals_syncing, as before:This sets the key for 5 minutes. A push to the source branch also sets it, but only for 15 seconds (MergeRequest.find(<merge_request_id>).approval_state.temporarily_unapprove!TEMPORARILY_UNAPPROVED_TTL_ON_RESET), which is hard to catch with curl. - Clear it and check the merge request is back to
not_approved:MergeRequest.find(<merge_request_id>).approval_state.expire_unapproved_key!
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.
Detailed context for AI agents
Change (ee/app/models/approval_state.rb, check_approved_service.rb)
temporarily_unapproved?(memoize: false)(onApprovalStateandEE::MergeRequest) passes the option tolegacy_temporarily_unapproved?, which memoizes theEXISTSonmr_<id>_cannot_mergein theApprovalStateinstance only when called withmemoize: trueand the flag is on (project actor). Once memoized, plain calls on the same object reuse the read.- Only
CheckApprovedService#cacheable?passesmemoize: true.RunChecksService#run_checkcallscacheable?only whenuse_cacheis true, so the merge paths (FromSourceBranch#mergeable?andMergeWhenChecksPassService#process, bothuse_cache: false) never start the memo:executeandapproved?each read Redis, as on master. Neither path runs a cached check on the same object first. This came out of review: a memo on the merge path could let a merge through if the key is set between theexecuteread and theapproved?read. temporarily_unapprove!anddelete_legacy_temporarily_unapproved_key!(used byexpire_unapproved_key!) clear the memo, so writes through the same object are seen immediately.approved?is alreadystrong_memoize_attron the same object, so this memo has the same lifetime as the existing approval memo:merge_request.approval_stateuntilreset_approval_cache!.- The deterministic sync coordinator check (
any_sync_in_progress?) is not memoized, so withdeterministic_approval_rules_syncon each call still makes itsSCARDcalls. That flag has never been enabled on gprd (no entries in the feature-flag-log project), so on GitLab.com only theEXISTSpath runs today. - Flag off: master behaviour, apart from the
Feature.enabled?call incacheable?and astrong_memoized?check.
Measured
ee/spec/services/merge_requests/mergeability/check_approved_service_spec.rb counts EXISTS on the key for cacheable? plus execute on a not-approved merge request: exactly 3 with the flag off, 1 with it on. execute alone (the use_cache: false path) makes 2 with the flag on, the same as master. In a full detailed merge status run the security policy check (approved?) and the approvals_syncing branch add further calls that now hit the memo.
Expected saving (estimate, not measured on production)
- Calls saved per merge request on a results-cache miss: 2 (
executeandapproved?reuse thecacheable?read), plus 1 more fornot_approvedmerge requests from the detailed merge statusapprovals_syncingbranch. - Production averages on slow
GET /groups/:id/merge_requestsrequests (Kibana, 2026-10-19): Redis 0.76 s over 854 calls, about 0.9 ms per call; the approval check averages 22.6 ms per merge request. - 2-3 calls x ~0.9 ms is about 1.8-2.7 ms, or about 8-12% of the approval check. Applied to the check's 9,148 s/hour, that is roughly 700-1,100 s/hour of the endpoint's ~122k s/hour (under 1%).
- The 0.9 ms average covers all Redis calls on the request (cache, shared state, repository cache). A single SharedState
EXISTSis probably cheaper, so treat this as an upper bound.
Considered and dropped
Adding the approval rule associations that approval_rules.applicable_to_branch preloads to with_api_entity_associations. For open merge requests they are already loaded: applicable_post_merge_approval_rules matches every rule of an open merge request (the column is NULL until merge), the Preloader shares those records, and group_users loads groups as its through association. A spec showed zero queries with and without the extra preload, so it was removed.
Verification
ee/spec/models/approval_state_spec.rb: no further Redis read after amemoize: truecall, a read per call without one, the memo cleared bytemporarily_unapprove!andexpire_unapproved_key!, a read per call with the flag off.ee/spec/services/merge_requests/mergeability/check_approved_service_spec.rb: the 3 to 1 count above, and 2 reads without the results cache.ee/spec/services/merge_requests/reset_approvals_service_spec.rbis unchanged: plain calls no longer memoize, so its examples need no memo reset.
Production context
Measured on GitLab.com (Kibana, 2026-10-19) for GET /api/:version/groups/:id/merge_requests:
- About 139k requests and 122k seconds of request time per hour. The per-MR mergeability checks behind
detailed_merge_statustake 40.6k of those seconds (33%), read from the per-MRapplication_jsonlines that share the request'scorrelation_id(mergeability.<check>.duration_s.values). - Seconds per hour by check:
commits_status11,174,approved9,148,security_policy_violations8,238,conflict_status4,157,path_locks2,100,lfs_file_locks1,980,discussions_status1,453,rebase_status1,317. - Most of the load comes from bots polling
state=opened&per_page=100every few minutes: 53% of the endpoint's server time, with typical pages spanning 30 to 90 projects. - The approval check is never served from its 60 second results cache for these pollers, which come back every few minutes.
The investigation and the other changes from it are tracked in #631601. This is one of four independent drafts, each behind its own gitlab_com_derisk flag.