简体   繁体   中英

Hibernate caching and database consistency

I have a question regarding Hibernate caching.

What I have understood is that Hibernate caching is used to avoid the frequently hitting the database. We therefore use Hibernate caching mechanism to gain performance.

If a new record is added to the database, when using caching, if we don't hit the database, how does the newly added record would be fetched?

Caching still fetches the old record right? can someone explain me how does this work?

Each Cache concurrency strategy has an associated cache synchronization mehcanism :

  1. NONSTRICT_READ_WRITE is a read-through cache, because entities are stored in cache when they are fetched from the database (and not when they are persisted). Updating an entity causes an entity cache entry invalidation.

  2. READ_WRITE is an asynchronous write-through cache strategy, because the database and the cache are not updated transactionally. Soft-locks are used to guarantee consistency.

  3. TRANSACTIONAL is a synchronous cache strategy, as both the database and the cache are updated atomically.

Hibernate favours strong consistency so both READ_WRITE and TRANSACTIONAL cache coherency is similar to the READ_COMMITTED isolation level . In NONSTRICT_READ_WRITE, stale records can can still occur.

Cache is a component that stores data so future requests for that data can be served faster; the data stored in a cache might be the results of an earlier computation, or the duplicates of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot.

.

in order to avoid the frequent hitting of database, we use hibernate caching mechanism to gain performace.

True.

if a new record has been added to the database, with caching,if we dont hit the database, how does the newly added record would be fetched?

We don't hit database in case of cache hit , however if there is cache miss , values will have to be fetched from database.

At initialization, cache is empty. Whenever you access record for first time, it won't be in cache. If you access a record, that record is first checked in cache, as cache is empty cache miss will occur. Due to cache miss, record will be fetched from database. Record that is fetched from database is put in cache, so that the next time you access the record cache hit occurs, and database is not accessed.

Note that there is limit to how much data/records can be stored in cache. With available limited storage, cache mostly use Least Recently Used algorithm to remove least recently used or the oldest accessed records from cache. So, even if you might have accessed a record previously, it can happen that it has been evicted from cache(cache miss). Database hit would be needed to again fetch that record. Yes, that record will again be added to database.

"If a new record is added to the database, when using caching, if we don't hit the database, how does the newly added record would be fetched?"

We will hit the database for the new record, because it will not be in the cache. Then it is stored in the cache and is ready to be served from there in subsequent requests.

"Caching still fetches the old record right?"

If you mean what happens when the row is updated, then Hibernate will update the row (disassembled entity) in the cache as well or invalidate it (depending on the cache concurrency strategy and cache topology in clustered environments).

However, if the row is updated in the database through another application without Hibernate being aware of that, then you are right, stale data will be served from the cache (until the stale entry expires or is evicted from the cache).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM