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

Screenshots or screen recordings

Not a UI change.

How to set up and validate locally

  1. Enable the flag in a Rails console:
    Feature.enable(:approval_check_fewer_lookups)
  2. 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
  3. Set the temporary state in the Rails console, and check the merge request reports approvals_syncing, as before:
    MergeRequest.find(<merge_request_id>).approval_state.temporarily_unapprove!
    This sets the key for 5 minutes. A push to the source branch also sets it, but only for 15 seconds (TEMPORARILY_UNAPPROVED_TTL_ON_RESET), which is hard to catch with curl.
  4. 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) (on ApprovalState and EE::MergeRequest) passes the option to legacy_temporarily_unapproved?, which memoizes the EXISTS on mr_<id>_cannot_merge in the ApprovalState instance only when called with memoize: true and the flag is on (project actor). Once memoized, plain calls on the same object reuse the read.
  • Only CheckApprovedService#cacheable? passes memoize: true. RunChecksService#run_check calls cacheable? only when use_cache is true, so the merge paths (FromSourceBranch#mergeable? and MergeWhenChecksPassService#process, both use_cache: false) never start the memo: execute and approved? 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 the execute read and the approved? read.
  • temporarily_unapprove! and delete_legacy_temporarily_unapproved_key! (used by expire_unapproved_key!) clear the memo, so writes through the same object are seen immediately.
  • approved? is already strong_memoize_attr on the same object, so this memo has the same lifetime as the existing approval memo: merge_request.approval_state until reset_approval_cache!.
  • The deterministic sync coordinator check (any_sync_in_progress?) is not memoized, so with deterministic_approval_rules_sync on each call still makes its SCARD calls. That flag has never been enabled on gprd (no entries in the feature-flag-log project), so on GitLab.com only the EXISTS path runs today.
  • Flag off: master behaviour, apart from the Feature.enabled? call in cacheable? and a strong_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 (execute and approved? reuse the cacheable? read), plus 1 more for not_approved merge requests from the detailed merge status approvals_syncing branch.
  • Production averages on slow GET /groups/:id/merge_requests requests (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 EXISTS is 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 a memoize: true call, a read per call without one, the memo cleared by temporarily_unapprove! and expire_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.rb is 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_status take 40.6k of those seconds (33%), read from the per-MR application_json lines that share the request's correlation_id (mergeability.<check>.duration_s.values).
  • Seconds per hour by check: commits_status 11,174, approved 9,148, security_policy_violations 8,238, conflict_status 4,157, path_locks 2,100, lfs_file_locks 1,980, discussions_status 1,453, rebase_status 1,317.
  • Most of the load comes from bots polling state=opened&per_page=100 every 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.

Edited by Marc Shaw

Merge request reports

Loading
Loading