# How to synchronize the Hibernate second-level cache with the database across multiple JVMs

**URL:** <https://discourse.hibernate.org/t/how-to-synchronize-the-hibernate-second-level-cache-with-the-database-across-multiple-jvms/2009>\
**Category:** Hibernate ORM\
**Created:** [January 10, 2019, 1:12pm UTC](https://discourse.hibernate.org/t/how-to-synchronize-the-hibernate-second-level-cache-with-the-database-across-multiple-jvms/2009 "2019-01-10T13:12:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiva](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/shiva/32/760_2.png) [@shiva](https://discourse.hibernate.org/u/shiva)\
**Post date:** [January 10, 2019, 1:12pm UTC](https://discourse.hibernate.org/t/how-to-synchronize-the-hibernate-second-level-cache-with-the-database-across-multiple-jvms/2009/1 "2019-01-10T13:12:11Z")

</div>

I have a stand alone java batch process which does load the database tables from third party soap service. We have now a requirement to create a new rest services to read the database.

We have created a caching for my second rest component. It works fine. However, the question is the standalone batch component updates the database every 10 minutes and how do we ensure that the cache in my rest service component is synchronized and up-to date.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 10, 2019, 1:28pm UTC](https://discourse.hibernate.org/t/how-to-synchronize-the-hibernate-second-level-cache-with-the-database-across-multiple-jvms/2009/2 "2019-01-10T13:28:05Z")

</div>

> I have a standalone Java batch process which does load the database tables from third-party soap service.

What database does it load? What does the Jaba batch process do with the data that it loads afterwards?

> We have now a requirement to create a new rest services to read the database.

What database are you talking about?

> We have created a caching for my second rest component.

Why did you create a cache? Are you using Hibernate? Maybe a second-level cache would work too?

> However, the question is the standalone batch component updates the database every 10 minutes and how do we ensure that the cache in my rest service component is synchronized and up-to date.

Again, are you using Hibernate?

---

<div class="post-metadata">

**Author:** ![shiva](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/shiva/32/760_2.png) [@shiva](https://discourse.hibernate.org/u/shiva)\
**Post date:** [January 10, 2019, 1:32pm UTC](https://discourse.hibernate.org/t/how-to-synchronize-the-hibernate-second-level-cache-with-the-database-across-multiple-jvms/2009/3 "2019-01-10T13:32:09Z")

</div>

1. Both the standalone batch process and rest service use Oracle database and Hibernate.
2. The standalone batch process query the soap service and if any updates, it only persists into the database.
3. Yes, I did configure the secondary level cache in both the components. However, these components are residing in different servers.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 10, 2019, 1:44pm UTC](https://discourse.hibernate.org/t/how-to-synchronize-the-hibernate-second-level-cache-with-the-database-across-multiple-jvms/2009/4 "2019-01-10T13:44:17Z")

</div>

Then you can use [Debezium](https://debezium.io/) to extract the [CDC (Change Data Capture) events](https://vladmihalcea.com/a-beginners-guide-to-cdc-change-data-capture/) and update the cache accordingly.

Check out [this article](https://debezium.io/blog/2018/12/05/automating-cache-invalidation-with-change-data-capture/) on the Debezium blog for more details about using this approach.
