Skip Redis in lock checks for projects without locks
What does this MR do and why?
The path lock and LFS lock mergeability checks read a cached result from Redis for every merge request, even when the check is inactive (the cached value is a constant) or the project has no locks at all. On merge request lists that is one or two Redis round trips per merge request for an answer that needs no I/O.
Behind mergeability_lock_checks_skip_redis, inactive checks and lock-free projects skip the results cache, and whether a project has locks is looked up once per project per request. Projects with locks keep today's cached path. Merges run the checks without the cache, so they still query locks fresh.
On GitLab.com these two checks are about 4% of the request time of GET /groups/:id/merge_requests. A page runs about two lock checks per project, so each project costs about 5 ms today, against one indexed EXISTS query after this change.
Supersedes !263466 (closed), rebased on current master.
References
- Investigation: #631601
- Rollout issue: #634664 (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(:mergeability_lock_checks_skip_redis) - Call the group list for a group with open merge requests, once with the flag on and once off, and compare the
detailed_merge_statusvalues. They should be identical:curl --header "PRIVATE-TOKEN: <token>" "http://gdk.test:3000/api/v4/groups/<id>/merge_requests?state=opened&per_page=100" - Lock a file in one of the projects (Repository > Files > Lock) and repeat. Merge requests changing that file by another author report
locked_paths, as before.
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
CheckPathLocksService#cacheable?(EE) andCheckLfsFileLocksService#cacheable?(CE) return false when the flag is on and either the check is inactive (nofile_lockslicence or target is not the default branch; LFS disabled) or the project has no locks.RunChecksService#run_checkthen callsexecutedirectly, which answersinactiveorsuccesswithout Redis.- Lock presence for
cacheable?comes frompath_locks_present_in_request?/lfs_file_locks_present_in_request?:project.path_locks.exists?/project.lfs_file_locks.any?memoized inGitlab::SafeRequestStoreper project id, so a page with many merge requests of one project queries once. executereuses that request-wide answer only whencacheable?fetched it on the same check instance (strong_memoized?).RunChecksServicecallscacheable?only whenuse_cacheis true, so merges (use_cache: false) never read the request memo and query locks fresh. This follows the review on !263468 (merged) (approval check), where a memoized read on the merge path was raised as a blocker.- Projects with locks keep the existing cache key (merge request id, diff head sha, lock epoch) and TTL.
- Flag off:
masterbehaviour, apart from theFeature.enabled?call.path_locks_present?runs the same singleexists?querycontains_locked_paths?ran inline.
Before and after per merge request
- Inactive check: before 1 Redis GET of a constant key (plus a SET when cold); after none.
- Active check, project without locks: before 1 Redis GET for the lock epoch (not memoized) plus 1 GET for the cached result, and on a miss 1 SQL plus a SET; after 1 SQL per project per request.
- Active check, project with locks: 1 extra SQL per project per request to decide, then today's cached path.
- Merge (
use_cache: false): unchanged, 1 SQL per check.
Staleness
The request-scoped memo means a lock created during the same request is not seen by later cached checks in that request. Lock and unlock requests (PathLocks::LockService, Lfs::LockFileService and their unlock counterparts) do not run mergeability checks afterwards, today's epoch-keyed cache already relies on a per-request epoch read, and merges do not use the memo.
Trade-off and possible follow-up
The change swaps per-merge-request Redis reads for one indexed EXISTS per project per page. On GitLab.com that is about 5 extra replica queries per request on average for this endpoint, about 5% more than it runs today. A follow-up could batch lock presence for every project on the page into one query per lock type (for example from the project ids Gitlab::Git::RefPreloader already collects), which would remove the added queries. It is left out to keep this change small; the per-project version is already a net win.
Verification
ee/spec/services/merge_requests/mergeability/check_path_locks_service_spec.rbandspec/services/merge_requests/mergeability/check_lfs_file_locks_service_spec.rb:#cacheable?for locked, lock-free and inactive cases, flag off, a request-store example asserting zero queries for a second check of the same project, and an example where a cached check earlier in the request found no locks, a lock is then created, andexecutestill reportsfailed.- Mutation runs: making
executeread the request memo unconditionally fails the newfailedexamples in both files. - All 44 examples pass locally on
masteras of 2026-10-29. RuboCop clean.
Production context
Kibana, GET /api/:version/groups/:id/merge_requests, one hour on 2026-10-29:
path_locksandlfs_file_locksare about 2.1% and 2.0% of the endpoint's request time. Inactive results are only about 6% of that time, so almost all of the gain is from lock-free projects.path_locksis active (licensed, default-branch target) for about 80% of checks andlfs_file_locksfor about 97%.- From a sample of requests, using
mergeability_project_idon the per-MR lines: about 2.3 active LFS checks and 2.0 active path lock checks per project per request, costing about 4.6 ms and 5.1 ms per project today. Redis cache calls on this endpoint average about 0.75 ms each. - The per-MR check timings come from the
application_jsonlines that share the request'scorrelation_id(mergeability.<check>.duration_s.values).
This is one of four changes from the investigation in #631601, each behind its own gitlab_com_derisk flag. !263467 (merged) (security policy check) and !263468 (merged) (approval check) are merged; the conflict check change is !266181 (merged).