cold hard cache

Post on 03-Sep-2014

4.891 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Cold Hard CacheAlex Miller (@puredanger)

Agenda

• Why cache?

• Ehcache

• Hibernate 2nd level cache

• Terracotta

• The future....

Why cache?

• Temporal locality

• Non-uniform distribution

Temporal locality

Items:

Temporal locality

Items:

Cache:

Temporal locality

Items:

Cache:

Hits: 0%

Temporal locality

Items:

Cache:

Items:

Hits: 0%

Temporal locality

Items:

Cache:

Items:

Hits: 0%

Cache:

Temporal locality

Items:

Cache:

Items:

Hits: 0%

Cache:

Hits: 65%

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

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

Cache is always good right?

• Watch out for:

• Amdahl’s law

• Memory

• Concurrency

• Copy cost

Reduce latency

0

500

1000

1500

2000

No cache With cache

Page build Data retrieval

Database offload

OperationsPer Second

Amount of Data

DBs are sized to peak load

OperationsPer Second

Amount of Data

Strive to downsize DBs

OperationsPer Second

Amount of Data

Frequently accessed app data:Shared Memory (Transactional)

Business Record Data : Database

Ehcache

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

Ehcache Architecture

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

Ehcache 1.6 performance

Ehcache Performance vs memcached

Hibernate Second-Level 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

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

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

Terracotta as cache

DistributedCache

• High-throughput clustered coherent cache

• Simple interface - basically ConcurrentMap

• Eviction options

• TTI, TTL

• Max in-memory size, max total size limits

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”);

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

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

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>

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

New Hibernate cache visibility

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

The future of caching...

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

Single node and replicated Ehcache

• Same license, code base, and API

• Better visibility

• Better performance testing -> improved concurrency and performance

• Smooth migration path to...

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

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)

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

top related