EthanAlef
EthanAlef

Reputation: 75

Caffeine cache failed with worker threads

Caffeine cache value not read when multiple threads try to access it simultaneously

Quick repo

https://github.com/ethanalef/caffeine-cache-example
Try to run CacheTest::threadedTest() to replicate the problem

Aims

A costly IO method would be frequently accessed by multiple threads at the same time. Using L1 caching would a relief.

Situation

I have several classes for their purpose:
CaffeineConfig: Cache TTL = 300 seconds
CacheHelper: get the cached AtomicInteger, if value is get from cache, then the integer should not be incremented
Worker: work to get the atomicInteger
Manager: use ScheduledExecutorService to start the threading workers A, B, C to work
CacheTest: simulate the Manager class

Expected

When manager starts, first worker to get value = 0 and cached. second and third worker should get the value (=0) from the cache.

Actual

When manager starts, first worker to get value = 0 and cached. second and third worker get the incremented value rather than from the cache.

Findings

  1. I created another worker at sometime during the runtime, the value is indeed cached.
  2. The strange behavior only happens when threads try to get/access the value at the same time.
  3. From ChatGPT, it explains it might be that the cache is not shared among workers due to Threadlocal properties but I cannot understand; Bito also cannot provide reasonable answer.

Question

Does anyone have the same experience could share how should I do to fix this problem?

Upvotes: 0

Views: 215

Answers (0)

Related Questions