# Multiple L2 cache implementation support at one time

**URL:** https://discourse.hibernate.org/t/multiple-l2-cache-implementation-support-at-one-time/9401
**Category:** Hibernate ORM
**Created:** [April 22, 2024, 2:17pm UTC](https://discourse.hibernate.org/t/multiple-l2-cache-implementation-support-at-one-time/9401 "2024-04-22T14:17:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![amir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/amir/32/2415_2.png) [@amir](https://discourse.hibernate.org/u/amir)
#### Post date: [April 22, 2024, 2:17pm UTC](https://discourse.hibernate.org/t/multiple-l2-cache-implementation-support-at-one-time/9401/1 "2024-04-22T14:17:22Z")

</div>

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 🙂  
Maybe split it by 2…n datasources and configure them separately ?

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

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [April 23, 2024, 9:46am UTC](https://discourse.hibernate.org/t/multiple-l2-cache-implementation-support-at-one-time/9401/2 "2024-04-23T09:46:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![amir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/amir/32/2415_2.png) [@amir](https://discourse.hibernate.org/u/amir)
#### Post date: [April 23, 2024, 10:52am UTC](https://discourse.hibernate.org/t/multiple-l2-cache-implementation-support-at-one-time/9401/3 "2024-04-23T10:52:08Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [April 23, 2024, 11:43am UTC](https://discourse.hibernate.org/t/multiple-l2-cache-implementation-support-at-one-time/9401/4 "2024-04-23T11:43:19Z")

</div>

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](https://www.ehcache.org/documentation/3.10/xml.html)

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](https://docs.jboss.org/hibernate/orm/6.4/userguide/html_single/Hibernate_User_Guide.html#caching).
