Performance: Re-introduce lazy locks #21102
Draft
+3
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
LockingMechanismby switchingReadLockandWriteLockto use their lazy implementations instead of eager onesBackground
The codebase already had the concept of lazy vs eager locks, but locks were always being acquired eagerly. This caused unnecessary database queries even when the actual data was already cached at the repository level.
The typical pattern:
Even if the media is cached, the eager lock still hits the database. With lazy locks, the lock is only acquired if and when the database is actually accessed.
Benefits
Load Test Results
All tests performed after rebasing on PR #21105 (deadlock fix). Each configuration was tested 5 times to ensure consistency.
Test 1: Cache-Hit Stress Test (50 VUs, 1000 iterations, 100% reads)
Tests performance when serving cached data - the ideal scenario for lazy locks to shine.
SQL Server Detail:
Test 2: Write-Heavy Stress Test (50 VUs, 200 iterations, mixed workload)
Tests high-concurrency write operations (create content type → GET → create content → GET → publish).
SQL Server Detail:
SQLite Detail:
Key Findings
SQL Server sees consistent improvement - Both read-heavy (29% faster) and write-heavy (10% faster) scenarios benefit from lazy locks under high concurrency.
Performance gap increases under load - Higher VU counts (50 vs 20) showed more pronounced benefits from lazy locks.
SQLite benefits from writes, not reads - SQLite's single-writer lock model means cache-hit scenarios don't benefit as much, but write-heavy workloads still see 15% improvement.
Both configurations are stable - No deadlocks observed with either approach after the PR Content Publishing: Fix deadlocks by acquiring WriteLock at outer scope #21105 fix.
Test plan