accelerate your coldfusion applications using caching

52
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Accelerate your ColdFusion Applications using Caching S V PAVAN KUMAR [email protected] October 2014

Upload: coldfusionconference

Post on 04-Jul-2015

836 views

Category:

Software


0 download

DESCRIPTION

Accelerate your ColdFusion Applications using Caching, by SV Pavan Kumar from Adobe Engineering team

TRANSCRIPT

Page 1: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Accelerate your ColdFusion Applications using Caching

S V PAVAN KUMAR

[email protected] 2014

Page 2: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Agenda

Caching Overview

CF Ehcache Implementation

CF Cache layout

Application specific caching

Query Caching

ORM Caching

Template & Object Caching

Distributed Caching

<cflogin> cache replication demo

Page 3: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache

During experiments, Many bird

species store peanuts in a cache for

later retrieval. In the wild, these birds

store acorns and insects.

-Wikipedia

Page 4: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache

A Simple Key/Value Pair

Key Value

USA Washington D.C

India New Delhi

England London

Japan Tokyo

Page 5: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

How Cache Works ?

Key Value

Key1 Value1

Key2 Value2

Key3 Value3

Key4 Value4

Application

Cache

Hit

Database ComputationNetwork

Page 6: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

How Cache Works ?

MissKey Value

Key1 Value1

Key2 Value2

Key3 Value3

Key4 Value4

Application

Cache

Database ComputationNetwork

Cache -

Aside

Page 7: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

How Cache Works ?

Miss Key Value

Key1 Value1

Key2 Value2

Key3 Value3

Key4 Value4

Application

Cache

Database ComputationNetwork

Read -

Through

Sync/ Async

Page 8: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Simply

Sacrifice memory for latency

reduction

Page 9: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Eviction Strategies

Manually deleting

Expiration

Expire after 2 Hours

Expire at Oct 18th, 2014

When Cache memory is Full

Delete using FIFO | LRU | LFU

Spill to Disk

EhCache does not remove expired elements immediately from

memory.i

Page 10: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache Bootstrapping

How to pre-load the cache on startup

From Disk store

From a peer cache node

From a central Cache server (e.g. Terracotta Server)

Start Empty, fill later (Cold Cache)

Page 11: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache Efficiency

Caches are not a panacea.

Page 12: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ColdFusion Caches

Query Cache

ORM Cache

Session level

2nd Level Cache

Template

Page

Fragment

Object Cache

<Cflogin> Cache

Page 13: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Overview

Industry Standard Caching Provider

Default cache provider for ColdFusion since CF 9

Various cache algorithms FIFO, LRU and LFU

Better control over cache through config

Disk persistence

Cache replication

Distributed caching support

Monitoring

Thread Safe

Page 14: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

CF Cache layout

Server Level Cache ManagerApplication level Cache Manager

Application level Cache

Manager

Application level Cache Manager

QUERYTEMPLATE

OBJECT

QUERY

TEMPLATE

OBJECT

QUERYTEMPLATE

OBJECT

Page 15: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Application Specific Caching

Added in CF 10

If enabled, caching is done at application level

Set this.cache.configfile in Application.cfc

Absolute: this.cache.configfile = "c:/myApp/ehcache.xml"

Relative to Application.cfc: this.cache.configfile = "ehcache.xml"

Server level Ehcache configuration file ehcache.xml can be found at

<Install_location>\<Instance name>\lib

Page 16: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Default Cache Regions

TEMPLATE

OBJECT

QUERY

Default cache names will be prefixed with applicationName (when app

specific caching is not enabled)

<App Name>TEMPLATE

<App Name>OBJECT

<App Name>QUERY

Page 17: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Custom Cache Regions

Using ehcache.xml in <CF Instance Name>/lib (Hard Coding)

cacheRegionNew() to create on-fly cache regions

All the caches supporting cacheRegion

All the cache functions supporting region argument

Custom cache region can contain any type of Data

Page 18: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache Region Properties

Cache region properties

retrieved using cacheGetProperties()

set using cacheSetProperties()

<cache

name=“myCache"

maxElementsInMemory="1000"

eternal="false"

timeToIdleSeconds="720"

timeToLiveSeconds="720"

overflowToDisk="true"

maxElementsOnDisk="100000"

diskPersistent="true"

memoryStoreEvictionPolicy="LRU"

/>

Page 19: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache Regions: CRUD

CacheRegionNew()

CacheRegionExists()

CacheRegionRemove(

)

CacheGetProperties()

CacheSetProperties()

• CacheGet()

• CacheGetAllIds()

• CachePut()

• CacheRemove()

• CacheRemoveAll()

Page 20: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache Metadata

CacheGetMetadata(): to get cache meta data for cache entry

cache_hitcount Number of hits

cache_misscount Number of misses

createdtime Creation time

Name Cache region Name

idletime time to idle for an element before it expires

lasthit last hit timestamp

Lastupdated last modified timestamp

size Cached Object size in bytes

timespan time to live for an element before it expires.

Page 21: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Query Cache - cachedwithin

<cfquery

name="GetParks" datasource="cfdocexamples"

cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#“ cacheRegion=“statewideParks”>

SELECT PARKNAME, REGION, STATE FROM Parks

ORDER BY ParkName, State

</cfquery>

Page 22: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Query Cache

Backed by Ehcache by default from CF10.

Internal cache fallback

Server level setting Caching -> Use internal cache to store queries

Application level specify

this.cache.useinternalquerycache=true|false

Query limit

Server level setting Caching -> Maximum number of cached queries default 100

Application level specify

this.cache.querysize = 150

Page 23: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Query Cache

Datasource name, the actual SQL statement itself, and the parameters

passed to the SQL statement will be used to pull out from the cache if

cache id not defined

dbtype=query" and "CachedWithin" are mutually Exclusive

Page 24: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Session Level Cache

Entities loaded from DB cached in ORM session

Short living exists as long as session is open

Caches persistent Objects

EntityLoad function

For the first time hits DB gets the entity.

For the second time retrieves from ORM session

Should use EntityReload to force retrieval from DB

ORMClearSession() clears session level cache

Page 25: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Second level Caching

CF uses EHCache as the default secondary cache provider from CF 9

Caches

Persistent Object

Persistent Object Associations

Results of Queries

Long lived

Supports Application specific Cache

Absolute: this.ormsettings.cacheConfig = "c:/myApp/ehcache.xml"

Relative to Application.cfc: this.ormsettings.cacheConfig = "ehcache.xml"

Can Scale in clustered environment

Page 26: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Cache Hierarchy

Database

ColdFusion

Application

Session Level Cache

Secondary Cache

(Ehcache)

Page 27: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Second level Caching Example

Application.cfc

<cfset this.name="Caching_Example">

<cfset this.datasource="cfartgallery">

<cfset this.ormenabled="true">

<cfset this.ormsettings.secondarycacheEnabled=true>

<cfset this.ormsettings.cacheProvider= "ehcache">

<cfset this.ormsettings.cacheConfig="ehcache.xml">

Page 28: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Second level Caching Example

CArtist.cfc

<cfcomponent persistent="true" schema="APP" table="Artists"cachename="artist" cacheuse="read-only">

<cfproperty name="artistid" fieldtype="id"/>

<cfproperty name="firstname"/>

<cfproperty name="lastname"/>

<cfproperty name="state"/>

<cfproperty name="art" fieldtype="one-to-many"cfc="CArt" fkcolumn="ArtID" cachename="ArtistArts"cacheuse="read-only">

</cfcomponent>

Page 29: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Second level Caching Example

CArt.cfc

<cfcomponent persistent="true" schema="APP" table="Art">

<cfproperty name="artid" generator="identity"fieldtype="id"/>

<cfproperty name="artname"/>

<cfproperty name="issold"/>

</cfcomponent>

Page 30: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Second level Caching Example

<cfscript>

//This will cache the Artist Component and also the association. It wouldn't cache the Art objects.

artistObj = EntityLoad("CArtists", 3, true);

//This will cache the query.

availableArts = ORMExecuteQuery("from CArt where issold=0", {}, false, {cacheable=true, cachename="availableArtsCache"});

</cfscript>

Page 31: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Cache Access Strategies

Read Only

Nonstrict-read-write

Read-write

Page 32: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

ORM Cache: Eviction

ORMEvictEntity("<entity_name>", [primarykey])

ORMevictcollection("<entity_name>", "<association_name>",

[primarykey])

ORMEvictQueries(cachename, datasource)

Page 33: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Template Cache: Pages

Both server- side and client-

side caching

Cache an entire page

Single <cfcache> at top of

page

No need of closing tag

</cfcache>

Also, <cfcache/>

Header

Naviga

tion

Bar

Content

Footer

<cfcache>

Page 34: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Template Cache: Fragments

Cache individual fragments of content

Useful when parts of a page must remain dynamic or personalized.

Surround with <cfcache>…</cfcache> tags

Multiple fragments can be cached in a single request

Each fragment can be configured independently

No Client side caching

Header

Naviga

tion

Bar

Content

Footer

<cfcache>

</cfcache>

<cfcache> <cfcache>

<cfcache>

</cfcache> </cfcache>

</cfcache>

Page 35: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Template Cache

Timespan

The interval until the item is flushed from the cache

idleTime

Flushes the cached item if it is not accessed for the specified time span

Metadata

Contains cache meta data info such as hit count, miss count created time etc.

expireURL

expireURL along with action = ‘flush’ used to flush pages that match the specified URL

or pattern

Wild card Support

<cfcache action="flush" expireurl="*.cfm?productID=#URL.productID#">

Page 36: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Template Cache Key

CacheI

d Given

?

Get the

Request URL

useQuer

yString ?

No

Append Query

String from

URL

Compute a hash

from page physical

path and Append

YesNo

Has

End

Tag?

Append Line No

Yes

depend

s on ?

No

Append

dependent

variables

YesTemplate Cache

Key

No

Yes

http://localhost/example/index.cfm_query_themeid=1_pageid:17CF7BD640264F4CB3D507A472E0190Bsession.username=JZNKA

Page 37: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Object Cache

Application code can take advantage of caching

Can cache anything (Simple values, Complex Objects like files, images

etc.)

Cache functions for CRUD operations and to get cache properties

(cfcache)

Cache keys need to be managed manually. (id is mandatory)

Page 38: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Distributed Caching

Page 39: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Standalone

Pros:

In Process & faster access

No serialization of keys and values

Eventual Consistency local to Application

ColdFusio

n

Cache

JVM

Cons

Not Scalable

Limited by CF JVM memory

GC pauses

On 32 Bit huge caches will suffer

Bootstrapping only from Disk Store.

Inconsistency on a cluster

Page 40: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Replicated

ColdFusio

n

Cach

eJVM

Cach

eJVM

Cach

eJVM

Cach

eJVM

Put, Remove,

Remove All,

Bootstrapping (Sync

or Async)

RMI, Jgroup

Protocols

ColdFusio

n

ColdFusio

nColdFusio

n

• Pros:

• In Process

• Data Consistency in

Cluster

• Bootstrapping from a

peer node

• Cons:

• GC Pauses

• Serialization overhead

• Limited by CF JVM

Memory

Page 41: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Centralized

ColdFusion

Cache

(L1)JVM

ColdFusion

Cache

(L1)JVM

Big

Memory

(L2

Cache)

JVM

Page 42: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Demo

Page 43: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

<cflogin> Auth cache

Prior to CF9 requires sticky sessions for clustered environment if

loginStorage=cookie

CF10 onwards auth info is stored in ehcache

To enable long lived logins or remember me type of functionality

To provide fail-over and replication support in cluster

Configuration for cflogin cache can be found at <cf-home>/lib/auth-

ehcache.xml

Page 44: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Peer Node Discovery

Automatic Peer Discovery

<cacheManagerPeerProviderFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties ="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,multicastGroupPort=4446, timeToLive=32"/>

Automatic addition & deletion of peer nodes

Page 45: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Peer Node Discovery

Manual Discovery

Peers cannot be added or removed at runtime

Multicast is not supported between the nodes

Each peer should know about all other peers

<cacheManagerPeerProviderFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=manual,rmiUrls=//server2:40001/sampleCache11|//server2:40001/sampleCache12"/>

Page 46: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Peer to Peer Communication

Configure hostname & port to listen for messages from peers

<cacheManagerPeerListenerFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="hostName=localhost, port=40001,

socketTimeoutMillis=2000"/>

*Port must be unique to each peer

Page 47: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Auth Cache

Configure replication to auth cache

<cache name="authcache"

maxElementsInMemory="10000"

eternal="false"

overflowToDisk="false"

diskSpoolBufferSizeMB="30"

maxElementsOnDisk="10000000"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="3600"

memoryStoreEvictionPolicy="LRU"

clearOnFlush="true">

Page 48: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Cache Replication

Enable replication for auth cache

<cacheEventListenerFactory

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,

replicateUpdatesViaCopy=false, replicateRemovals=true "/>

</cache>

Page 49: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Peer Bootstrapping

Enable Bootstrapping from peer for auth cache

<bootstrapCacheLoaderFactory

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"

properties="bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"

propertySeparator="," />

Page 50: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Monitoring

Copy ehcache-probe-<version>.jar to lib directory of each peer

Add the below listener to monitor the cache

<cacheManagerPeerListenerFactoryclass="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"

properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true" />

Page 51: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Q & A

Page 52: Accelerate your ColdFusion Applications using Caching

© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.