10 Data caching

19
DATA CACHING Talk 10

Transcript of 10 Data caching

Page 1: 10 Data caching

DATA CACHINGTalk 10

Page 2: 10 Data caching

CACHING: WHAT FOR?

•Spatial optimization: original data is far and slow to retrieve.

•Temporal optimization: original data is heavy to compute.

•Concurrency optimization: many users request the same data.

Page 3: 10 Data caching

CACHE: WHAT?

Client CacheMain

Repository

(CPU, web browser...) (RAM, disk, web, DB...)

Page 4: 10 Data caching

LATENCY NUMBERS!!!!!!!!!!!!!Send packet CA->Netherlands->CA 150,000,000 ns 150 ms

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

Page 5: 10 Data caching

LATENCY NUMBERS!!!!!!!!!!!Disk seek 10,000,000 ns 10 ms!!Send packet CA->Netherlands->CA 150,000,000 ns 150 ms

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

Page 6: 10 Data caching

LATENCY NUMBERS!!!!Main memory reference 100 ns!!!!!!!Disk seek 10,000,000 ns 10 ms!!Send packet CA->Netherlands->CA 150,000,000 ns 150 ms

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

Page 7: 10 Data caching

LATENCY NUMBERS!!L2 cache reference 7 ns!!Main memory reference 100 ns!!!!!!!Disk seek 10,000,000 ns 10 ms!!Send packet CA->Netherlands->CA 150,000,000 ns 150 ms

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

Page 8: 10 Data caching

LATENCY NUMBERSL1 cache reference 0.5 ns!!L2 cache reference 7 ns!!Main memory reference 100 ns!!!!!!!Disk seek 10,000,000 ns 10 ms!!Send packet CA->Netherlands->CA 150,000,000 ns 150 ms

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

Page 9: 10 Data caching

LATENCY NUMBERSL1 cache reference 0.5 ns!Branch mispredict 5 ns!L2 cache reference 7 ns!Mutex lock/unlock 25 ns!Main memory reference 100 ns!Compress 1K bytes with Google Snappy 3,000 ns!Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms!Read 4K randomly from SSD 150,000 ns 0.15 ms!Read 1 MB sequentially from memory 250,000 ns 0.25 ms!Round trip within same datacenter 500,000 ns 0.5 ms!Read 1 MB sequentially from SSD 1,000,000 ns 1 ms!Disk seek 10,000,000 ns 10 ms!Read 1 MB sequentially from disk 20,000,000 ns 20 ms!Send packet CA->Netherlands->CA 150,000,000 ns 150 ms

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

Page 10: 10 Data caching

CACHE COHERENCE

• Cache hit: the cache content must be in sync with the original data.

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

• DNS cache provide stale data until expiration.

Page 11: 10 Data caching

CACHE EVICTION

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

• LRU (Last Recently Used)

• LFU (Least Frequently Used)

Page 12: 10 Data caching

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 CacheMain

Repository

Page 13: 10 Data caching

CACHE: THE BBOX WAY

Database

DAOs

BOs

Controllers

Windows

Server

Client

Page 14: 10 Data caching

CACHE: THE BBOX WAY

Database

DAOs

BOs

Controllers

Windows

DBMS cacheFirst level cache

Second level cacheQuery cache

Application cache

Page 15: 10 Data caching

HIBERNATE FIRST LEVEL CACHE

•Completely automatic and not tunable.

•Is flushed at the session’s end (simplifying: at the end of the transaction).

Page 16: 10 Data caching

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.

Page 17: 10 Data caching

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.

Page 18: 10 Data caching

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.

Page 19: 10 Data caching

Next talk: Welcome to Java 7