# 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:** 1\
**Showing post:** 5

<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.

---

_[View the full topic](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-with-redis-shared-amongst-multiple-jvms/5120)._
