Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 744
Roman Avatar asked Dec 14 '10 10:12

Roman


People also ask

What is the difference between L1 and L2 cache Hibernate?

The main difference between the first level and second level cache in Hibernate is that the first level is maintained at the Session level and accessible only to the Session, while the second level cache is maintained at the SessionFactory level and available to all Sessions.

What is the difference between first level cache and second level cache in the Hibernate?

The first level cache is associated with the Session Object, while the second-level cache is associated with the SessionFactory object. This means first-level cache's scope is limited to session-level while second-level cache's scope is at the application level.

What is the difference between L1 L2 and L3 cache?

The main difference between L1 L2 and L3 cache is that L1 cache is the fastest cache memory and L3 cache is the slowest cache memory while L2 cache is slower than L1 cache but faster than L3 cache. Cache is a fast memory in the computer. It holds frequently used data by the CPU.

What is the use of L1 and L2 cache?

The first-level (L1) cache is small enough to provide a one- or two-cycle access time. The second-level (L2) cache is also built from SRAM but is larger, and therefore slower, than the L1 cache. The processor first looks for the data in the L1 cache. If the L1 cache misses, the processor looks in the L2 cache.


3 Answers

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

like image 195
Chandan Avatar answered Nov 15 '22 21:11

Chandan


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.

like image 37
jpkrohling Avatar answered Nov 15 '22 21:11

jpkrohling


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.

like image 32
GaryF Avatar answered Nov 15 '22 20:11

GaryF