10. Non-uniform distribution
Web page hits, ordered by rank
3200 100%
2400 75%
1600 50%
800 25%
0 0%
Page views, ordered by rank
Pageviews per rank
% of total hits per rank
11. Non-uniform distribution
Web page hits, ordered by rank
3200 100%
2400 75%
1600 50%
800 25%
0 0%
Page views, ordered by rank
Pageviews per rank
% of total hits per rank
12. Cache is always good right?
• Watch out for:
• Amdahl’s law
• Memory
• Concurrency
• Copy cost
13. Reduce latency
Page build Data retrieval
2000
1500
1000
500
0
No cache With cache
15. DBs are sized to peak load
Operations
Per Second
Amount of Data
16. Strive to downsize DBs
Frequently accessed app data:
Shared Memory (Transactional)
Operations
Per Second
Business Record Data : Database
Amount of Data
18. 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
20. 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
25. 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
26. 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
28. DistributedCache
• High-throughput clustered coherent cache
• Simple interface - basically ConcurrentMap
• Eviction options
• TTI, TTL
• Max in-memory size, max total size limits
29. 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”);
30. 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
31. 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
32. 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>
33. 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
37. Wonder twin powers, activate!
Terracotta Ehcache
• “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
38. Single node and replicated Ehcache
• Same license, code base, and API
• Better visibility
• Better performance testing -> improved concurrency and performance
• Smooth migration path to...
39. 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
40. 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)
41. 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