Boosting Performance and Scale for ColdFusion Applications with Ehcache

Comments

There aren't any comments for this presentation.

Add Comment

Comments have been closed.

Transcript

no image

Slide Text

Slide Notes


Boosting Performance and Scale for ColdFusion Applications with Ehcache

no text exists for this slide

no notes exist for this slide

Terracotta Simplifying Application Scalability

no text exists for this slide

no notes exist for this slide

Agenda


What’s new in ColdFusion 9.0.1
What’s new in ColdFusion 9.0.1
Caching architectures & strategies
Ehcache
Object Caching
Template Caching
Cache Replication
Hibernate Caching
Distributed Caching
Q&A

no notes exist for this slide

ColdFusion Caching Whats New

no text exists for this slide

no notes exist for this slide

Caching Opportunities

no text exists for this slide

Database – Most modern RDBMS have in-memory caching functionality. However, every insert or update flushes the cache and causes it to repopulate. ColdFusion – Cache data, query results, page fragments, web pages Web server – Cache rendered web pages Proxy/Cache Appliance – Cache images, web pages Content Delivery Network – Cache images, web pages Client – Cache rendered pages

Database Scalability Example


Consider a DB that can handle 100 queries per second
Consider a DB that can handle 100 queries per second
Now add in a cache that accounts for a hit ratio of 90%
Database load is reduced to 10 queries per second, allowing the DB to scale by a factor of 10.

no notes exist for this slide

Caching Granularity What to Cache


What to Cache
What to Cache
Data
Query result sets
Objects
Partial page content (fragments)
Entire web pages

no notes exist for this slide

General Caching Tips


Cache like crazy
Cache like crazy
Memory and disk are cheap
Cache as close to the final state as possible
E.g. don’t cache a recordset if you’ll ultimately use it to build a dropdown box*
Cache entire pages whenever possible
Cache to static files whenever possible
Works well for content that rarely changes
For dynamic sites, look to other options
Be mindful of cache size
May limit what/how much you can cache, especially on 32-bit systems

Distributed caching solutions run out of process, so restarting a ColdFusion instance or application doesn’t affect the cache Don’t put a distributed cache instance on the same server as your database

Caching Architectures


In-Process
In-Process
Operates in the same process (JVM) as the application server
Limited scalability for 32-bit systems
Must consider possible number of cache variants
Max JVM heap size
Can fail-over to disk storage
Fast
Data/object serialization is not required for memory based in-process cache

http://gregluck.com/blog/archives/2007/05/comparing_memca.html

Slide 10


Out-of-Process
Out-of-Process
Operates in its own process, outside of the application server’s jvm
Highly scalable on both 32-bit and 64-bit platforms
Scale out
Utilize spare memory throughout the network
Slower than in-process caching
Data/objects must be serialized/deserialized

no notes exist for this slide

Caching Strategies


Non-deterministic (cache-aside)
Non-deterministic (cache-aside)
Look in the cache; if not there, hit the database (do this in your getters)
This is the most popular strategy
Must be mindful of the effect of cache misses
Deterministic (cache-as-sor)
Always go to the cache
All cache data is pre-populated

no notes exist for this slide

Cache Eviction Algorithms


Time Based
Time Based
Time period (2 hours, 4 days, etc.)
Expiration date (Jan 23, 2010)
Cost Based
First In First Out (FIFO)
Least Recently Used (LRU)
Less Frequently Used (LFU)
Unique to Ehcache

Ehcache supports the following: Memory based (LRU, LFU, FIFO) Disk based (LFU)

Slide 13






“There are only two hard things in Computer Science: cache invalidation and naming things. “
-Phil Karlton

no notes exist for this slide

ExpiringUpdating Cached Content


Most caches will expire data based on a number of factors:
Most caches will expire data based on a number of factors:
Time based expiry
When the cache runs out of room (FIFO, LRU, etc.)
However, how do you update the cache when your backend data changes?
It depends!
How timely does the data need to be?
How heavily is your application loaded?
What method did you use to cache the data (built in query caching, shared scope variable, external cache, etc.)?
Update your cache when you update your database
Put the updated data directly into the cache when data is written to the DB (preferred). Do this in your setters.
Purge the updated item from the cache, causing a cache miss upon next request (watch out for the dog pile)

no notes exist for this slide

Ehcache Overview


De facto cache for Enterprise Java and ColdFusion
De facto cache for Enterprise Java and ColdFusion
Over 250,000 implementations
Can be configured to run:
Local: In-process
Replicated: In-process
Distributed: In-process and Out-of-process
Cached items are stored in memory as key/value pairs
Supports disk stores for cache overflow
Implemented as ColdFusion 9’s caching provider for fragment, object and 2nd level Hibernate caching
Also available in Railo as of 3.1.2

no notes exist for this slide

Slide 16


Ehcache is fast
Ehcache is fast
In-process caches are generally faster than out-of-process caches
No serialization required for objects written to memory
Supports LRU, LFU, FIFO and Expiration Timeouts
Ehcache can scale both vertically and horizontally
By default Ehcache runs within CF’s JVM
Cache size is limited by the amount of memory available CF’s single jvm
You can cluster ehcache servers, but data is replicated among them
Ehcache 2.0 adds distributed caching via Terracotta
Ehcache Enterprise
Built-in distributed caching
Commercial support
Added enterprise features for production operation

no notes exist for this slide

Performance


Don’t cache to disk unless you absolutely have to
Don’t cache to disk unless you absolutely have to
Consider using a RAM based file system for disk based caches (CF 9’s RAM based file system is not currently compatible)
Memory based caches are several orders of magnitude faster than disk based because:
Serialization of key/value
Eviction from memory store uses an eviction algorithm (disk uses LFU)
Disk read speed is slow

Speedometer photo is Creative Commons: http://www.flickr.com/photos/thatguyfromcchs08/2300190277/ No synchronous overhead writing to disk because it’s handled by a separate thread

Ehcache Implementation


Every ColdFusion application has two available caches by default:
Every ColdFusion application has two available caches by default:
Template
Accessed via the cfcache tag
Cache page fragments and entire pages
Fairly automatic
Object
Accessed via the cfcache tag and/or cache functions
Cache anything
Granular control over the cache

no notes exist for this slide

Slide 19


Caches are bound to named applications (Application.cfc or Application.cfm)
Caches are bound to named applications (Application.cfc or Application.cfm)
Caches are not tied to ColdFusion scopes
Caches persist even if the bound application scope times out
If a cache is created outside of an application it is available to all unnamed applications
Additional named caches can be defined:
ehcache.xml
Using the cfcache tag
Using cache functions (as of CF 9.0.1)
Using Java

no notes exist for this slide

ehcachexml


/JRun4/servers/servername/cfusion-ear/cfusion-war/WEB-INF/cfusion/lib
/JRun4/servers/servername/cfusion-ear/cfusion-war/WEB-INF/cfusion/lib
Disk store location – applies to all caches
Specifies cache configuration
Named cache regions
Default cache configuration
Replication configuration
Distributed caching

no notes exist for this slide

Cache Properties


Can be set in ehcache.xml or dynamically using cacheSetProperties()
Can be set in ehcache.xml or dynamically using cacheSetProperties()
Dynamic setting only for default caches – unless you use Java
Get the cache properties using cacheGetProperties()
Applies only to default caches
Available Properties
clearOnFlush
diskExpiryThreadIntervalSeconds
diskPersistent
diskSpoolBufferSizeMB
Eternal
maxElementsInMemory
maxElementsOnDisk
memoryEvictionPolicy
objectType
overflowToDisk
timeToIdleSeconds
timeToLiveSeconds

no notes exist for this slide

Template Caching


Can be used to cache:
Can be used to cache:
Entire pages
Place a single cfcache tag at the top of your page
Page fragments
Wrap content you want to cache in cfcache tags
Useful when part(s) of a page must remain dynamic
Implemented via cfcache tag only
ColdFusion automatically:
Generates and manages keys
Handles gets and puts
Configurable dependent caching
Watch a variable or list of variables for change
If the value of one of these variables changes from the first page or fragment that was cached, ColdFusion will create a new variant for the page/fragment and store that in the cache as well.

no notes exist for this slide

Object Caching


Most flexible way to take advantage of caching in ColdFusion
Most flexible way to take advantage of caching in ColdFusion
Can cache anything you can put in a ColdFusion variable
Granular control over cache gets, puts, removals
Access to cache item metadata:
cache_hitcount
cache_misscount
createdtime
hitcount
idletime
lasthit
lastupdated
size
timespan

no notes exist for this slide

Object Caching Options


With cache functions:
With cache functions:
cacheGetProperties()
cacheSetProperties()
cacheGet()
cachePut()
cacheRemove()
cacheGetMetadata()
cacheGetAllIDs()
getAllTemplateCacheIDs()
cacheGetSession()

no notes exist for this slide

Ehcache vs Persistent Variable Scopes


Why use Ehcache when ColdFusion already lets you store data in persistent scopes and the query cache?
Why use Ehcache when ColdFusion already lets you store data in persistent scopes and the query cache?
Both session/application variables, the ColdFusion query cache and Ehcache implement a HashMap
In many cases, performance across all three caching methods may be relatively equal
Ehcache advantages:
Performs well regardless of load
Easily replicated/distributed
Flexible eviction policies
Self-managing
Comprehensive monitoring and statistics

no notes exist for this slide

Object Caching Considerations


Carefully consider how you plan to use keys to avoid potential key collision
Carefully consider how you plan to use keys to avoid potential key collision
Key management is not necessary with the Template cache
Ehcache doesn’t support namespaces but you can simulate them
User_
Product_
Item_
Consider multiple cache regions to segment cached items
Objects (including queries) are stored by reference – be careful
Only serializable objects can be written to the disk cache
Be careful using cacheGetAllIDs()

no notes exist for this slide

Using Ehcache with Hibernate in ColdFusion 9


Hibernate supports two levels of caching:
Hibernate supports two levels of caching:
Session Level Cache
Caches the objects loaded from the database
Limited to the duration of an ORM session
2nd Level cache
Can cache:
Persistent object data
Persistent object association
Query data
Can live longer than the lifetime of an ORM session
Ehcache is the default 2nd Level cache for Hibernate in ColdFusion 9
Configurable via ehcache.xml and/or at runtime
Can easily be replicated/distributed

no notes exist for this slide

Slide 28


What to cache:
What to cache:
Data that rarely changes
Data local to the application (not updated by other apps)
Access strategies:
Read-only
Non-restrict read-write
Read-write
Transactional

no notes exist for this slide

Slide 29


You must enable 2nd Level Caching for ORM in your Application.cfc:
You must enable 2nd Level Caching for ORM in your Application.cfc:
this.ormsettings.secondarycacheEnabled=“true”
this.ormsettings.cacheProvider="ehcache"
this.ormsettings.cacheConfig="ehcache.xml“
Use ORMGetSessionFactory() to get the session factory that created the current Hibernate session
Use the session factory’s getStatistics() method to return metadata about your Hibernate session, including cache size, hits, misses, etc.
Statistics must be enabled via setStatisticsEnabled(true)

no notes exist for this slide

Cache Replication in ColdFusion 9


Both object and template caches can be replicated
Both object and template caches can be replicated
Simple configuration via ehcache.xml file
Cache replication via RMI, JMS, JGroups or Terracotta
Asynchronous replication is the fastest method
Because it’s asynchronous the caller returns immediately
Messages are placed in a queue and bacthed via RMI as they are processed

Show configured ehcache.xml

Enterprise Ehcache Snapin Scale

no text exists for this slide

no notes exist for this slide

Its almost ridiculous how easy it is to deployand the scalability is awesome

no text exists for this slide

Study Island helps students in kindergarten through 12th grade, or K-12, master state-specific grade level academic standards in a fun and engaging manner. During the 2009-2010 school year, Study Island products were utilized by approximately 10.0 million students in nearly 22,000 schools in 50 states. These students answered over 3.2 billion practice questions.

Enterprise Ehcache in ColdFusion 3 Easy Steps 2 Lines of Config

no text exists for this slide

no notes exist for this slide

Resources


Ehcache: http://ehcache.org/
Ehcache: http://ehcache.org/
ColdFusion 9 Documentation
Using Ehcache with ColdFusion: http://ehcache.org/documentation/coldfusion.html
My Blog Series on Caching in ColdFusion 9:
http://www.brooks-bilson.com/blogs/rob/index.cfm/Caching
High Scalability: http://highscalability.com/
Setting up Ehcache on Railo (Gary Gilbert): http://www.garyrgilbert.com/blog/index.cfm/2009/11/19/Setting-up-EHCache-on-Railo

no notes exist for this slide

Interesting Ehcache 2x Enhancements


CacheWriters
CacheWriters
Write-through – when data is written to the cache, the CacheWritter also writes the data to the underlying datasource in the same transaction. This does not improve write performance.
Write-behind – data is written to the cache and asynchronously written to the underlying data store. This can greatly improve write performance.
Read-through
Attempt to read from cache, if not there, the cache requests the item from the datasource, updates the cache then returns the data to the calling program.
Blocking Cache
All threads requesting the same key wait for the first thread to complete. Once the first thread has completed the other threads simply obtain the cache entry and return.
SelfPopulatingCache
Uses a CacheEntryFactory, that given a key, knows how to populate the entry.

no notes exist for this slide

QampA

no text exists for this slide

no notes exist for this slide