DATA CACHING
Talk 10
CACHING: WHAT FOR?
• Spatial

optimization: original data is far and slow
to retrieve.	


• Temporal

compute.	


optimiza...
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/284...
LATENCY NUMBERS
!
!
!
!
!
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

10,000,000

ns

10

ms!

150,000,000

n...
LATENCY NUMBERS
!
!
!
!
Main memory reference
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

100

ns!

10,000,00...
LATENCY NUMBERS
!
!
L2 cache reference
!
Main memory reference
!
!
!
!
!
!
Disk seek
!
Send packet CA->Netherlands->CA

7
...
LATENCY NUMBERS
L1 cache reference
!
L2 cache reference
!
Main memory reference
!
!
!
!
!
!
Disk seek
!
Send packet CA->Ne...
LATENCY NUMBERS
L1 cache reference
0.5
Branch mispredict
5
L2 cache reference
7
Mutex lock/unlock
25
Main memory reference...
CACHE COHERENCE

• Cache

data.	


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

• Proxy
• DNS

ask to the ori...
CACHE EVICTION

• Cache

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

• LRU

(Last Recently Used)	


• L...
WRITE POLICY
•I

changed the data and put it back into the cache.	


• Write-through: the

data is synchronously pushed to...
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 ca...
HIBERNATE
FIRST LEVEL CACHE

• Completely
• Is

automatic and not tunable.	


flushed at the session’s end (simplifying: at...
HIBERNATE
SECOND LEVEL CACHE
@Entity!
@Indexed!
@Table(name = "BOM_PLACE")!
@Cacheable!
@Cache(usage = CacheConcurrencyStr...
HIBERNATE
QUERY CACHE
@CacheIt!
@Dao(entity = PlaceImpl.class)!
public interface PlaceImplDao extends GenericDao<PlaceImpl...
BBOX APPLICATION CACHE
/**!
* @author Federico Russo!
*/!
public interface PlaceBO extends ThingBO<Place, PlaceStrategy, P...
Next talk: Welcome to Java 7
Upcoming SlideShare
Loading in...5
×

10 Data caching

585
-1

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
585
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

10 Data caching

  1. 1. DATA CACHING Talk 10
  2. 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. 3. CACHE: WHAT? Client (CPU, web browser...) Cache Main Repository (RAM, disk, web, DB...)
  4. 4. LATENCY NUMBERS ! ! ! ! ! ! ! ! ! ! ! ! ! Send packet CA->Netherlands->CA 150,000,000 https://gist.github.com/jboner/2841832 ns 150 ms
  5. 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. 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. 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. 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. 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. 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. 11. CACHE EVICTION • Cache miss: the cache eventually gets full. What can be discarded? • LRU (Last Recently Used) • LFU (Least Frequently Used)
  12. 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. 13. CACHE: THE BBOX WAY Windows Client Controllers BOs Server DAOs Database
  14. 14. CACHE: THE BBOX WAY Windows Controllers BOs DAOs Database Application cache Query cache Second level cache First level cache DBMS cache
  15. 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. 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. 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. 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. 19. Next talk: Welcome to Java 7
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×