# Hibernate 2nd level cache with Redis shared amongst multiple JVMS

**URL:** <https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120>\
**Category:** Hibernate ORM\
**Created:** [February 26, 2021, 6:10pm UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120 "2021-02-26T18:10:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikas\_Verma](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vikas_verma/32/1414_2.png) [@Vikas\_Verma](https://discourse.hibernate.org/u/Vikas_Verma)\
**Post date:** [February 26, 2021, 6:10pm UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120/1 "2021-02-26T18:10:34Z")

</div>

I have to use Redis as 2nd level cache in my jvm application. There are multiple instances of the jvms application and all have to use same 2nd level cache instance of redis.  
Is it supported by hibernate to share common 2nd level cache across JVMs.  
If yes then how data update in one jvm is reflected in other jvms.

---

<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:** [March 1, 2021, 8:39am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120/2 "2021-03-01T08:39:09Z")

</div>

That’s the purpose of a 2nd level cache. Here is an article explaining how to setup redis as 2nd level cache: [Caching in Hibernate With Redis - DZone Java](https://dzone.com/articles/caching-in-hibernate-with-redis)

---

<div class="post-metadata">

**Author:** ![Vikas\_Verma](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vikas_verma/32/1414_2.png) [@Vikas\_Verma](https://discourse.hibernate.org/u/Vikas_Verma)\
**Post date:** [March 2, 2021, 8:05am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120/3 "2021-03-02T08:05:35Z")

</div>

Thanks @beikov . I am running multiple instance of microservices having hibernate as ORM and all are pointing to same instance of Redis as second level cache using Redisson library.

But as the JVM goes down the cache gets cleared. Is there a property in hibernate that i can set to stop 2level cache clear on JVM shut down.

---

<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:** [March 2, 2021, 8:14am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120/4 "2021-03-02T08:14:20Z")

</div>

The second level cache literally is your Redis instance. There is no reason for Hibernate to clear that cache. I don’t know your setup, but I can tell you that this should work as you expect.

---

<div class="post-metadata">

**Author:** ![Vikas\_Verma](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vikas_verma/32/1414_2.png) [@Vikas\_Verma](https://discourse.hibernate.org/u/Vikas_Verma)\
**Post date:** [March 3, 2021, 9:39am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120/5 "2021-03-03T09:39:40Z")

</div>

@beikov. Shared second level cache is behaving as not expected in some scenario.

My set up is:

Multiple instances of same java application are running with shared redis 2nd level cache.  
There are 2 entites Employee with one to many with Items:

```auto
@Data
@Entity
@Cache(region = "employeeCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee {
	@Id
	private Long id;
	private String firstName;
	
	@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="employee_id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Items> items;

.....
}

```

I want both the entities to be in cache so added @Cache in the relationship.  
Now somehow Items table is update in background so i refresh the cache by calling below code in one of the instances of java applications.:

```auto
entityManagerFactory.getCache().evict(Items.class, id);
entityManagerFactory.getCache().evict(Employee.class, id);

```

Now when find the Employee using `employeeRepository.findById(id)` in the same java instance i get the correct data.

But when i find the same employee object in other java instance it fails with exception that item with id(which was removed) is not found.

How this can be tackled. There are multiple instances of the microservice sharing same cache. If one instance changes anything other doesn’t cope up.

---

<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:** [March 3, 2021, 9:56am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120/6 "2021-03-03T09:56:56Z")

</div>

> But when i find the same employee object in other java instance it fails with exception that item with id(which was removed) is not found.

What do you mean by removed? If you remove an entity/row, you will obviously not be able to retrieve that anymore as Hibernate will also remove the element from the cache automatically. If by remove you mean evict from the cache, then you will have to ask the maintainers of the Redis second level cache implementation about this. What exception do you get? Usually, when the second level cache does not contain the element Hibernate queries the database. So either your expectation as to how caching should work is wrong or there is a problem in the Redis second level cache implementation.
