-
I used caffeine cache (2.3.1) in my app, and I found the performance is not as good as expected. I used JVM async profiler to check, and found the following, which indicates ForkJoinPool.scan take 55.48% CPU. I used sized based eviction. Is there any way to reduce the overhead from ForkJoinPool?
My cache stats is:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The cache does not need FJP for its own operations and you can instead supply a caller-runs / direct executor, e.g. An executor is used for any async code that may call a foreign (user supplied) method, since we won't know how expensive that is. For example during eviction a listener can be supplied for notification within the atomic map computation that removes the entry. The cache's own operations are fast and O(1), but users might do something unexpectedly expensive. If done on the calling thread then it could impact user-facing latencies, so the executor helps hide that. Caffeine also offers an async cache where the computation occurs through a CompletableFuture. That requires an executor, so we pass through the one configured on the cache. Java defaults to Please also use the latest version of Caffeine, either 2.x (if Java 8) or 3.x (Java 11 or above). This will avoid you from suffering from known and fixed bugs, as that version is 6 years old. Similarly try to stay on a recent JDK for the same reason, e.g. there were bugs in FJP that impacted our users which we tried to mitigate and JDK fixes resolved. If an upgrade isn't possible then using a direct executor is probably an okay alternative. |
Beta Was this translation helpful? Give feedback.
The cache does not need FJP for its own operations and you can instead supply a caller-runs / direct executor, e.g.
Runnable::run
. That works fine for many users and our non-concurrent tests use that for simplicity.An executor is used for any async code that may call a foreign (user supplied) method, since we won't know how expensive that is. For example during eviction a listener can be supplied for notification within the atomic map computation that removes the entry. The cache's own operations are fast and O(1), but users might do something unexpectedly expensive. If done on the calling thread then it could impact user-facing latencies, so the executor helps hide that.
Caffeine also o…