SlideShare una empresa de Scribd logo
1 de 59
Descargar para leer sin conexión
Scaling Your Cache
& Caching at Scale


          Alex Miller
          @puredanger
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?
What is caching?
Lots of data
Memory Hierarchy
                                               Clock cycles to access

   Register   1


   L1 cache   3


   L2 cache   15


      RAM     200


       Disk   10000000


Remote disk   1000000000


          1E+00 1E+01 1E+02 1E+03 1E+04 1E+05 1E+06 1E+07 1E+08 1E+09
Facts of Life
  Register          Fast Small Expensive
 L1 Cache
 L2 Cache
Main Memory
 Local Disk
Remote Disk         Slow   Big   Cheap
Caching to the rescue!
Temporal Locality
Hits:                         0%
Cache:

Stream:
Temporal Locality
Hits:                         0%
Cache:

Stream:

Stream:

Cache:
Hits:                         65%
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
Temporal locality
        +
  Non-uniform
  distribution
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
The hidden benefit:
 reduces database load


Memory    Database

                                 sio ning
                                i
                            rov
                        er p
                  o f ov
           line
A brief aside...


• What is Ehcache?
• What is Terracotta?
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"
            overflowToDisk="false"/>
Terracotta

App Node   App Node     App Node     App Node



           Terracotta   Terracotta
             Server       Server



App Node   App Node     App Node     App Node
But things are not
always so simple...
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?
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
Eviction in Ehcache

Evict with “Least Recently Used” policy:
    <cache   name="employees"
             maxElementsInMemory="1000"
             memoryStoreEvictionPolicy="LRU"
             eternal="false"
             timeToIdleSeconds="600"
             timeToLiveSeconds="3600"
             overflowToDisk="false"/>
Spill to Disk in Ehcache
Spill to disk:
      <diskStore path="java.io.tmpdir"/>

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

               overflowToDisk="true"
               maxElementsOnDisk="1000000"
               diskExpiryThreadIntervalSeconds="120"
               diskSpoolBufferSizeMB="30" />
Terracotta Clustering
Terracotta configuration:
     <terracottaConfig url="server1:9510,server2:9510"/>

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

         <terracotta/>
     </cache>
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?
Expiration

TTI=4


        0 1 2 3 4 5 6 7 8 9

TTL=4
TTI and TTL in Ehcache

 <cache   name="employees"
          maxElementsInMemory="1000"
          memoryStoreEvictionPolicy="LRU"
          eternal="false"
          timeToIdleSeconds="600"
          timeToLiveSeconds="3600"
          overflowToDisk="false"/>
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>
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”
Pain of Loading
• How do I pre-load the cache on startup?
• How do I avoid re-loading the data on every
  node?
Persistent Disk Store
<diskStore path="java.io.tmpdir"/>

<cache   name="employees"
         maxElementsInMemory="1000"
         memoryStoreEvictionPolicy="LRU"
         eternal="false"
         timeToIdleSeconds="600"
         timeToLiveSeconds="3600"
         overflowToDisk="true"
         maxElementsOnDisk="1000000"
         diskExpiryThreadIntervalSeconds="120"
         diskSpoolBufferSizeMB="30"

         diskPersistent="true" />
Bootstrap Cache Loader

Bootstrap a new cache node from a peer:
       <bootstrapCacheLoaderFactory
             class="net.sf.ehcache.distribution.
                    RMIBootstrapCacheLoaderFactory"
             properties="bootstrapAsynchronously=true,
                         maximumChunkSizeBytes=5000000"
             propertySeparator=",” />




On startup, create background thread to pull
the existing cache data from another peer.
Terracotta Persistence
Nothing needed beyond setting up
Terracotta clustering.

Terracotta will automatically bootstrap:
- the cache key set on startup
- cache values on demand
Pain of Duplication
• How do I get failover capability while avoiding
  excessive duplication of data?
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
    overflow and availability
•   Use causal ordering to guarantee coherency
Scaling Your Cache
Scalability Continuum
 causal
ordering   YES NO                            NO                  YES                         YES
                                                                                  2 or more       2 or more
                                          2 or more JVMS                             JVMSmore
                                                                                     2 or
                                           2 or more big JVMs                                        2 or more
                                                                                                     JVMs
                     2 or more                                  2 or more              2 or more
                                                                                        JVMs           2 or more
                                                                                                        JVMs
                                                                                          JVMs of
                                                                                            lots
 # JVMs    1 JVM
                       2 or more
                        JVMS
                          JVMs
                                                                  2 or more
                                                                   JVMS
                                                                     JVMs                    JVMs
                                                                                                          JVMs of
                                                                                                            lots
                                                                                                             JVMs




                                                                  Terracotta
runtime    Ehcache
                       Ehcache
                         RMI
                                                Ehcache
                                               disk store            OSS            Terracotta FX         Terracotta FX
                                                                                        Ehcache FX           Ehcache FX




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




                                                 more scale




                                                                                                     21
Caching at Scale
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?
Types of caches
Name             Communication      Advantage

Broadcast        multicast          low latency
invalidation
Replicated       multicast          offloads db


Datagrid         point-to-point     scalable


Distributed      2-tier point-to-   all of the above
                 point
Common Data Patterns
  I/O pattern     Locality         Hot set          Rate of
                                                    change
  Catalog/        low              low              low
  customer
  Inventory       high             high             high
  Conversations   high             high             low



Catalogs/customers       Inventory              Conversations
     • warm all the      • fine-grained          • sticky load
        data into            locking               balancer
        cache            • write-behind to DB   • disconnect
     • High TTL                                    conversations from
                                                   DB
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
Cache Warming
Cache Warming

• Explicitly record cache warming or
  loading as a testing phase
• Possibly multiple warming phases
Lots o’ Knobs
Things to Change
• Cache size
• Read / write / other mix
• Key distribution
• Hot set
• Key / value size and structure
• # of nodes
Lots o’ Gauges
Things to Measure

• Application throughput (TPS)
• Application latency
• OS: CPU, Memory, Disk, Network
• JVM: Heap, Threads, GC
Benchmark and Tune

• Create a baseline
• Run and modify parameters
 • Test, observe, hypothesize, verify
• Keep a run log
Bottleneck Analysis
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
I/O Waiting
• Database
 • Connection pooling
 • Database tuning
 • Lazy connections
• Remote services
Locking and
                    Concurrency
Threads             Locks
                              Key     Value
                                  1



           ge
                                  2



               t2                 3


     get 2                        4

                                  5

                                  6



     put 8
                                  7

                                  8



               12
                                  9



          ut
                               10

      p                        11

                               12

                               13

                               14

                               15

                               16
Locking and
               Concurrency
Threads        Locks
                         Key     Value

      get 2                  1

                             2




      get 2
                             3

                             4

                             5

                             6


      put 8                  7

                             8

                             9

                          10


      put 12              11

                          12

                          13

                          14

                          15

                          16
Objects and GC
• Unnecessary object churn
• Tune GC
 • Concurrent vs parallel collectors
 • Max heap
 • ...and so much more
• Watch your GC pauses!!!
Cache Efficiency
• Watch hit rates and latencies
 • Cache hit - should be fast
   • Unless concurrency issue
 • Cache miss
   • Miss local vs
   • Miss disk / cluster
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
Cache Coherency

• No replication (fastest)
• RMI replication (loose coupling)
• Terracotta replication (causal
  ordering) - way faster than strict
  ordering
Latency Analysis

• Profilers
• Custom timers
• Tier timings
• Tracer bullets
mumble-mumble*

 It’s time to add it to Terracotta.




                    * lawyers won’t let me say more
Thanks!

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

Más contenido relacionado

La actualidad más candente

0628阙宏宇
0628阙宏宇0628阙宏宇
0628阙宏宇zhu02
 
Optimizing WordPress for Performance - WordCamp Houston
Optimizing WordPress for Performance - WordCamp HoustonOptimizing WordPress for Performance - WordCamp Houston
Optimizing WordPress for Performance - WordCamp HoustonChris Olbekson
 
캐시 분산처리 인프라
캐시 분산처리 인프라캐시 분산처리 인프라
캐시 분산처리 인프라Park Chunduck
 
Bangalore cloudstack user group
Bangalore cloudstack user groupBangalore cloudstack user group
Bangalore cloudstack user groupShapeBlue
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cacheMarc Cortinas Val
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010isnull
 
Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQGeert Pante
 
Plugin Memcached%20 Study
Plugin Memcached%20 StudyPlugin Memcached%20 Study
Plugin Memcached%20 StudyLiu Lizhi
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnishschoefmax
 
vSphere APIs for performance monitoring
vSphere APIs for performance monitoringvSphere APIs for performance monitoring
vSphere APIs for performance monitoringAlan Renouf
 
Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012Combell NV
 

La actualidad más candente (19)

First Day With J Ruby
First Day With J RubyFirst Day With J Ruby
First Day With J Ruby
 
0628阙宏宇
0628阙宏宇0628阙宏宇
0628阙宏宇
 
My Old Friend Malloc
My Old Friend MallocMy Old Friend Malloc
My Old Friend Malloc
 
Optimizing WordPress for Performance - WordCamp Houston
Optimizing WordPress for Performance - WordCamp HoustonOptimizing WordPress for Performance - WordCamp Houston
Optimizing WordPress for Performance - WordCamp Houston
 
캐시 분산처리 인프라
캐시 분산처리 인프라캐시 분산처리 인프라
캐시 분산처리 인프라
 
Memcached
MemcachedMemcached
Memcached
 
ESX performance problems 10 steps
ESX performance problems 10 stepsESX performance problems 10 steps
ESX performance problems 10 steps
 
Bangalore cloudstack user group
Bangalore cloudstack user groupBangalore cloudstack user group
Bangalore cloudstack user group
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cache
 
Wckansai 2014
Wckansai 2014Wckansai 2014
Wckansai 2014
 
Varnish Cache
Varnish CacheVarnish Cache
Varnish Cache
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
 
Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQ
 
Memcached
MemcachedMemcached
Memcached
 
Plugin Memcached%20 Study
Plugin Memcached%20 StudyPlugin Memcached%20 Study
Plugin Memcached%20 Study
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
 
vSphere APIs for performance monitoring
vSphere APIs for performance monitoringvSphere APIs for performance monitoring
vSphere APIs for performance monitoring
 
Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012
 
Intro to riak
Intro to riakIntro to riak
Intro to riak
 

Similar a Scaling Your Cache And Caching At Scale

Scaling Your Cache
Scaling Your CacheScaling Your Cache
Scaling Your CacheAlex Miller
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsDavide Carnevali
 
Virtualization Manager 5.0 – Now with Hyper-V Support!
Virtualization Manager 5.0 – Now with Hyper-V Support!Virtualization Manager 5.0 – Now with Hyper-V Support!
Virtualization Manager 5.0 – Now with Hyper-V Support!SolarWinds
 
Usenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a ProxyUsenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a ProxyLeif Hedstrom
 
Secrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on KubernetesSecrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on KubernetesBruno Borges
 
Building low latency java applications with ehcache
Building low latency java applications with ehcacheBuilding low latency java applications with ehcache
Building low latency java applications with ehcacheChris Westin
 
Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...
Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...
Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...srisatish ambati
 
Lightweight Grids With Terracotta
Lightweight Grids With TerracottaLightweight Grids With Terracotta
Lightweight Grids With TerracottaPT.JUG
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13Dave Gardner
 
ContainerWorkloadwithSemeru.pdf
ContainerWorkloadwithSemeru.pdfContainerWorkloadwithSemeru.pdf
ContainerWorkloadwithSemeru.pdfSumanMitra22
 
CloudStack Performance Testing
CloudStack Performance TestingCloudStack Performance Testing
CloudStack Performance Testingbuildacloud
 
java-monitoring-troubleshooting
java-monitoring-troubleshootingjava-monitoring-troubleshooting
java-monitoring-troubleshootingWilliam Au
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011bobmcwhirter
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous ApplicationsJohan Edstrom
 
WordPress Performance & Scalability
WordPress Performance & ScalabilityWordPress Performance & Scalability
WordPress Performance & ScalabilityJoseph Scott
 
[Outdated] Secrets of Performance Tuning Java on Kubernetes
[Outdated] Secrets of Performance Tuning Java on Kubernetes[Outdated] Secrets of Performance Tuning Java on Kubernetes
[Outdated] Secrets of Performance Tuning Java on KubernetesBruno Borges
 

Similar a Scaling Your Cache And Caching At Scale (20)

Scaling Your Cache
Scaling Your CacheScaling Your Cache
Scaling Your Cache
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limits
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
 
Cold Hard Cache
Cold Hard CacheCold Hard Cache
Cold Hard Cache
 
Virtualization Manager 5.0 – Now with Hyper-V Support!
Virtualization Manager 5.0 – Now with Hyper-V Support!Virtualization Manager 5.0 – Now with Hyper-V Support!
Virtualization Manager 5.0 – Now with Hyper-V Support!
 
Usenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a ProxyUsenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a Proxy
 
Secrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on KubernetesSecrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on Kubernetes
 
Building low latency java applications with ehcache
Building low latency java applications with ehcacheBuilding low latency java applications with ehcache
Building low latency java applications with ehcache
 
Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...
Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...
Cache is King ( Or How To Stop Worrying And Start Caching in Java) at Chicago...
 
EVCache at Netflix
EVCache at NetflixEVCache at Netflix
EVCache at Netflix
 
Lightweight Grids With Terracotta
Lightweight Grids With TerracottaLightweight Grids With Terracotta
Lightweight Grids With Terracotta
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13
 
ContainerWorkloadwithSemeru.pdf
ContainerWorkloadwithSemeru.pdfContainerWorkloadwithSemeru.pdf
ContainerWorkloadwithSemeru.pdf
 
CloudStack Performance Testing
CloudStack Performance TestingCloudStack Performance Testing
CloudStack Performance Testing
 
java-monitoring-troubleshooting
java-monitoring-troubleshootingjava-monitoring-troubleshooting
java-monitoring-troubleshooting
 
Hibernate Cache
Hibernate CacheHibernate Cache
Hibernate Cache
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous Applications
 
WordPress Performance & Scalability
WordPress Performance & ScalabilityWordPress Performance & Scalability
WordPress Performance & Scalability
 
[Outdated] Secrets of Performance Tuning Java on Kubernetes
[Outdated] Secrets of Performance Tuning Java on Kubernetes[Outdated] Secrets of Performance Tuning Java on Kubernetes
[Outdated] Secrets of Performance Tuning Java on Kubernetes
 

Más de Alex Miller

Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)Alex Miller
 
Concurrent Stream Processing
Concurrent Stream ProcessingConcurrent Stream Processing
Concurrent Stream ProcessingAlex Miller
 
Stream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/joinStream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/joinAlex Miller
 
Cracking clojure
Cracking clojureCracking clojure
Cracking clojureAlex Miller
 
Releasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic WebReleasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic WebAlex Miller
 
Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of AbstractionAlex Miller
 
Tree Editing with Zippers
Tree Editing with ZippersTree Editing with Zippers
Tree Editing with ZippersAlex Miller
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
Innovative Software
Innovative SoftwareInnovative Software
Innovative SoftwareAlex Miller
 
Caching In The Cloud
Caching In The CloudCaching In The Cloud
Caching In The CloudAlex Miller
 
Marshmallow Test
Marshmallow TestMarshmallow Test
Marshmallow TestAlex Miller
 
Strange Loop Conference 2009
Strange Loop Conference 2009Strange Loop Conference 2009
Strange Loop Conference 2009Alex Miller
 
Scaling Hibernate with Terracotta
Scaling Hibernate with TerracottaScaling Hibernate with Terracotta
Scaling Hibernate with TerracottaAlex Miller
 
Project Fortress
Project FortressProject Fortress
Project FortressAlex Miller
 
Java Collections API
Java Collections APIJava Collections API
Java Collections APIAlex Miller
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency IdiomsAlex Miller
 
Design Patterns Reconsidered
Design Patterns ReconsideredDesign Patterns Reconsidered
Design Patterns ReconsideredAlex Miller
 

Más de Alex Miller (20)

Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)
 
Concurrent Stream Processing
Concurrent Stream ProcessingConcurrent Stream Processing
Concurrent Stream Processing
 
Stream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/joinStream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/join
 
Cracking clojure
Cracking clojureCracking clojure
Cracking clojure
 
Releasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic WebReleasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic Web
 
Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of Abstraction
 
Tree Editing with Zippers
Tree Editing with ZippersTree Editing with Zippers
Tree Editing with Zippers
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Blogging ZOMG
Blogging ZOMGBlogging ZOMG
Blogging ZOMG
 
Innovative Software
Innovative SoftwareInnovative Software
Innovative Software
 
Caching In The Cloud
Caching In The CloudCaching In The Cloud
Caching In The Cloud
 
Marshmallow Test
Marshmallow TestMarshmallow Test
Marshmallow Test
 
Strange Loop Conference 2009
Strange Loop Conference 2009Strange Loop Conference 2009
Strange Loop Conference 2009
 
Scaling Hibernate with Terracotta
Scaling Hibernate with TerracottaScaling Hibernate with Terracotta
Scaling Hibernate with Terracotta
 
Project Fortress
Project FortressProject Fortress
Project Fortress
 
Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 
Design Patterns Reconsidered
Design Patterns ReconsideredDesign Patterns Reconsidered
Design Patterns Reconsidered
 
Java 7 Preview
Java 7 PreviewJava 7 Preview
Java 7 Preview
 

Último

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Scaling Your Cache And Caching At Scale

  • 1. Scaling Your Cache & Caching at Scale Alex Miller @puredanger
  • 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?
  • 5. Memory Hierarchy Clock cycles to access Register 1 L1 cache 3 L2 cache 15 RAM 200 Disk 10000000 Remote disk 1000000000 1E+00 1E+01 1E+02 1E+03 1E+04 1E+05 1E+06 1E+07 1E+08 1E+09
  • 6. Facts of Life Register Fast Small Expensive L1 Cache L2 Cache Main Memory Local Disk Remote Disk Slow Big Cheap
  • 7. Caching to the rescue!
  • 8. Temporal Locality Hits: 0% Cache: Stream:
  • 9. Temporal Locality Hits: 0% Cache: Stream: Stream: Cache: Hits: 65%
  • 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. Temporal locality + Non-uniform distribution
  • 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
  • 13. The hidden benefit: reduces database load Memory Database sio ning i rov er p o f ov line
  • 14. A brief aside... • What is Ehcache? • What is Terracotta?
  • 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" overflowToDisk="false"/>
  • 16. Terracotta App Node App Node App Node App Node Terracotta Terracotta Server Server App Node App Node App Node App Node
  • 17. But things are not always so simple...
  • 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?
  • 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
  • 20. Eviction in Ehcache Evict with “Least Recently Used” policy: <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600" overflowToDisk="false"/>
  • 21. Spill to Disk in Ehcache Spill to disk: <diskStore path="java.io.tmpdir"/> <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600" overflowToDisk="true" maxElementsOnDisk="1000000" diskExpiryThreadIntervalSeconds="120" diskSpoolBufferSizeMB="30" />
  • 22. Terracotta Clustering Terracotta configuration: <terracottaConfig url="server1:9510,server2:9510"/> <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600" overflowToDisk="false"> <terracotta/> </cache>
  • 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?
  • 24. Expiration TTI=4 0 1 2 3 4 5 6 7 8 9 TTL=4
  • 25. TTI and TTL in Ehcache <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600" overflowToDisk="false"/>
  • 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>
  • 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”
  • 28. Pain of Loading • How do I pre-load the cache on startup? • How do I avoid re-loading the data on every node?
  • 29. Persistent Disk Store <diskStore path="java.io.tmpdir"/> <cache name="employees" maxElementsInMemory="1000" memoryStoreEvictionPolicy="LRU" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600" overflowToDisk="true" maxElementsOnDisk="1000000" diskExpiryThreadIntervalSeconds="120" diskSpoolBufferSizeMB="30" diskPersistent="true" />
  • 30. Bootstrap Cache Loader Bootstrap a new cache node from a peer: <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution. RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000" propertySeparator=",” /> On startup, create background thread to pull the existing cache data from another peer.
  • 31. Terracotta Persistence Nothing needed beyond setting up Terracotta clustering. Terracotta will automatically bootstrap: - the cache key set on startup - cache values on demand
  • 32. Pain of Duplication • How do I get failover capability while avoiding excessive duplication of data?
  • 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 overflow and availability • Use causal ordering to guarantee coherency
  • 35. Scalability Continuum causal ordering YES NO NO YES YES 2 or more 2 or more 2 or more JVMS JVMSmore 2 or 2 or more big JVMs 2 or more JVMs 2 or more 2 or more 2 or more JVMs 2 or more JVMs JVMs of lots # JVMs 1 JVM 2 or more JVMS JVMs 2 or more JVMS JVMs JVMs JVMs of lots JVMs Terracotta runtime Ehcache Ehcache RMI Ehcache disk store OSS Terracotta FX Terracotta FX Ehcache FX Ehcache FX Ehcache DX Ehcache EX and FX management management and control and control more scale 21
  • 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?
  • 38. Types of caches Name Communication Advantage Broadcast multicast low latency invalidation Replicated multicast offloads db Datagrid point-to-point scalable Distributed 2-tier point-to- all of the above point
  • 39. Common Data Patterns I/O pattern Locality Hot set Rate of change Catalog/ low low low customer Inventory high high high Conversations high high low Catalogs/customers Inventory Conversations • warm all the • fine-grained • sticky load data into locking balancer cache • write-behind to DB • disconnect • High TTL conversations from DB
  • 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
  • 42. Cache Warming • Explicitly record cache warming or loading as a testing phase • Possibly multiple warming phases
  • 44. Things to Change • Cache size • Read / write / other mix • Key distribution • Hot set • Key / value size and structure • # of nodes
  • 46. Things to Measure • Application throughput (TPS) • Application latency • OS: CPU, Memory, Disk, Network • JVM: Heap, Threads, GC
  • 47. Benchmark and Tune • Create a baseline • Run and modify parameters • Test, observe, hypothesize, verify • Keep a run log
  • 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
  • 50. I/O Waiting • Database • Connection pooling • Database tuning • Lazy connections • Remote services
  • 51. Locking and Concurrency Threads Locks Key Value 1 ge 2 t2 3 get 2 4 5 6 put 8 7 8 12 9 ut 10 p 11 12 13 14 15 16
  • 52. Locking and Concurrency Threads Locks Key Value get 2 1 2 get 2 3 4 5 6 put 8 7 8 9 10 put 12 11 12 13 14 15 16
  • 53. Objects and GC • Unnecessary object churn • Tune GC • Concurrent vs parallel collectors • Max heap • ...and so much more • Watch your GC pauses!!!
  • 54. Cache Efficiency • Watch hit rates and latencies • Cache hit - should be fast • Unless concurrency issue • Cache miss • Miss local vs • Miss disk / cluster
  • 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
  • 56. Cache Coherency • No replication (fastest) • RMI replication (loose coupling) • Terracotta replication (causal ordering) - way faster than strict ordering
  • 57. Latency Analysis • Profilers • Custom timers • Tier timings • Tracer bullets
  • 58. mumble-mumble* It’s time to add it to Terracotta. * lawyers won’t let me say more
  • 59. Thanks! • Twitter - @puredanger • Blog - http://tech.puredanger.com • Terracotta - http://terracotta.org • Ehcache - http://ehcache.org