SlideShare a Scribd company logo
@alexsnaps @ljacomet#Devoxx #cache101
Caching 101
Caching on the JVM 

and beyond
alex snaps - principal software engineer at terracotta
louis jacomet - lead software engineer at terracotta
@alexsnaps @ljacomet#Devoxx #cache101
Agenda
•Theory
•Caching patterns
•Scaling
@alexsnaps @ljacomet#Devoxx #cache101
Introductory Poll
•Who knows nothing about caching?
•Who already uses caching in production?
•Who had caching related problems in
production?
•Who is interested in advanced caching patterns?
@alexsnaps @ljacomet#Devoxx #cache101
(Caching) theoryhttp://blog.spylight.com/sheldons-big-bang-theory-style/
@alexsnaps @ljacomet#Devoxx #cache101
There are only two hard things
in Computer Science: 

cache invalidation and naming
things.
-- Phil Karlton
@alexsnaps @ljacomet#Devoxx #cache101
CPU Caches
https://en.wikipedia.org/wiki/Non-uniform_memory_access
@alexsnaps @ljacomet#Devoxx #cache101
Cache coherence
• P write A to X, P read X => returns A when no writes
happened in between, must be valid for single processor too
• P2 writes A to X, P1 reads X => returns A if “enough time” has
elapsed
• P1 writes A to X, P2 writes B to X => no one can ever see B
then A at X
@alexsnaps @ljacomet#Devoxx #cache101
Why?
http://www.eecs.berkeley.edu/~rcs/research/interactive_latency.html
@alexsnaps @ljacomet#Devoxx #cache101
Numbers explained
L1 cache reference 1 s 2 heartbeats
Branch mispredict 3 s Yawn
L2 cache reference 4 s (Longer) Yawn
Mutex lock / unlock 17 s Coffee preparation
Main memory reference 100 s Brushing your teeth
Send 2k over commodity network 4.2 s A song
@alexsnaps @ljacomet#Devoxx #cache101
Numbers explained
Compress 1kB with zippy 33 m Sitcom episode
Read 1MB from memory 2 h Bad commute
SSD random read 4 h Half day at work
Read 1MB from SSD 2.6 d Long week-end
Round trip in datacenter 10 d Two weeks delivery
Packet roundtrip CA to Be 4.8 y PhD thesis
@alexsnaps @ljacomet#Devoxx #cache101
http://yourstory.com/2015/05/performance-matrix/
@alexsnaps @ljacomet#Devoxx #cache101
Amdhal’s law
•Parallelisation
•P: parallel proportion
•N: processor count
@alexsnaps @ljacomet#Devoxx #cache101
Amdhal’s law (cont’d)
•Sequential
•p: times a part was sped up
•f: fraction of time not
improved
@alexsnaps @ljacomet#Devoxx #cache101
Further reading
• Gustafson’s law states that
computations involving
arbitrarily large data sets
can be efficiently
parallelized.
• As computing powers
grow, larger problems can
be solved in a given time
@alexsnaps @ljacomet#Devoxx #cache101
Where and when to use a cache?
https://www.flickr.com/photos/wwarby/
It depends …
Measure, do not guess!
@alexsnaps @ljacomet#Devoxx #cache101
What is a cache in an application?
• Data structure holding a temporary copy of some data
• Trade off between higher memory usage for reduced latency
• Targets:
• Data which is reused
• Data which is expensive to compute or retrieve
@alexsnaps @ljacomet#Devoxx #cache101
Desired cache features
• Capacity control / eviction
• Data freshness / expiry
• Data consistency / invalidation
• Fault tolerant
@alexsnaps @ljacomet#Devoxx #cache101
@alexsnaps @ljacomet#Devoxx #cache101
JSR 107
• Java Community Process driven standard
• Specifies API and semantics for temporary, 

in-memory caching of Java objects, including object creation,
shared access, spooling, invalidation, and consistency across JVM's
@alexsnaps @ljacomet#Devoxx #cache101
javax.cache API
• CacheManagers managing … Caches !
• Expiry
• Integration
• Cache Entry Listeners
• Entry Processors
• Caching Annotations
• Some Management
@alexsnaps @ljacomet#Devoxx #cache101
CacheManager
• Caching.getCachingProvider() static method
•CachingProvider.getCacheManager()
• CachingProvider
• CacheManager repository
• Identified by name & ClassLoader
@alexsnaps @ljacomet#Devoxx #cache101
CacheManager
•CacheManager
• Manages Cache lifecycle:
• Create & Destroy
• Repository of (named) Cache instances
• Manages Cache JMX (stats & management)
• … is .unwrap()’ able
@alexsnaps @ljacomet#Devoxx #cache101
Cache aside - miss
Application
Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Cache aside - hit
Application
Cache
Database
Demo
@alexsnaps @ljacomet#Devoxx #cache101
@alexsnaps @ljacomet#Devoxx #cache101
Cache aside conclusions
• Solution most seen out there: Spring, Play, Grails, …
• Most often based on annotations
• Tricky to get the concurrency and / or atomicity right
• Especially when rolling your own
• Does not resolve doing multiple real invocations when warming cache
@alexsnaps @ljacomet#Devoxx #cache101
Hibernate
boat1 = session.get( Boat.class, id );
boat2 = session.get( Boat.class, id );
boat1 == boat2 ?
@alexsnaps @ljacomet#Devoxx #cache101
Hibernate 2nd level cache
• Stores “dehydrated” entity states
• hibernate.cache.use_structured_entries
• 4 strategies
• ReadOnly
• ReadWrite
• NonStrictReadWrite
• Transactional
@alexsnaps @ljacomet#Devoxx #cache101
Cache through - miss
Application Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Cache through - hit
Application Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Cache through - write
Application Cache
Database
Demo
@alexsnaps @ljacomet#Devoxx #cache101
@alexsnaps @ljacomet#Devoxx #cache101
Cache through conclusions
• Requires different abstraction / modelling
• Viewing the system of record through the cache may not be easy
• Provides better guarantees and consistency as invalidation is no
longer required
• Still falls apart if data is modified by other applications / processes
• Cost of writing is paid by thread putting in the cache
@alexsnaps @ljacomet#Devoxx #cache101
Application Cache
Database
queue
Write behind
@alexsnaps @ljacomet#Devoxx #cache101
Application Cache
Database
queue
Write behind - eviction
(K1,V1)
(K1,V1)
@alexsnaps @ljacomet#Devoxx #cache101
Write behind - failure
Application Cache
queue
Database
@alexsnaps @ljacomet#Devoxx #cache101
Write behind - failure
queue
Database
Demo
@alexsnaps @ljacomet#Devoxx #cache101
@alexsnaps @ljacomet#Devoxx #cache101
Write behind conclusions
• Scales out your writes
• Batching and coalescing
• Persistent queue or not
• Idempotent operations are important in distributed and failure
conditions
@alexsnaps @ljacomet#Devoxx #cache101
Refresh ahead
Application Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Refresh ahead
Application Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Scheduled refresh
Application Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Scheduled refresh
Application Cache
Database
@alexsnaps @ljacomet#Devoxx #cache101
Scaling your cache
@alexsnaps @ljacomet#Devoxx #cache101
Moving off heap
• JVMs have issues with large heaps due to garbage collection
• OffHeap is a nice alternative for data which has a well known
lifecycle
• Perfect match for cache data
• Performance hit due to binary representation compared to
objects on heap
@alexsnaps @ljacomet#Devoxx #cache101
Want to know
more?
Talk on Thursday at 5.50pm room 10
by Chris Dennis (Terracotta)
@alexsnaps @ljacomet#Devoxx #cache101
Clustering
• Cache shared between multiple machines
• Different topologies
• Peer to peer
• Client server - with or without client cache
• Consistency becomes a much harder problem
@alexsnaps @ljacomet#Devoxx #cache101
Terracotta clustering
@alexsnaps @ljacomet#Devoxx #cache101
Strong consistency
Cache
Terracotta
client
Terracotta
server
Terracotta
client
Terracotta
client
@alexsnaps @ljacomet#Devoxx #cache101
Eventual consistency
Cache
Terracotta
client
Terracotta
server
Terracotta
client
Terracotta
client
@alexsnaps @ljacomet#Devoxx #cache101
Probabilistically Bounded Staleness
• In how “long” will a write be “readable” by all ?
• How many “old versions” are still around ?
• Depending on
• Network delay
• Node processing time
• … delayed replication 

(e.g. batching)
• LinkedIn’s data stores returned consistent data 99.9 percent of the time
within 13.6 ms, and on SSDs (solid-state drives) within 1.63 ms.
@alexsnaps @ljacomet#Devoxx #cache101
Conclusion
• You know it all now!
• Don’t be afraid of caching
• Consider it early on
• Topologies!
• Thanks! Questions?

More Related Content

PPTX
Delphix Workflow for SQL Server
PPT
Delphix for DBAs by Jonathan Lewis
PPT
Jonathan Lewis explains Delphix
PPTX
Kscope 2013 delphix
PDF
The Highs and Lows of Stateful Containers
PPTX
Transforming IT Infrastructure
PDF
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 1
PDF
Where Django Caching Bust at the Seams
Delphix Workflow for SQL Server
Delphix for DBAs by Jonathan Lewis
Jonathan Lewis explains Delphix
Kscope 2013 delphix
The Highs and Lows of Stateful Containers
Transforming IT Infrastructure
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 1
Where Django Caching Bust at the Seams

What's hot (20)

PDF
Ehcache 3 @ BruJUG
PDF
Priming Your Teams For Microservice Deployment to the Cloud
PDF
Taming the Cloud Database with Apache jclouds
PPTX
Oracle Database on Docker - Best Practices
PPTX
DevOps tools for winning agility
PPTX
Why Play Framework is fast
PPTX
War of the Indices- SQL vs. Oracle
PDF
Delphix database virtualization v1.0
PDF
Hotsos Advanced Linux Tools
PDF
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
PPTX
Production Debugging War Stories
PDF
Scaling Your Cache
PPT
SparkSQL et Cassandra - Tool In Action Devoxx 2015
PPTX
PASS 24HOP Linux Scripting Tips and Tricks
PDF
Play Framework + Docker + CircleCI + AWS + EC2 Container Service
PDF
#WeSpeakLinux Session
PDF
Play Framework: Intro & High-Level Overview
PDF
Dropwizard and Groovy
PDF
DevOps and Decoys How to Build a Successful Microsoft DevOps Including the Data
PDF
12-factor-jruby
Ehcache 3 @ BruJUG
Priming Your Teams For Microservice Deployment to the Cloud
Taming the Cloud Database with Apache jclouds
Oracle Database on Docker - Best Practices
DevOps tools for winning agility
Why Play Framework is fast
War of the Indices- SQL vs. Oracle
Delphix database virtualization v1.0
Hotsos Advanced Linux Tools
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Production Debugging War Stories
Scaling Your Cache
SparkSQL et Cassandra - Tool In Action Devoxx 2015
PASS 24HOP Linux Scripting Tips and Tricks
Play Framework + Docker + CircleCI + AWS + EC2 Container Service
#WeSpeakLinux Session
Play Framework: Intro & High-Level Overview
Dropwizard and Groovy
DevOps and Decoys How to Build a Successful Microsoft DevOps Including the Data
12-factor-jruby
Ad

Similar to Caching 101: Caching on the JVM (and beyond) (20)

PDF
Caching 101: Caching on the JVM (and beyond)
PDF
Caching 101: sur la JVM et au delà
PDF
Ehcache 3: JSR-107 on steroids at Devoxx Morocco
PDF
Don’t give up, You can... Cache!
PPTX
From cache to in-memory data grid. Introduction to Hazelcast.
PPTX
Cache Rules Everything Around Me - Momentum - October 2022.pptx
PDF
Caching in applications still matters
PPTX
Jug Lugano - Scale over the limits
PPTX
CREAM - That Conference Austin - January 2024.pptx
PPTX
Cache Rules Everything Around Me - DevIntersection - December 2022
PDF
JCON World 2023 - Cache, but Cache Wisely.pdf
PDF
Overview of the ehcache
PPTX
Cache-Aside Cloud Design Pattern
PDF
Data consistency: Analyse, understand and decide
PDF
Ehcache Architecture, Features And Usage Patterns
PPTX
Selecting the right cache framework
PPTX
Distributed Cache with dot microservices
PDF
Caching principles-solutions
PDF
Caching reboot: javax.cache & Ehcache 3
PPTX
From distributed caches to in-memory data grids
Caching 101: Caching on the JVM (and beyond)
Caching 101: sur la JVM et au delà
Ehcache 3: JSR-107 on steroids at Devoxx Morocco
Don’t give up, You can... Cache!
From cache to in-memory data grid. Introduction to Hazelcast.
Cache Rules Everything Around Me - Momentum - October 2022.pptx
Caching in applications still matters
Jug Lugano - Scale over the limits
CREAM - That Conference Austin - January 2024.pptx
Cache Rules Everything Around Me - DevIntersection - December 2022
JCON World 2023 - Cache, but Cache Wisely.pdf
Overview of the ehcache
Cache-Aside Cloud Design Pattern
Data consistency: Analyse, understand and decide
Ehcache Architecture, Features And Usage Patterns
Selecting the right cache framework
Distributed Cache with dot microservices
Caching principles-solutions
Caching reboot: javax.cache & Ehcache 3
From distributed caches to in-memory data grids
Ad

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
medical staffing services at VALiNTRY
PPTX
Essential Infomation Tech presentation.pptx
PDF
System and Network Administraation Chapter 3
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
AI in Product Development-omnex systems
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Nekopoi APK 2025 free lastest update
PPTX
Reimagine Home Health with the Power of Agentic AI​
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
medical staffing services at VALiNTRY
Essential Infomation Tech presentation.pptx
System and Network Administraation Chapter 3
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo POS Development Services by CandidRoot Solutions
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Digital Strategies for Manufacturing Companies
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
AI in Product Development-omnex systems
Design an Analysis of Algorithms II-SECS-1021-03
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms I-SECS-1021-03
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Nekopoi APK 2025 free lastest update
Reimagine Home Health with the Power of Agentic AI​

Caching 101: Caching on the JVM (and beyond)