This is how I build my Caffeine Cache in Spring boot:
Caffeine.newBuilder()
.initialCapacity(200000)
.maximumSize(200000)
.expireAfterWrite(15, TimeUnit.MINUTES)
.scheduler(Scheduler.systemScheduler())
.executor(caffeineWriteClientConnectionPool(cacheSpec))
.recordStats(() -> caffeineMetrics)
.writer(new CacheWriter<Object, Object>() {});
I use the following Synchronous methods:
cache.getAllPresent() -> fetch all keys present in cache if not fallback to distributed cache/db and then add the key to the caffeine cache
cache.putAll() -> a key is added to the caffeine cache when its missed
When the cache load is high, I see get/put time increasing(>100ms) and this is making me think if these calls should be behind hystrix but I am think that's gonna be an overkill spinning up a thread for an in-memory search?
Also, I see the GC happening pretty often and I believe this might be impacting the read/write time. Any suggestions/best practices on setting up the GC or any other general suggestion I can do to improve the latency? thanks