Cold Hard Cache

41
Cold Hard Cache Alex Miller (@puredanger)

description

An examination of caching, Ehcache, Hibernate second level-cache, Terracotta caching, and the future.

Transcript of Cold Hard Cache

Page 1: Cold Hard Cache

Cold Hard CacheAlex Miller (@puredanger)

Page 2: Cold Hard Cache

Agenda

• Why cache?

• Ehcache

• Hibernate 2nd level cache

• Terracotta

• The future....

Page 3: Cold Hard Cache

Why cache?

• Temporal locality

• Non-uniform distribution

Page 4: Cold Hard Cache

Temporal locality

Items:

Page 5: Cold Hard Cache

Temporal locality

Items:

Cache:

Page 6: Cold Hard Cache

Temporal locality

Items:

Cache:

Hits: 0%

Page 7: Cold Hard Cache

Temporal locality

Items:

Cache:

Items:

Hits: 0%

Page 8: Cold Hard Cache

Temporal locality

Items:

Cache:

Items:

Hits: 0%

Cache:

Page 9: Cold Hard Cache

Temporal locality

Items:

Cache:

Items:

Hits: 0%

Cache:

Hits: 65%

Page 10: Cold Hard Cache

Non-uniform distribution

0

800

1600

2400

3200

0%

25%

50%

75%

100%Web page hits, ordered by rank

Page views, ordered by rank

Pageviews per rank% of total hits per rank

Page 11: Cold Hard Cache

Non-uniform distribution

0

800

1600

2400

3200

0%

25%

50%

75%

100%Web page hits, ordered by rank

Page views, ordered by rank

Pageviews per rank% of total hits per rank

Page 12: Cold Hard Cache

Cache is always good right?

• Watch out for:

• Amdahl’s law

• Memory

• Concurrency

• Copy cost

Page 13: Cold Hard Cache

Reduce latency

0

500

1000

1500

2000

No cache With cache

Page build Data retrieval

Page 14: Cold Hard Cache

Database offload

OperationsPer Second

Amount of Data

Page 15: Cold Hard Cache

DBs are sized to peak load

OperationsPer Second

Amount of Data

Page 16: Cold Hard Cache

Strive to downsize DBs

OperationsPer Second

Amount of Data

Frequently accessed app data:Shared Memory (Transactional)

Business Record Data : Database

Page 17: Cold Hard Cache

Ehcache

Page 18: Cold Hard Cache

Ehcache history

• First created in 2003 by Greg Luck

• Most widely used Java cache - 100k’s of deployments

• Apache 2.0 license

• JSR 107 Java cache implementation

Page 19: Cold Hard Cache

Ehcache Architecture

Page 20: Cold Hard Cache

Ehcache Features

• In-memory and spill-to-disk storage

• Cache bootstrap loaders

• Cache replication via listener API - RMI, JGroups, JMS

• Cache server with REST and SOAP APIs

• Servlet caching filter API

• Hibernate second-level cache support

Page 21: Cold Hard Cache

Ehcache 1.6 performance

Page 22: Cold Hard Cache

Ehcache Performance vs memcached

Page 23: Cold Hard Cache

Hibernate Second-Level Cache

Page 24: Cold Hard Cache

Hibernate Caching

Application Thread

Database

Session

Application Thread

Session

CacheConcurrency

Strategy

CacheConcurrency

Strategy

CacheConcurrency

Strategy

CacheRegion

CacheRegion

CacheRegion

Hib

ern

ate

1st Level Cache

2nd Level CacheCacheProvider

Page 25: Cold Hard Cache

Entity and collection caches

• Entity and collection cache regions

• Mark a Hibernate entity or a collection in an entity as @Cacheable

• Specify a cache concurrency strategy

• ReadOnly, ReadWrite, NonstrictReadWrite, Transactional

• Turn on second level caching in the Hibernate config

Page 26: Cold Hard Cache

Query Cache

• Query cache regions

• Mark HQL, Criteria, Query as cacheable

• Store result set id values

• Timestamp cache region - last update time for each entity type

• Useful for caching natural key lookups (non-primary key)

• ...but lots of hidden issues

Page 27: Cold Hard Cache

Terracotta as cache

Page 28: Cold Hard Cache

DistributedCache

• High-throughput clustered coherent cache

• Simple interface - basically ConcurrentMap

• Eviction options

• TTI, TTL

• Max in-memory size, max total size limits

Page 29: Cold Hard Cache

DistributedCache Example

CacheConfig config = CacheConfigFactory.newConfig();config.setMaxTTISeconds(30 * 60) .setMaxTTLSeconds(2 * 60 * 60);

DistributedCache<String, Person> cache = config.newCache();

Person person = new Person(.......);cache.put(“Alex”, person);

Person cached = cache.get(“Alex”);

Page 30: Cold Hard Cache

DistributedCache features

• Built on high throughput ConcurrentDistributedMap

• Expiration based on either TTI or TTL

• Both in-memory and total target max limits

• Automatic memory management of caches

• Coherent clustered cache

Page 31: Cold Hard Cache

Terracotta Hibernate Second Level Cache

• Easy integration and configuration

• Supports entity, collection, and query cache regions

• Supports read-only, read-write, and nonstrict-read-write cache concurrency strategies

• Hibernate-specific tooling

• High performance with cache coherency

Page 32: Cold Hard Cache

Enabling Second Level Cache

• Mark your entities with a cache concurrency strategy

• In hibernate.cfg.xml: <cache usage="read-write"/>

• With annotations: @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)

• hibernate.cfg.xml

• <property name="cache.use_second_level_cache">true</property>

• <property name="cache.provider_class"> org.terracotta.hibernate.TerracottaHibernateCacheProvider</property>

Page 33: Cold Hard Cache

Enabling Second Level Cache

• Define the tc-hibernate-cache.xml in your classpath

<?xml version=”1.0” encoding=”UTF-8”?><terracotta-hibernate-cache-configuration> <default-configuration> <time-to-idle-seconds>7200</time-to-idle-seconds> <time-to-live-seconds>7200</time-to-live-seconds> </default-configuration> <cache> <region-name>org.terracotta.authinator.domain.Account</region-name> <!-- as many region-names here as you want --> <configuration> <time-to-idle-seconds>600</time-to-idle-seconds> <time-to-live-seconds>600</time-to-live-seconds> </configuration> </cache> </terracotta-hibernate-cache-configuration>

• Add the Terracotta Hibernate cache provider jar to your classpath

• -cp terracotta-hibernate-cache-1.0.0.jar

• Add the Terracotta Hibernate cache agent jar to your command line

• -javaagent:terracotta-hibernate-agent-1.0.0.jar

Page 34: Cold Hard Cache

New Hibernate cache visibility

Page 35: Cold Hard Cache

Performance - Read-Only Comparison

0

20

40

60

80

100

Database IMDG EhcacheTerracotta

Latency

Avg

Lat

ency

(ms)

0K

50K

100K

150K

200K

Database IMDG EhcacheTerracotta

Throughput

Tran

sact

ions

per

sec

ond

Page 36: Cold Hard Cache

The future of caching...

Page 37: Cold Hard Cache

Wonder twin powers, activate!

• “Standard” cache apis (Ehcache / JSR 107)

• Low latency local cache

• High throughput clustered cache

• Coherent caching with options to degrade for greater performance

• Support for both “copy” and “shared object” caching

Terracotta Ehcache

Page 38: Cold Hard Cache

Single node and replicated Ehcache

• Same license, code base, and API

• Better visibility

• Better performance testing -> improved concurrency and performance

• Smooth migration path to...

Page 39: Cold Hard Cache

Clustered Ehcache

• Short release for initial integration (probably Ehcache 1.7)

• Clustered store - partial API support

• Smooth upgrade from single node or replicated Ehcache

• New management and visibility features

Page 40: Cold Hard Cache

Hibernate 2nd level cache

• More efficient in-memory and total count eviction (3.1.1)

• Better visibility of memory conditions

• Improved query caching

• Improved performance of core Terracotta (lock manager and memory manager)

Page 41: Cold Hard Cache

Thanks!

• Terracotta Open Source JVM clustering:

• http://www.terracotta.org

• Apress: “The Definitive Guide to Terracotta”

• by Ari Zilka, Alex Miller, Geert Bevin, Jonas Boner, Orion Letizi, Taylor Gautier

• 2nd edition in progress....

• Alex Miller

• @puredanger

• http://tech.puredanger.com