Search code examples
playframeworkehcachecaffeine-cache

What is the (default) Cache size limit in Play Framework when using Ehcache and/or Caffeine?


Recently I switched my Play Framework-based project from Ehcache to Caffeine, because the Play documentation for version 2.8 says "For in-process caching Caffeine is typically the best choice."

Now, I implemented a little "test" to see how many items can be added to the cache before some items are evicted. The test simply adds more and more items to the cache (without an explicit expiration) in a loop, checking whether all previosuly inserted items still are available after each insert operation, until at least one previously inserted item is detected to be missing.

With Ehcache I found that the limit apparently is 10,000 items. After adding that many items to the cache, some "old" items start to disappear from the cache. So my conclusion is that Ehcache, by default, has a fixed size limit of 10,000 items. With Caffeine, on the other hand, there seems to be no limit at all! I kept the test running for a very long time, but even after inserting ~1 million items, still no previously inserted items had been evicted. At that point I stopped the test.

So, does Caffeine, unlike Ehcache, not have a size limit by default? Will it keep on accumulating items until, eventually, my application crashes with "out of memory" error, or is there some logic in Caffeine that evicts items in "low memory" situations? Is it advisable to configure an explicit cache size limit when using Caffeine? I would think so. But then, why Play doesn't do it by default?


Unfortuantely, the Cache documentation of Play Framework doesn't make clear at all which default settings Play uses with Ehcache and/or Caffeine. Also, a list of available Cache options in the Play configuration (and the respecitive default values) would be really helpful...

Regards.


Solution

  • This appears to be the defaults set by Play's integrations.

    For ehcache, they have a configuration file ehcache-default.xml with a maximum of 10,000 entries and an expiration time of 120 seconds.

    For caffeine, they have a configuration file reference.conf that specifies no constraints. A maximum-size may be set to limit the number of entries.

    The Caffeine library does not have any implicit (arbitrary) defaults, as that might be surprising and most often be incorrect. If a size limit is specified, then the cache is allowed to grow slightly above the threshold in order to support concurrent writes (else all serializing against an exclusive lock), but won't suffer runaway growth due to applying back pressure.