A Beginner's Guide To Cache Synchronization Strategies: Vlad Mihalcea

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

Vlad Mihalcea

HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS

A beginner’s Let’s
connect

guide to Cache
synchronization
strategies
Last modified: Oct 26, 2020

Follow @vlad_mihalcea Find


Article

Imagine having a tool that can automatically detect if


Search …Go
you are using JPA and Hibernate properly.
Hypersistence Optimizer is that tool!

Introduction Book

A system of record is the authoritative data source when


information is scattered among various data providers.
When we introduce a caching solution, we automatically
duplicate our data. To avoid inconsistent reads and data
integrity issues, it’s very important to synchronize the
database and the cache (whenever a change occurs in the
Video
system). Course

Privacy
There& Cookies: This siteways
are various uses cookies.
to keep By continuing
the cache to use
andthisthe
website, you agree to their use.
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept
underlying database in sync and this article will present
some of the most common cache synchronization
strategies.
https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 1/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

Vlad Mihalcea
Cache-aside
HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS

The application code can manually manage both the


database and the cache information. The application logic
inspects the cache before hitting the database and it
updates the cache after any database modification.
Hypersistence
Optimizer

ERP
Contact

Mixing caching management and application is not very Online


Workshop
appealing, especially if we have to repeat these steps in
every data retrieval method. Leveraging an Aspect-
Oriented caching interceptor can mitigate the cache
leaking into the application code, but it doesn’t exonerate
us from making sure that both the database and the cache
are properly synchronized.
Consulting

Read-through
Instead of managing both the database and the cache, we
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
canoutsimply
To find delegate
more, including howthe database
to control cookies,synchronization toPolicy
see here: Our Cookie the Close and accept
cache provider. All data interaction is, therefore, done
through the cache abstraction layer.

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 2/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

Vlad Mihalcea

HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS

Limited
time offer:
Get 10 free
Adobe
Stock
images.

ADS VIA CARBO

Em
Upon fetching a cache entry, the Cache verifies the
cached element availability and loads the underlying Get
resource on our behalf. The application uses the cache as it
the system of record and the cache is able to auto- Now
populate on demand.

Write-through
Analogous to the read-through data fetching strategy, the
cache can update the underlying database every time a
cache entry is changed.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 3/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

Vlad Mihalcea

HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS

Although the database and the cache are updated


synchronously, we have the liberty of choosing the
transaction boundaries according to our current business
requirements.

If strong consistency is mandatory and the cache


provider offers an XAResource we can then enlist the
cache and the database in the same global transaction.
The database and the cache are therefore updated in a
single atomic unit-of-work

If consistency can be weaken, we can update the


cache and the database sequentially, without using a
global transaction. Usually the cache is changed first
and if the database update fails, the cache can use a

Privacy compensating
& Cookies: This siteaction to roll-back
uses cookies. the current
By continuing to use this website, you agree to their use.
transaction changes
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

Write-behind caching
https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 4/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

To speed
Vlad up the write operations, we can simply enqueue
Mihalcea
the cache changes and periodically flush them to the
database.
HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS

This strategy is employed by the Java Persistence


EntityManager (first-level cache), all entity state transitions
being flushed towards the end of the current running
transaction (or when a query is issued).

If you enjoyed this article, I bet you are going to love


my Book and Video Courses as well.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 5/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

To guarantee
Vlad Mihalcea strong consistency, the buffer must be
flushed prior to executing any query or read operation.

HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS


The advantage of the write-behind caching strategy is that
we can batch the database DML statements, therefore
improving transaction response time.

Follow @vlad_mihalcea

Enter your email address

DOWNLOAD NOW

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 6/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

Vlad Mihalcea
Related

High-Performance Things to consider How does


HOME BLOG
Java Persistence –STORE TRAINING
before jumping to CONSULTING
Hibernate TUTORIALS VIDEOS TALKS
Chapter 16 – application-level READ_WRITE
Caching caching CacheConcurren…
In "Hibernate" In "Hibernate" work
In "Hibernate"

Category: Hibernate  Tags: cache-aside, caching,


hibernate, read-through, Training, Tutorial, write-behind,
write-through

← Things to consider before jumping to application-level caching


How does Hibernate READ_ONLY CacheConcurrencyStrategy
work →

2 Comments on “A
beginner’s guide to
Cache synchronization
strategies”
Clement
October 2, 2020

Thank a lot for all the information you


share, you make me very interested
by the persistance layer now and made me bought
your book to fully understand what happen in this
layer.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Reply
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 7/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

Vlad Mihalcea
vladmihalcea
October 2, 2020
HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS
Thanks. I’m glad you like my articles.
Enjoy reading the book as well. It’s
even better than the articles.

Reply

Leave a Reply
Your email address will not be published. Required
fields are marked *

Comment

Before posting the comment, please take the


time to read the FAQ page

Name *

Email *

Website

NotifyThis
Privacy & Cookies: mesite
of uses
follow-up
cookies.comments
By continuing by email.
to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

Post Comment

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 8/9
11/6/2020 A beginner's guide to Cache synchronization strategies - Vlad Mihalcea

This site uses Akismet to reduce spam. Learn how


Vlad your
Mihalcea
comment data is processed.

HOME BLOG STORE TRAINING CONSULTING TUTORIALS VIDEOS TALKS

Tu t o r i a l s Social About Meta


Media

Hibernate About Log in


Twitter
SQL FAQ Entries feed
Facebook
Comments feed
Spring Archive
YouTube
WordPress.org
Git Privacy Policy
GitHub
FlexyPool Terms of Service
LinkedIn

Vlad Mihalcea
Powered by WordPress.com.      

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Our Cookie Policy Close and accept

https://vladmihalcea.com/a-beginners-guide-to-cache-synchronization-strategies/ 9/9

You might also like