BAMF4bacon
BAMF4bacon

Reputation: 593

How to use Ehcache 3 with persistent store to do write-behind caching?

I use the code below to set up a persistent cache with Ehcache3.

I would like to use a write-behind algorithm to try and speed up my writes. However, I have found no code example or guide to do this. The official ehcache documentation is lacking -- and when I sought help on their forum all I received was "we gotta fix this document!" I have found many examples of how to do it with caches that are not of type PersistentCacheManager, which do not work with the new class type.

There are some code examples out there, most notably ehcache write-behind behaviour But I am not certain which version of Ehcache this is using.

I am a little bit stuck here and it seems to me that I can't be the first person to want to write to a persistent store with write-behind behavior. I have found the meaning of "write-behind" to be slightly nebulous. What I want to do is to write to the disk in a chunking pattern instead of one at a time. My hope is that this would speed up writing speed when replacing existing data.

I can imagine that optimizing for write speed might cause something like file fragmentation on a HD -- there might be unused holes left in the file. I am hoping that there is prebuilt code to address these sorts of things. Maybe there are options around this, which I could use an additional tweak.

            PersistentCacheManager myCacheManager;
            myCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .with(CacheManagerBuilder.persistence(this.filename))
            .withCache("myCache",
                    CacheConfigurationBuilder.newCacheConfigurationBuilder(
                            Long.class, long[].class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder()
                                    .heap(100000, EntryUnit.ENTRIES)
                                    .offheap(1, MemoryUnit.GB)
                                    .disk(2000, MemoryUnit.GB, true)
                    )
            ).build(true);

Upvotes: 0

Views: 1537

Answers (1)

Louis Jacomet
Louis Jacomet

Reputation: 14500

As replied on the mailing list - more extensively than you indicate here - there is absolutely no relation between write-behind and PersistentCacheManager.

  • write-behind is an asynchronous extension of the write-through pattern in caching. Descriptions can be found in different places on the web, including on Wikipedia. You can enable write-behind on a heap only caches. It does not require a disk tier and will not in any way interfere with how the data is written to disk in the cache itself.
  • In order to implement write-behind you first need to support write-through by configuring a CacheLoaderWriter which will implement interactions with whatever acts as your reference data store (a DB, a ReST service, ...)

And yes, as admitted, the documentation can be improved on that topic. Note that aside from the configuration and the exact interface to implement, which combines readers and writers from Ehcache 2.x, high level documented principles remain valid.

So to get back to your confusion about the way disk writes are emitted from Ehcache - this is an implementation detail and in no way happens per mutation. Note that the Ehcache disk store is not crash proof and if you do not properly close the CacheManager data will be dumped on restart. This is not a feature that transforms a cache in a key-value store! And is definitely not a durability guarantee.

Upvotes: 1

Related Questions