Skip to content

perf: batch cache check to reduce lock contention in Check endpoint#382

Merged
yosiharan merged 5 commits intomainfrom
async-cache-update-after-check
Feb 18, 2026
Merged

perf: batch cache check to reduce lock contention in Check endpoint#382
yosiharan merged 5 commits intomainfrom
async-cache-update-after-check

Conversation

@yosiharan
Copy link
Contributor

@yosiharan yosiharan commented Feb 17, 2026

Related Issues

Resolves: https://github.com/descope/etc/issues/14193

Summary

Replaces the per-relation CheckRelation loop (which acquired RLock/RUnlock N times per request) with a single CheckRelations call that holds one RLock for the entire batch. This eliminates redundant lock acquisitions and significantly reduces contention under concurrent load.

Changes

  • Added CheckRelations(ctx, relations) batch method to ProjectAuthzCache interface and implementation — acquires a single RLock for all relation lookups
  • Removed CheckRelation (single-relation method) from the interface
  • Updated Check endpoint to use the new batch method
  • Updated mock and tests accordingly

Benchmark

Simulated concurrent load with 50% cache hits and 1ms SDK latency:

Relations per call ~ns/op
500 ~99,000
1000 ~180,000
5000 ~935,000

Prior to this change (with N separate RLock acquisitions), contention under concurrent load was ~34–40% higher at 1000+ relations, which could be the source of the latency mentioned in the parent issue

Copilot AI review requested due to automatic review settings February 17, 2026 17:42
@yosiharan yosiharan self-assigned this Feb 17, 2026
yosiharan

This comment was marked as outdated.

This comment was marked as outdated.

@yosiharan yosiharan force-pushed the async-cache-update-after-check branch from f727bd6 to 71606b8 Compare February 17, 2026 18:10
@yosiharan yosiharan changed the title perf: async cache update after Check perf: async cache update and batch cache check to reduce lock contention Feb 17, 2026
Two optimizations to reduce lock contention in the Check endpoint:

1. Async cache update: UpdateCacheWithChecks now runs in a background
   goroutine by default (configurable via AUTHZCACHE_ASYNC_CACHE_UPDATE),
   so the write lock doesn't block concurrent readers.

2. Batch cache check: New CheckRelations method acquires the read lock
   once for the entire batch instead of per-relation, eliminating N-1
   redundant lock acquire/release cycles.

Combined, these reduce p50 latency by ~54% for 5000-relation checks
under concurrent load.

Resolves descope/etc#14193

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@yosiharan yosiharan force-pushed the async-cache-update-after-check branch from 71606b8 to 9a99e02 Compare February 18, 2026 07:27
yosiharan

This comment was marked as outdated.

@yosiharan yosiharan enabled auto-merge (squash) February 18, 2026 09:15
@yosiharan yosiharan disabled auto-merge February 18, 2026 09:15
@yosiharan yosiharan enabled auto-merge (squash) February 18, 2026 09:15
@yosiharan yosiharan force-pushed the async-cache-update-after-check branch from d93abef to 676b986 Compare February 18, 2026 09:45
@yosiharan yosiharan changed the title perf: async cache update and batch cache check to reduce lock contention perf: batch cache check to reduce lock contention in Check endpoint Feb 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yosiharan yosiharan disabled auto-merge February 18, 2026 09:59
@yosiharan yosiharan enabled auto-merge (squash) February 18, 2026 10:00
@yosiharan yosiharan requested a review from orius123 February 18, 2026 10:04
@yosiharan yosiharan merged commit 48c3026 into main Feb 18, 2026
14 checks passed
@yosiharan yosiharan deleted the async-cache-update-after-check branch February 18, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants