Scaling Your Cache & Caching at Scale Alex Miller @puredanger - - PowerPoint PPT Presentation

scaling your cache caching at scale
SMART_READER_LITE
LIVE PREVIEW

Scaling Your Cache & Caching at Scale Alex Miller @puredanger - - PowerPoint PPT Presentation

Scaling Your Cache & Caching at Scale Alex Miller @puredanger Mission Why does caching work? Whats hard about caching? How do we make choices as we design a caching architecture? How do we test a cache for performance?


slide-1
SLIDE 1

Scaling Your Cache & Caching at Scale

Alex Miller @puredanger

slide-2
SLIDE 2

Mission

  • Why does caching work?
  • What’s hard about caching?
  • How do we make choices as we

design a caching architecture?

  • How do we test a cache for

performance?

slide-3
SLIDE 3

What is caching?

slide-4
SLIDE 4

Lots of data

slide-5
SLIDE 5

Memory Hierarchy

Register L1 cache L2 cache RAM Disk Remote disk 1E+00 1E+01 1E+02 1E+03 1E+04 1E+05 1E+06 1E+07 1E+08 1E+09

1000000000 10000000 200 15 3 1

Clock cycles to access

slide-6
SLIDE 6

Register L1 Cache L2 Cache Main Memory Local Disk Remote Disk Fast Slow Small Expensive Big Cheap

Facts of Life

slide-7
SLIDE 7

Caching to the rescue!

slide-8
SLIDE 8

Temporal Locality

Stream: Cache: Hits: 0%

slide-9
SLIDE 9

Temporal Locality

Stream: Cache: Hits: 0% Stream: Cache: Hits: 65%

slide-10
SLIDE 10

Non-uniform distribution

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

slide-11
SLIDE 11

Temporal locality + Non-uniform distribution

slide-12
SLIDE 12

17000 pageviews assume avg load = 250 ms cache 17 pages / 80% of views cached page load = 10 ms new avg load = 58 ms trade memory for latency reduction

slide-13
SLIDE 13

The hidden benefit: reduces database load

Database Memory

line of over provisioning

slide-14
SLIDE 14

A brief aside...

  • What is Ehcache?
  • What is Terracotta?
slide-15
SLIDE 15

Ehcache Example

CacheManager manager = new CacheManager(); Ehcache cache = manager.getEhcache("employees"); cache.put(new Element(employee.getId(), employee)); Element element = cache.get(employee.getId());

<cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"

  • verflowToDisk="false"/>
slide-16
SLIDE 16

Terracotta

App Node Terracotta Server Terracotta Server App Node App Node App Node App Node App Node App Node App Node

slide-17
SLIDE 17

But things are not always so simple...

slide-18
SLIDE 18

Pain of Large Data Sets

  • How do I choose which

elements stay in memory and which go to disk?

  • How do I choose which

elements to evict when I have too many?

  • How do I balance cache size

against other memory uses?

slide-19
SLIDE 19

Eviction

When cache memory is full, what do I do?

  • Delete - Evict elements
  • Overflow to disk - Move to slower,

bigger storage

  • Delete local - But keep remote data
slide-20
SLIDE 20

Eviction in Ehcache

<cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"

  • verflowToDisk="false"/>

Evict with “Least Recently Used” policy:

slide-21
SLIDE 21

Spill to Disk in Ehcache

<diskStore path="java.io.tmpdir"/> <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"

  • verflowToDisk="true"

maxElementsOnDisk="1000000" diskExpiryThreadIntervalSeconds="120" diskSpoolBufferSizeMB="30" />

Spill to disk:

slide-22
SLIDE 22

Terracotta Clustering

<terracottaConfig url="server1:9510,server2:9510"/> <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"

  • verflowToDisk="false">

<terracotta/> </cache>

Terracotta configuration:

slide-23
SLIDE 23

Pain of Stale Data

  • How tolerant am I of seeing

values changed on the underlying data source?

  • How tolerant am I of seeing

values changed by another node?

slide-24
SLIDE 24

Expiration

1 2 3 4 5 6 7 8 9 TTI=4 TTL=4

slide-25
SLIDE 25

TTI and TTL in Ehcache

<cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"

  • verflowToDisk="false"/>
slide-26
SLIDE 26

Replication in Ehcache

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution. RMICacheManagerPeerProviderFactory" properties="hostName=fully_qualified_hostname_or_ip, peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32"/> <cache name="employees" ...> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory” properties="replicateAsynchronously=true, replicatePuts=true, replicatePutsViaCopy=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true asynchronousReplicationIntervalMillis=1000"/> </cache>

slide-27
SLIDE 27

Terracotta Clustering

Still use TTI and TTL to manage stale data between cache and data source Coherent by default but can relax with coherentReads=”false”

slide-28
SLIDE 28

Pain of Loading

  • How do I pre-load the cache on startup?
  • How do I avoid re-loading the data on every

node?

slide-29
SLIDE 29

Persistent Disk Store

<diskStore path="java.io.tmpdir"/> <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"

  • verflowToDisk="true"

maxElementsOnDisk="1000000" diskExpiryThreadIntervalSeconds="120" diskSpoolBufferSizeMB="30" diskPersistent="true" />

slide-30
SLIDE 30

Bootstrap Cache Loader

<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution. RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000" propertySeparator=",” />

Bootstrap a new cache node from a peer: On startup, create background thread to pull the existing cache data from another peer.

slide-31
SLIDE 31

Terracotta Persistence

Nothing needed beyond setting up Terracotta clustering. Terracotta will automatically bootstrap:

  • the cache key set on startup
  • cache values on demand
slide-32
SLIDE 32

Pain of Duplication

  • How do I get failover capability while avoiding

excessive duplication of data?

slide-33
SLIDE 33

Partitioning + Terracotta Virtual Memory

  • Each node (mostly) holds data it has seen
  • Use load balancer to get app-level partitioning
  • Use fine-grained locking to get concurrency
  • Use memory flush/fault to handle memory
  • verflow and availability
  • Use causal ordering to guarantee coherency
slide-34
SLIDE 34

Scaling Your Cache

slide-35
SLIDE 35

21

1 JVM Ehcache 2 or more JVMS 2 or more JVMs Ehcache RMI 2 or more JVMS 2 or more big JVMs Ehcache disk store Terracotta OSS 2 or more JVMS 2 or more JVMs 2 or more JVMs 2 or more JVMs 2 or more JVMs lots of JVMs

more scale

Terracotta FX Ehcache FX 2 or more JVMS 2 or more JVMs 2 or more JVMs lots of JVMs Terracotta FX Ehcache FX

NO NO YES YES YES

Scalability Continuum

causal

  • rdering

# JVMs runtime

Ehcache DX management and control Ehcache EX and FX management and control

slide-36
SLIDE 36

Caching at Scale

slide-37
SLIDE 37

Know Your Use Case

  • Is your data partitioned (sessions) or

not (reference data)?

  • Do you have a hot set or uniform

access distribution?

  • Do you have a very large data set?
  • Do you have a high write rate (50%)?
  • How much data consistency do you

need?

slide-38
SLIDE 38

Types of caches

Name Communication Advantage Broadcast invalidation multicast low latency Replicated multicast

  • ffloads db

Datagrid point-to-point scalable Distributed 2-tier point-to- point all of the above

slide-39
SLIDE 39

Common Data Patterns

I/O pattern Locality Hot set Rate of change

Catalog/ customer low low low Inventory high high high Conversations high high low

Catalogs/customers

  • warm all the

data into cache

  • High TTL

Inventory

  • fine-grained

locking

  • write-behind to DB

Conversations

  • sticky load

balancer

  • disconnect

conversations from DB

slide-40
SLIDE 40

Build a Test

  • As realistic as possible
  • Use real data (or good fake data)
  • Verify test does what you think
  • Ideal test run is 15-20 minutes
slide-41
SLIDE 41

Cache Warming

slide-42
SLIDE 42

Cache Warming

  • Explicitly record cache warming or

loading as a testing phase

  • Possibly multiple warming phases
slide-43
SLIDE 43

Lots o’ Knobs

slide-44
SLIDE 44

Things to Change

  • Cache size
  • Read / write / other mix
  • Key distribution
  • Hot set
  • Key / value size and structure
  • # of nodes
slide-45
SLIDE 45

Lots o’ Gauges

slide-46
SLIDE 46

Things to Measure

  • Application throughput (TPS)
  • Application
slide-47
SLIDE 47

Benchmark and Tune

  • Create a baseline
  • Run and modify parameters
  • Test, observe, hypothesize, verify
  • Keep a run log
slide-48
SLIDE 48

Bottleneck Analysis

slide-49
SLIDE 49

Pushing It

  • If CPUs are not all busy...
  • Can you push more load?
  • Waiting for I/O or resources
  • If CPUs are all busy...
  • Latency analysis
slide-50
SLIDE 50

I/O Waiting

  • Database
  • Connection pooling
  • Database tuning
  • Lazy connections
  • Remote services
slide-51
SLIDE 51

Locking and Concurrency

Key Value

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

get 2 get 2 p u t 8 put 12

Threads Locks

slide-52
SLIDE 52

Locking and Concurrency

Key Value

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

g e t 2 get 2 put 8 p u t 1 2

Threads Locks

slide-53
SLIDE 53

Objects and GC

  • Unnecessary object churn
  • Tune GC
  • Concurrent vs parallel collectors
  • Max heap
  • ...and so much more
  • Watch your GC pauses!!!
slide-54
SLIDE 54

Cache Efficiency

  • Watch hit rates and latencies
  • Cache hit - should be fast
  • Unless concurrency issue
  • Cache miss
  • Miss local vs
  • Miss disk / cluster
slide-55
SLIDE 55

Cache Sizing

  • Expiration and eviction tuning
  • TTI - manage moving hot set
  • TTL - manage max staleness
  • Max in memory - keep hot set

resident

  • Max on disk / cluster - manage total

disk / clustered cache

slide-56
SLIDE 56

Cache Coherency

  • No replication (fastest)
  • RMI replication (loose coupling)
  • Terracotta replication (causal
  • rdering) - way faster than strict
  • rdering
slide-57
SLIDE 57

Latency Analysis

  • Profilers
  • Custom timers
  • Tier timings
  • Tracer bullets
slide-58
SLIDE 58

mumble-mumble*

It’s time to add it to Terracotta.

* lawyers won’t let me say more

slide-59
SLIDE 59

Thanks!

  • Twitter - @puredanger
  • Blog - http://tech.puredanger.com
  • Terracotta - http://terracotta.org
  • Ehcache - http://ehcache.org