Multiple L2 cache implementation support at one time

Hello there,
I’m wondering if there is already, or maybe some hints on how-to on the following thing:
Let’s say I have entities A, B, C, D
And I want to use for example ehCache for A, B and let’s say redis/any other “shared cache” for C and D.

like if I have a cluster of servers and enities A & B is kind of “static/rarely changing” data and C & D are user controlled entities that can change frequently during user interaction and so either session affinity is required or other defensive mechanism to prevent request bouncing between nodes in the cluster or having stale data in the cache in case request processed on node1 now is served by node2.

If this is not possible even with moderate customizations/overrides of hibernate, maybe there are other approaches other than rewrite legacy monster from scratch :slight_smile:
Maybe split it by 2…n datasources and configure them separately ?

P.S. please this question is not about system design etc.

With e.g. Infinispan, you can configure a cache for every entity region and hence us either a replicating, invalidating or TTL based cache, whatever works better for you.
I don’t know what other cache implementations support, but Hibernate ORM will create/request a Cache by a name that includes the entity name. You have to consult your cache implementation documentation for configuration details.

Either way, using two different cache implementations is an overkill and not possibly or IMO also not sensible. Usually, you can just configure a local + invalidation cache for some entities in your cache configuration and be done with it.

Thanks for your answer,

Either way, using two different cache implementations is an overkill and not possibly or IMO also not sensible.

Totally agree with that.

Usually, you can just configure a local + invalidation cache for some entities in your cache configuration and be done with it.

Can you please elaborate on this a bit more ?
Is the idea to find some cache impl that will provide you with option that region “A” is simple local in-memory “transparent” cache(like ehCache) and region “B” is setup as a “distributed cache like redis” ?
Thx for giving hint about infinispan I will take a look.
Just thinking about options to find balance between local & distributed cache to max benefits from both approaches…

I have no idea how EHCache or Redis are configured or work, but I guess you can configure multiple named caches through some sort of XML. See e.g. XML Configuration

For every entity, you can configure a region name with the org.hibernate.annotations.Cache annotation. The region name is the name of a cache that is created/requested by the Hibernate, so if you use e.g. @Cache(region = "local-cache") and configure a cache with that name in e.g. EHCache to your needs, the annotated entity will be stored in such a cache. Note that you can find all of this and much more information in the documentation.