Spring Boot & Microservices (50 Qs)•Part 2: Persistence, Transactions & Security (11 - 24)
What are Hibernate First-Level and Second-Level Caches?
junior LevelMost AskedState ManagementSecurity & AuthSystem Design
In Simple Words:
- First-Level Cache (L1): Bound to the
EntityManager/Sessionlifecycle. Always enabled. If you query the same entity ID twice within the same transaction, Hibernate returns the in-memory reference without hitting the database. - Second-Level Cache (L2): Shared across all sessions and transactions at the
EntityManagerFactorylevel (e.g. Ehcache, Redis, Hazelcast). Caches entity data across multiple transactions. Disabled by default.