trafficserver icon indicating copy to clipboard operation
trafficserver copied to clipboard

Add reader/writer locks to cache stripe for reduced contention

Open bryancall opened this issue 2 months ago • 0 comments

This commit implements reader/writer locks for cache directory operations to significantly reduce lock contention under high concurrency.

Changes:

  • Added ts::shared_mutex dir_mutex to StripeSM for directory operations
  • Created CacheDirSharedLock and CacheDirExclusiveLock RAII wrappers
  • Converted critical Cache.cc read paths to use shared locks for directory.probe()
  • Multiple readers can now access directory concurrently

Performance Impact:

  • Throughput: 17,520 req/s -> 44,218 req/s (+152%, 2.5x improvement)
  • Mean latency: 55.94ms -> 22.23ms (-60%, 2.5x faster)
  • Cache lock overhead: 42.81ms -> 9.10ms (-79%)

Test configuration: 1M requests, 1K concurrent clients, non-cacheable origin

This is a partial implementation covering Cache.cc read paths. Further optimization possible by converting CacheRead.cc and CacheWrite.cc.

Files modified:

  • src/iocore/cache/StripeSM.h: Added dir_mutex member
  • src/iocore/cache/P_CacheInternal.h: Added lock wrapper classes
  • src/iocore/cache/Cache.cc: Converted 3 critical paths to shared locks

Documentation:

  • CACHE_RWLOCK_ANALYSIS.md: Design analysis and implementation strategy
  • CACHE_RWLOCK_BENCHMARK_RESULTS.md: Detailed benchmark results and analysis

bryancall avatar Oct 22 '25 23:10 bryancall