Under The Hood
backend springboot Hibernate 6.x / Spring Boot 3.x

Hibernate First-Level (PersistenceContext) vs Second-Level Cache

Last updated
Prerequisites:
JPA entities and sessions
Understanding of caching and transactions
  • spring
  • hibernate
  • first-level-cache
  • second-level-cache
  • persistence-context
  • caching

Read at your depth

The practical view

Hibernate's first-level cache is the PersistenceContext (the Session/EntityManager): every entity loaded within a transaction is cached there, keyed by (entity class, id). Repeated loads of the same entity in the same transaction return the cached instance — no duplicate SQL. It's automatic and cleared when the session closes. The second-level cache (L2) is a shared, process-level cache (configured with a provider like Ehcache/Redis): entities cached in L2 are shared across sessions and can survive transaction boundaries, avoiding DB round trips for repeat reads. Enable with @Cacheable on entities (hibernate.cache.use_second_level_cache=true).

The same idea in other frameworks

react equivalent
N/A — server-side Hibernate concern.

Legacy vs modern

Repeated loads in a transaction vs L2-cached reads across transactions

L1 dedupes within one transaction automatically; L2 shares cached entities across transactions and sessions to avoid repeated DB reads.

before → after
Repeated find without L2
Order o1 = repo.findById(1L).orElseThrow(); // ❌ OLD: SQL
Order o2 = repo.findById(1L).orElseThrow(); // ❌ OLD: SQL again (no L2)
@Cacheable second-level cache
@Entity
@Cacheable // ✅ NEW: share this entity across sessions via L2
public class Order { ... }

Interview gotchas

Context

This tests the PersistenceContext identity-map semantics — interviewers want to distinguish the JPA-required identity guarantee from a cache meant for performance.

The mechanical answer

The first-level cache is the PersistenceContext (backed by the Session/EntityManager): it holds every entity loaded or persisted in the current transaction, keyed by entity type and id. Its primary role is the IDENTITY MAP: within one transaction, there is exactly one Java instance per (entity, id) — calling find twice returns the same object, and two loads never produce two conflicting instances of the same row. This identity guarantee is required by JPA (it's what makes the persistence context coherent). The performance benefit (avoiding duplicate SQL for repeated loads in the same transaction) is secondary. The cache is scoped to the transaction/session — it is cleared when the session closes, so it provides no cross-request benefit, which is why the second-level cache exists.

Trap

The naive answer is 'L1 is a cache to make reads faster'. Its essential job is identity/consistency within the transaction; the read-dedup is incidental. Another trap: assuming L1 persists across requests — it's bound to the session (transaction), so each request/transaction starts fresh. Candidates who mention the identity map and transaction scoping show real Hibernate understanding.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.