SlideShare a Scribd company logo
DATA CACHING
Talk 10
CACHING: WHAT FOR?
• Spatial

optimization: original data is far and slow
to retrieve.	


• Temporal

compute.	


optimization: original data is heavy to

• Concurrency

optimization: many users request
the same data.
CACHE: WHAT?

Client
(CPU, web browser...)

Cache

Main
Repository
(RAM, disk, web, DB...)
LATENCY NUMBERS
!
!
!
!
!
!
!
!
!
!
!
!
!
Send packet CA->Netherlands->CA

150,000,000

https://gist.github.com/jboner/2841832

ns

150

ms
LATENCY NUMBERS
!
!
!
!
!
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

10,000,000

ns

10

ms!

150,000,000

ns

150

ms

https://gist.github.com/jboner/2841832
LATENCY NUMBERS
!
!
!
!
Main memory reference
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

100

ns!

10,000,000

ns

10

ms!

150,000,000

ns

150

ms

https://gist.github.com/jboner/2841832
LATENCY NUMBERS
!
!
L2 cache reference
!
Main memory reference
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

7

ns!

100

ns!

10,000,000

ns

10

ms!

150,000,000

ns

150

ms

https://gist.github.com/jboner/2841832
LATENCY NUMBERS
L1 cache reference
!
L2 cache reference
!
Main memory reference
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

0.5 ns!
7

ns!

100

ns!

10,000,000

ns

10

ms!

150,000,000

ns

150

ms

https://gist.github.com/jboner/2841832
LATENCY NUMBERS
L1 cache reference
0.5
Branch mispredict
5
L2 cache reference
7
Mutex lock/unlock
25
Main memory reference
100
Compress 1K bytes with Google Snappy
3,000
Send 1K bytes over 1 Gbps network
10,000
Read 4K randomly from SSD
150,000
Read 1 MB sequentially from memory
250,000
Round trip within same datacenter
500,000
Read 1 MB sequentially from SSD
1,000,000
Disk seek
10,000,000
Read 1 MB sequentially from disk
20,000,000
Send packet CA->Netherlands->CA
150,000,000
https://gist.github.com/jboner/2841832

ns!
ns!
ns!
ns!
ns!
ns!
ns
0.01
ns
0.15
ns
0.25
ns
0.5
ns
1
ns
10
ns
20
ns 150

ms!
ms!
ms!
ms!
ms!
ms!
ms!
ms
CACHE COHERENCE

• Cache

data.	


hit: the cache content must be in sync with the original

• Proxy
• DNS

ask to the original source if their data is up-to-date.	


cache provide stale data until expiration.
CACHE EVICTION

• Cache

miss: the cache eventually gets full. What can be
discarded?	

• LRU

(Last Recently Used)	


• LFU

(Least Frequently Used)
WRITE POLICY
•I

changed the data and put it back into the cache.	


• Write-through: the

data is synchronously pushed to the

original source.	


• Write

back: the data is pushed to the source only when it
is requested.

Client

Cache

Main
Repository
CACHE: THE BBOX WAY
Windows

Client

Controllers
BOs
Server
DAOs
Database
CACHE: THE BBOX WAY
Windows
Controllers
BOs
DAOs
Database

Application cache
Query cache
Second level cache
First level cache
DBMS cache
HIBERNATE
FIRST LEVEL CACHE

• Completely
• Is

automatic and not tunable.	


flushed at the session’s end (simplifying: at the
end of the transaction).
HIBERNATE
SECOND LEVEL CACHE
@Entity!
@Indexed!
@Table(name = "BOM_PLACE")!
@Cacheable!
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)!
public class PlaceImpl extends ThingImpl<PlaceImpl, PlaceTO,
PlaceStrategy> implements Place {!
...!

• The

cache survives the sessions.	


• The

cache is held and configured by EhCache.
HIBERNATE
QUERY CACHE
@CacheIt!
@Dao(entity = PlaceImpl.class)!
public interface PlaceImplDao extends GenericDao<PlaceImpl, Long> {!
!
@CacheIt!
List<PlaceTO> findTO();!
!
...!
!
!
!
Criteria c = sessionFactory.getCurrentSession().getCriteria(...);!
c.setCacheable(true);!

Works together with the Second level cache.
BBOX APPLICATION CACHE
/**!
* @author Federico Russo!
*/!
public interface PlaceBO extends ThingBO<Place, PlaceStrategy, PlaceTO> {!

!

!

/**!
* Ritorna tutti i figli di un certo TO.!
*/!
@PreAuthorize("hasRole('ROLE_READ_PLACES') and hasPermission(#parentTO, 'READ')")!
@PostFilter("hasPermission(filterObject, 'READ')")!
@Cacheable(cacheName = "places")!
Collection<PlaceTO> findChildrenTO(PlaceTO parentTO, String strategyClassName);!
/**!
* Rimuove il Component fornito dai magazzini in cui eventualmente sta.!
*/!
@Transactional!
@PreAuthorize("hasRole('ROLE_WRITE_PLACES')")!
@TriggersRemove(removeAll = true, cacheName = "places")!
void removeFromWarehouses(ComponentTO componentTO);!

We’re not using it. We should.
Next talk: Welcome to Java 7

More Related Content

PDF
A quick intro to Ansible
PDF
Automation and Ansible
PPTX
WP-CLI Workshop at WordPress Meetup Cluj-Napoca
PDF
Ansible + WordPress
PPTX
Best practices for ansible
PPTX
Ansible: How to Get More Sleep and Require Less Coffee
PPTX
PDF
Deploying PHP Applications with Ansible
A quick intro to Ansible
Automation and Ansible
WP-CLI Workshop at WordPress Meetup Cluj-Napoca
Ansible + WordPress
Best practices for ansible
Ansible: How to Get More Sleep and Require Less Coffee
Deploying PHP Applications with Ansible

What's hot (20)

PPTX
HBaseConEast2016: Practical Kerberos with Apache HBase
PDF
Testing with Ansible
PDF
Ansible 101
PDF
Jenkins and ansible reference
PDF
Network Automation: Ansible 102
PDF
Getting started with Ansible
PPT
Python Deployment with Fabric
PPT
Speeding Up The Snail
PDF
Ansible 101 - Presentation at Ansible STL Meetup
PDF
Ansible + WordPress - WordCamp Toronto 2016
PDF
Using Capifony for Symfony apps deployment (updated)
PDF
Scaling PHP web apps
PDF
Getting Started with Ansible
PDF
Managing development environments with Docksal
PDF
Debugging webOS applications
PPTX
Node.js
PPTX
Webdevcon Keynote hh-2012-09-18
ODP
Multi tenant CMSes using php
PPTX
Drupal, varnish, esi - Toulouse November 2
PPTX
Introduction to ansible
HBaseConEast2016: Practical Kerberos with Apache HBase
Testing with Ansible
Ansible 101
Jenkins and ansible reference
Network Automation: Ansible 102
Getting started with Ansible
Python Deployment with Fabric
Speeding Up The Snail
Ansible 101 - Presentation at Ansible STL Meetup
Ansible + WordPress - WordCamp Toronto 2016
Using Capifony for Symfony apps deployment (updated)
Scaling PHP web apps
Getting Started with Ansible
Managing development environments with Docksal
Debugging webOS applications
Node.js
Webdevcon Keynote hh-2012-09-18
Multi tenant CMSes using php
Drupal, varnish, esi - Toulouse November 2
Introduction to ansible
Ad

Similar to 10 Data caching (20)

PDF
Globo.com & Varnish
PPTX
EECI 2013 - ExpressionEngine Performance & Optimization - Laying a Solid Foun...
PPTX
Accelerating Rails with edge caching
PPT
Roy foubister (hosting high traffic sites on a tight budget)
PDF
Top ten-list
PDF
Caching with Memcached and APC
KEY
Site Performance - From Pinto to Ferrari
KEY
CHI - YAPC NA 2012
PDF
Large-scale Web Apps @ Pinterest
PDF
Maximize your Cache for No Cash
PDF
Shadow forensics print
PDF
Tuning Your SharePoint Environment
ODP
Caching and tuning fun for high scalability
PDF
High Performance With Java
PDF
Scaling Servers and Storage for Film Assets
PDF
Hyperbatch (LoteRapido) - Punta Dreamin' 2017
KEY
Introduction to memcached
PDF
Rails Caching: Secrets From the Edge
PDF
Rails Caching Secrets from the Edge
PPTX
Windows Azure Blob Storage
Globo.com & Varnish
EECI 2013 - ExpressionEngine Performance & Optimization - Laying a Solid Foun...
Accelerating Rails with edge caching
Roy foubister (hosting high traffic sites on a tight budget)
Top ten-list
Caching with Memcached and APC
Site Performance - From Pinto to Ferrari
CHI - YAPC NA 2012
Large-scale Web Apps @ Pinterest
Maximize your Cache for No Cash
Shadow forensics print
Tuning Your SharePoint Environment
Caching and tuning fun for high scalability
High Performance With Java
Scaling Servers and Storage for Film Assets
Hyperbatch (LoteRapido) - Punta Dreamin' 2017
Introduction to memcached
Rails Caching: Secrets From the Edge
Rails Caching Secrets from the Edge
Windows Azure Blob Storage
Ad

More from Federico Russo (20)

PDF
23 Sicurezza in BBox
PDF
21 Buzzwords
PDF
18 - InfluxDB
PDF
19 - The Highlander Project
PDF
22 - Better Coding
PDF
20 - Ottimizzare le query
PDF
17 - Web Application Threats
PDF
16 - Project Lombok
PDF
15 - Java 8
PDF
14 - Atom
PDF
Slides functionalities 0.26-r16
PDF
BBox e vaadin7
PDF
Box Functionalities 0.20
PDF
Tile server
PDF
PDF
12 java modifiers
PDF
11 Java 7
PDF
08 Workflow e strumenti web
PDF
06 Refactoring
PDF
09 Transactions
23 Sicurezza in BBox
21 Buzzwords
18 - InfluxDB
19 - The Highlander Project
22 - Better Coding
20 - Ottimizzare le query
17 - Web Application Threats
16 - Project Lombok
15 - Java 8
14 - Atom
Slides functionalities 0.26-r16
BBox e vaadin7
Box Functionalities 0.20
Tile server
12 java modifiers
11 Java 7
08 Workflow e strumenti web
06 Refactoring
09 Transactions

Recently uploaded (20)

PPTX
sap open course for s4hana steps from ECC to s4
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
sap open course for s4hana steps from ECC to s4
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MIND Revenue Release Quarter 2 2025 Press Release
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology

10 Data caching

  • 2. CACHING: WHAT FOR? • Spatial optimization: original data is far and slow to retrieve. • Temporal compute. optimization: original data is heavy to • Concurrency optimization: many users request the same data.
  • 3. CACHE: WHAT? Client (CPU, web browser...) Cache Main Repository (RAM, disk, web, DB...)
  • 4. LATENCY NUMBERS ! ! ! ! ! ! ! ! ! ! ! ! ! Send packet CA->Netherlands->CA 150,000,000 https://gist.github.com/jboner/2841832 ns 150 ms
  • 5. LATENCY NUMBERS ! ! ! ! ! ! ! ! ! ! ! Disk seek ! Send packet CA->Netherlands->CA 10,000,000 ns 10 ms! 150,000,000 ns 150 ms https://gist.github.com/jboner/2841832
  • 6. LATENCY NUMBERS ! ! ! ! Main memory reference ! ! ! ! ! ! Disk seek ! Send packet CA->Netherlands->CA 100 ns! 10,000,000 ns 10 ms! 150,000,000 ns 150 ms https://gist.github.com/jboner/2841832
  • 7. LATENCY NUMBERS ! ! L2 cache reference ! Main memory reference ! ! ! ! ! ! Disk seek ! Send packet CA->Netherlands->CA 7 ns! 100 ns! 10,000,000 ns 10 ms! 150,000,000 ns 150 ms https://gist.github.com/jboner/2841832
  • 8. LATENCY NUMBERS L1 cache reference ! L2 cache reference ! Main memory reference ! ! ! ! ! ! Disk seek ! Send packet CA->Netherlands->CA 0.5 ns! 7 ns! 100 ns! 10,000,000 ns 10 ms! 150,000,000 ns 150 ms https://gist.github.com/jboner/2841832
  • 9. LATENCY NUMBERS L1 cache reference 0.5 Branch mispredict 5 L2 cache reference 7 Mutex lock/unlock 25 Main memory reference 100 Compress 1K bytes with Google Snappy 3,000 Send 1K bytes over 1 Gbps network 10,000 Read 4K randomly from SSD 150,000 Read 1 MB sequentially from memory 250,000 Round trip within same datacenter 500,000 Read 1 MB sequentially from SSD 1,000,000 Disk seek 10,000,000 Read 1 MB sequentially from disk 20,000,000 Send packet CA->Netherlands->CA 150,000,000 https://gist.github.com/jboner/2841832 ns! ns! ns! ns! ns! ns! ns 0.01 ns 0.15 ns 0.25 ns 0.5 ns 1 ns 10 ns 20 ns 150 ms! ms! ms! ms! ms! ms! ms! ms
  • 10. CACHE COHERENCE • Cache data. hit: the cache content must be in sync with the original • Proxy • DNS ask to the original source if their data is up-to-date. cache provide stale data until expiration.
  • 11. CACHE EVICTION • Cache miss: the cache eventually gets full. What can be discarded? • LRU (Last Recently Used) • LFU (Least Frequently Used)
  • 12. WRITE POLICY •I changed the data and put it back into the cache. • Write-through: the data is synchronously pushed to the original source. • Write back: the data is pushed to the source only when it is requested. Client Cache Main Repository
  • 13. CACHE: THE BBOX WAY Windows Client Controllers BOs Server DAOs Database
  • 14. CACHE: THE BBOX WAY Windows Controllers BOs DAOs Database Application cache Query cache Second level cache First level cache DBMS cache
  • 15. HIBERNATE FIRST LEVEL CACHE • Completely • Is automatic and not tunable. flushed at the session’s end (simplifying: at the end of the transaction).
  • 16. HIBERNATE SECOND LEVEL CACHE @Entity! @Indexed! @Table(name = "BOM_PLACE")! @Cacheable! @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)! public class PlaceImpl extends ThingImpl<PlaceImpl, PlaceTO, PlaceStrategy> implements Place {! ...! • The cache survives the sessions. • The cache is held and configured by EhCache.
  • 17. HIBERNATE QUERY CACHE @CacheIt! @Dao(entity = PlaceImpl.class)! public interface PlaceImplDao extends GenericDao<PlaceImpl, Long> {! ! @CacheIt! List<PlaceTO> findTO();! ! ...! ! ! ! Criteria c = sessionFactory.getCurrentSession().getCriteria(...);! c.setCacheable(true);! Works together with the Second level cache.
  • 18. BBOX APPLICATION CACHE /**! * @author Federico Russo! */! public interface PlaceBO extends ThingBO<Place, PlaceStrategy, PlaceTO> {! ! ! /**! * Ritorna tutti i figli di un certo TO.! */! @PreAuthorize("hasRole('ROLE_READ_PLACES') and hasPermission(#parentTO, 'READ')")! @PostFilter("hasPermission(filterObject, 'READ')")! @Cacheable(cacheName = "places")! Collection<PlaceTO> findChildrenTO(PlaceTO parentTO, String strategyClassName);! /**! * Rimuove il Component fornito dai magazzini in cui eventualmente sta.! */! @Transactional! @PreAuthorize("hasRole('ROLE_WRITE_PLACES')")! @TriggersRemove(removeAll = true, cacheName = "places")! void removeFromWarehouses(ComponentTO componentTO);! We’re not using it. We should.
  • 19. Next talk: Welcome to Java 7