What's the difference between L1 and L2 caches in web-applications with Hibernate as ORM mechanism?

18,035

Solution 1

L1 Cache is the cache that exists per Hibernate session, and this cache is not shared among threads. This cache makes use of Hibernate's own caching.

L2 Cache is a cache that survives beyond a Hibernate session, and can be shared among threads. For this cache you can use either a caching implementation that comes with Hibernate like EHCache or something else like JBossCache2

Solution 2

In JPA/Hibernate (and other similar ORM tools), the L1 cache is the transactional cache i.e. the entities stored from when you open a transaction to when you close it. This is almost never a shared cache (other threads can't make use of it). In JPA, this would usually be held by the EntityManager.

The L2 cache is a full (typically) shared cache. If you have multiple threads/queries pulling in data, then they can make use of entities that have already been retrieved by other threads that are still live in the cache. In JPA, this would usually be held by the EntityManagerFactory.

Solution 3

GaryF is not wrong, but is not technically right :-) Anton is more correct on this, but to complement his answer:

First Level Cache: this is a "cache" which stores all the entities known by a specific session. So, if you have 3 transactions inside this session, it'll hold all entities touched by all three transactions. It gets cleared when you close the session or when you perform the "clear" method.

Second Level Cache: this is a "real" cache and is delegated to an external provider, such as Infinispan. In this cache, you have full control over the contents of the cache, meaning that you are able to specify which entries should be evicted, which ones should be retained longer and so on.

Share:
18,035
Roman
Author by

Roman

Updated on July 05, 2022

Comments

  • Roman
    Roman almost 2 years

    I just want some general info about standard purpose of using L1 cache and L2 cache.

    I'm curious because I'm investigating the system with terracotta as 2nd level cache and I've found that it also has 1st-level cache.

  • KyleM
    KyleM about 11 years
    For the first level cache, what happens when the cache is full? You mentioned two cases where the cache gets cleared but I'm pretty sure it is automatically cleared according to a "last used" or similar algorithm when the cache gets filled up. Otherwise an out of memory exception would occur, or nothing would be storable in cache after that point.
  • jpkrohling
    jpkrohling about 11 years
    You might want to check this with the documentation or by experimenting yourself, but as far as I remember, entries on the first-level cache are never evicted. So, if you have a transaction affecting a huge amount of data, you might indeed face an Out of Memory exception (like in Batch operations). For this "edge" case, you might want to use a StatelessSession (docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/…).
  • v.ladynev
    v.ladynev almost 5 years
    "if you have 3 transactions inside this session" It is impossible for the separate (not nested transactions) because Session is not intend to be used with multiple threads.