Java cache with expiration since first write

782 views Asked by At

I have events which should be accumulated into persistent key-value store. After 24 hours after key first insert this accumulated record should be processed and remove from store. Expired data processing is distributed among multiple nodes, so use of database involves processing synchronization problems. I don't want to use any SQL database. The best fit for me is probably some cache with configurable expiration policy according to my needs. Is there any? Or can be this solved with some No-SQL database?

1

There are 1 answers

0
cruftex On

It should be possible with products like infinispan or hazelcast.

Both are JSR107 compatible.

With a JSR107 compatible cache API a possible approach is to set your 24h hours expiry via the CreatedExpiryPolicy. Next, you implement and register CacheEntryExpiredListener to get a call when the entry is expired.

The call on the CacheEntryExpiredListener may be lenient and implementation dependent. Actually the event is triggered on the "eviction due to expiry". For example, one implementation may do a peridoc scan and remove expired entries every 30 minutes. However I think that "lag time" is adjustible in most implementations, so you will be able to operate in defined bounds.

Also check whether there are some resource constraints for the event callbacks you may run into, like thread pools.

I mentioned infispan or hazelcast for two reasons:

  • You may need the distribution capabilities.
  • Since you do long running processing and store data that is not recoverable, you may need the persistence and fault tolerance features. So I would say a simple in memory cache like Google Guava is out of the scope.

Good luck!