lettuce icon indicating copy to clipboard operation
lettuce copied to clipboard

Enhance thread-safety in ClientSideCaching key retrieval

Open yybmion opened this issue 8 months ago • 3 comments

Issue

Closes #2402

The current implementation of the get method in the ClientSideCaching class does not provide a mechanism to ensure exclusive access by a single thread when retrieving a value from the Redis server for a specific key. As a result, in high concurrent load scenarios involving the same key, the current implementation may cause redundant and unnecessary calls to the Redis server.

Solution

Added a per-key locking mechanism using ReentrantLock to ensure that only a single thread can fetch a value from Redis for a specific key at any given time.

Tests

  1. valueLoaderShouldBeInvokedOnceForConcurrentRequests: Verifies that when multiple threads concurrently access the same key using a valueLoader, the loader is called exactly once

  2. locksShouldBeProperlyCleanedUp: Verifies the proper lifecycle management of locks (creation, cleanup on invalidation, recreation, and final cleanup)

yybmion avatar Apr 20 '25 13:04 yybmion

Hey @yybmion could you please format your changes using mvn formatter:format

tishun avatar May 07 '25 08:05 tishun

Hi @tishun. Thank you for pointing out the formatting issues - I was having trouble with CRLF/LF line endings. I've applied the formatting changes as requested using mvn formatter:format.

yybmion avatar May 07 '25 11:05 yybmion

Hi @tishun, Just a gentle reminder — PTAL when you have time.

yybmion avatar Aug 08 '25 14:08 yybmion