Where does external caches like ehcache / redis fit w.r.t hibernate level 1 / level 2 caches?

469 views Asked by At

Hibernate (and so JPA which is wrapper on hibernate) provides two levels of caching mechanisms. a) level 1 cache which is at session object layer/level b) level 2 cache which is at session factory layer/level

If I am using external caches like redis or ehcache, where does this cache sit in the above. Or should I disable level 2 cache to enable redis/ehcache. Not sure how and where the external cache fits with level 1 & 2 caches.

Or - Are various configurations possible?

Could someone explain. Thanks in advance.

1

There are 1 answers

2
sonus21 On

Level 1 cache is considered a local in-memory cache, it can be a local Redis/Memcache cache as well. The 2nd level cache is like a proxy server that caches the results of a query and all subsequeuent queries from any server will provide the result from the cache if it's available.

Level 1 cache result is only available to one server, where as Level 2 cache can be considered as a distributed cache that's available to all servers. If your application is running on only one server/instance than it does not make sense to use Level 2 cache.

You can consider using proxy server as well for your database for example if you're using MySQL than you can consider using ProxySQL, mysql-proxy etc.