KEMBAR78
Caching in applications still matters | PDF
© 2018 Software AG. All rights reserved.
[DEV5935] CACHING IN APPLICATIONS
STILL MATTERS !
Anthony Dahanne
@anthonydahanne
blog.dahanne.net
Henri Tremblay
@henri_tremblay
blog.tremblay.pro
LET US INTRODUCE OURSELVES
„Henri Tremblay, Senior Software Engineer @
Terracotta, a Software AG company
„Working on Ehcache mostly
„Lead developer of EasyMock and Objenesis
„Java Champion, Oracle Groundbreaker Ambassador
and Montréal JUG leader
„Anthony Dahanne, Senior Software Engineer @
Terracotta, a Software AG company
„Working on the Terracotta Management Console
„Working on Terracotta cloud deployments (Docker,
Kubernetes, AWS, etc.)
„Montréal JUG leader
AGENDA
Caching 101
Caching on the JVM
Clustered Caching with Terracotta Server
CACHING 101
CACHE DEFINITION
“Store of things
that will be required in the future,
and can be retrieved rapidly.”
from wiktionary.com
CACHE DEFINITION
A Map (key/value mappings) with
• capacity control (via eviction)
• freshness control (via expiry)
WHERE IS CACHING USED ?
LET’S START WITH THE CPU !
Core
L1 D-cache
L1 I-cache
L2 Cache
L3 Cache
Core
L1 D-cache
L1 I-cache
L2 Cache
Core
L1 D-cache
L1 I-cache
L2 Cache
Core
L1 D-cache
L1 I-cache
L2 Cache
Not that long ago (Intel I7 series) :
L1 Instruction Cache and Data Cache : 32KB
L2 Cache : 256KB
L3 Cache : 8MB
LATENCIES TO REMEMBER
L1 cache reference 0.5 ns
L2 cache reference 7 ns 14x L1 cache
Main memory reference 100 ns 20x L2 cache
Read 1 MB sequentially from memory 250,000 ns 250 us
Read 1 MB sequentially from SSD* 1,000,000 ns 1,000 us 1 ms ~1GB/sec SSD
Read 1 MB sequentially from disk 20,000,000 ns 20,000 us 20 ms 80x memory
Send packet CA->Netherlands->CA 150 ms 150 ms
from github.com/jboner
WHERE IS CACHING USED ?
Browser Caching CDN Caching
CPU Caching
Application Caching
Disk Caching
CACHING THEORY : AMDAHL’S LAW
“the theoretical speedup is always limited by the part of the task that cannot
benefit from the improvement.”, from Wikipedia
s : speedup in latency
p : percentage of the execution time
CACHING THEORY : THE LONG TAIL
CACHING GLOSSARY
• Hit : when the cache returns a value
• Miss : when the cache does not have a value
• Cold / Hot : when the cache is empty / full
WHAT TO MEASURE WHEN CACHING
• Cache Usage (empty ? full ?)
• HitRatio : hits / (misses + hits)
• HitRate : hits / second
• Eviction rate
• Size (in entries or bytes)
CACHING ON THE JVM
EHCACHE
HISTORY OF CACHING ON THE JVM
Review Ballot
2001
Public Review
2013
First Release
2003
2.0 “Express”
2010
2.3 Offheap
2011
JSR-107
First Release
2003
Terracotta 5
2016
TERRACOTTA
2009
Ehcache acquisition
Public Release
2014
1.1
2017
Ehcache 3
2016
EHCACHE REBOOT : EHCACHE3
• 3.0 (May 2016)
• Compatible with JSR-107
• User managed cache
• Copiers & Serializers
• Strong typing
• 3.1 (Jun 2016)
• Clustered tier added
• 3.2 (November 2016)
• High Availability with Terracotta
Server
• 3.5 (March 2018)
• Embracing Java 8 / dropping Java
6 support
• Documented resilience strategies
• 3.6 (September 2018)
• On-heap performance
improvement
JSR-107 AND EHCACHE 3 INTEGRATIONS
• JSR-107
• Spring (since 4.1, Spring Boot since 1.2)
• Guice
• jcache-cdi
• Hibernate (since 5.2.0)
• JHipster (since 3.12)
• Bootique (since 0.21)
• Ehcache 3
• Apache Shiro
• Apache Camel (since 2.18)
CACHING PATTERNS
• No Caching
CACHING PATTERNS
• No Caching
• Cache aside
Business logic
Cache
GET(K)
PUT(K,V)
NULL
LOAD(K)
SoR
K,V
K,V
V
THUNDERING HERD
Client 1
Client 2
Client n
...
Cache
GET(K)
PUT(K,V)
NULL
LOAD(K)
SoR
K,V
K,V
V
PREVENTING THE THUNDERING HERD
Client 1
Client 2
Client n
...
Cache
GET(K)
PUT(K,V)
NULL
LOAD(K)
SoR
K,V
K,V
V
Waiting...
CACHING PATTERNS
• No Caching
• Cache aside
• Cache through
V
Business logic Cache
GET(K) LOAD(K)
PUT(K,V)
K,V
SoR
K,VV
CLUSTERED CACHING
WITH TERRACOTTA SERVER
TERRACOTTA SERVER RESOURCES
from ehcache.org documentation
n
…
SEVERAL CLIENTS, ACTIVE PASSIVE TERRACOTTA CLUSTER
MySQL
Webapp with
Ehcache3 Clustered
Terracotta Server Terracotta Server
LINKS AND REFERENCES
• Old version of this conference by Anthony (Devoxx):
• Slideshare: https://www.slideshare.net/anthonydahanne/terracotta-ehcache-simpler-faster-distributed
• Youtube: https://www.youtube.com/watch?v=-j6cNZc5wYM
• Caching 101: Caching on the JVM (and beyond) by Louis Jacomet & Aurelien Broszniowski (Devoxx UK)
• Youtube: https://www.youtube.com/watch?v=FQfd8x29Ud8
• Ehcache3 documentation: http://www.ehcache.org/
• Ehcache3 and Terracotta Server demos: https://github.com/ehcache/ehcache3-samples
• The essence of caching, by Greg Luck
• Youtube: https://www.youtube.com/watch?v=TszcAWgCXD0
WHO HAS LEARNED SOMETHING
TODAY?
?
© 2018 Software AG. All rights reserved. For internal use only"28
http://ehcache.org
@ehcache
http://terracotta.org
@terracottatech
http://montreal-jug.org
@montrealjug
http://easymock.org
@easymock
Questions?
Anthony Dahanne
@anthonydahanne
blog.dahanne.net
Henri Tremblay
@henri_tremblay
blog.tremblay.pro

Caching in applications still matters

  • 1.
    © 2018 SoftwareAG. All rights reserved. [DEV5935] CACHING IN APPLICATIONS STILL MATTERS ! Anthony Dahanne @anthonydahanne blog.dahanne.net Henri Tremblay @henri_tremblay blog.tremblay.pro
  • 2.
    LET US INTRODUCEOURSELVES „Henri Tremblay, Senior Software Engineer @ Terracotta, a Software AG company „Working on Ehcache mostly „Lead developer of EasyMock and Objenesis „Java Champion, Oracle Groundbreaker Ambassador and Montréal JUG leader „Anthony Dahanne, Senior Software Engineer @ Terracotta, a Software AG company „Working on the Terracotta Management Console „Working on Terracotta cloud deployments (Docker, Kubernetes, AWS, etc.) „Montréal JUG leader
  • 3.
    AGENDA Caching 101 Caching onthe JVM Clustered Caching with Terracotta Server
  • 4.
  • 5.
    CACHE DEFINITION “Store ofthings that will be required in the future, and can be retrieved rapidly.” from wiktionary.com
  • 6.
    CACHE DEFINITION A Map(key/value mappings) with • capacity control (via eviction) • freshness control (via expiry)
  • 7.
    WHERE IS CACHINGUSED ? LET’S START WITH THE CPU ! Core L1 D-cache L1 I-cache L2 Cache L3 Cache Core L1 D-cache L1 I-cache L2 Cache Core L1 D-cache L1 I-cache L2 Cache Core L1 D-cache L1 I-cache L2 Cache Not that long ago (Intel I7 series) : L1 Instruction Cache and Data Cache : 32KB L2 Cache : 256KB L3 Cache : 8MB
  • 8.
    LATENCIES TO REMEMBER L1cache reference 0.5 ns L2 cache reference 7 ns 14x L1 cache Main memory reference 100 ns 20x L2 cache Read 1 MB sequentially from memory 250,000 ns 250 us Read 1 MB sequentially from SSD* 1,000,000 ns 1,000 us 1 ms ~1GB/sec SSD Read 1 MB sequentially from disk 20,000,000 ns 20,000 us 20 ms 80x memory Send packet CA->Netherlands->CA 150 ms 150 ms from github.com/jboner
  • 9.
    WHERE IS CACHINGUSED ? Browser Caching CDN Caching CPU Caching Application Caching Disk Caching
  • 10.
    CACHING THEORY :AMDAHL’S LAW “the theoretical speedup is always limited by the part of the task that cannot benefit from the improvement.”, from Wikipedia s : speedup in latency p : percentage of the execution time
  • 11.
    CACHING THEORY :THE LONG TAIL
  • 12.
    CACHING GLOSSARY • Hit: when the cache returns a value • Miss : when the cache does not have a value • Cold / Hot : when the cache is empty / full
  • 13.
    WHAT TO MEASUREWHEN CACHING • Cache Usage (empty ? full ?) • HitRatio : hits / (misses + hits) • HitRate : hits / second • Eviction rate • Size (in entries or bytes)
  • 14.
  • 15.
    EHCACHE HISTORY OF CACHINGON THE JVM Review Ballot 2001 Public Review 2013 First Release 2003 2.0 “Express” 2010 2.3 Offheap 2011 JSR-107 First Release 2003 Terracotta 5 2016 TERRACOTTA 2009 Ehcache acquisition Public Release 2014 1.1 2017 Ehcache 3 2016
  • 16.
    EHCACHE REBOOT :EHCACHE3 • 3.0 (May 2016) • Compatible with JSR-107 • User managed cache • Copiers & Serializers • Strong typing • 3.1 (Jun 2016) • Clustered tier added • 3.2 (November 2016) • High Availability with Terracotta Server • 3.5 (March 2018) • Embracing Java 8 / dropping Java 6 support • Documented resilience strategies • 3.6 (September 2018) • On-heap performance improvement
  • 17.
    JSR-107 AND EHCACHE3 INTEGRATIONS • JSR-107 • Spring (since 4.1, Spring Boot since 1.2) • Guice • jcache-cdi • Hibernate (since 5.2.0) • JHipster (since 3.12) • Bootique (since 0.21) • Ehcache 3 • Apache Shiro • Apache Camel (since 2.18)
  • 18.
  • 19.
    CACHING PATTERNS • NoCaching • Cache aside Business logic Cache GET(K) PUT(K,V) NULL LOAD(K) SoR K,V K,V V
  • 20.
    THUNDERING HERD Client 1 Client2 Client n ... Cache GET(K) PUT(K,V) NULL LOAD(K) SoR K,V K,V V
  • 21.
    PREVENTING THE THUNDERINGHERD Client 1 Client 2 Client n ... Cache GET(K) PUT(K,V) NULL LOAD(K) SoR K,V K,V V Waiting...
  • 22.
    CACHING PATTERNS • NoCaching • Cache aside • Cache through V Business logic Cache GET(K) LOAD(K) PUT(K,V) K,V SoR K,VV
  • 23.
  • 24.
    TERRACOTTA SERVER RESOURCES fromehcache.org documentation
  • 25.
    n … SEVERAL CLIENTS, ACTIVEPASSIVE TERRACOTTA CLUSTER MySQL Webapp with Ehcache3 Clustered Terracotta Server Terracotta Server
  • 26.
    LINKS AND REFERENCES •Old version of this conference by Anthony (Devoxx): • Slideshare: https://www.slideshare.net/anthonydahanne/terracotta-ehcache-simpler-faster-distributed • Youtube: https://www.youtube.com/watch?v=-j6cNZc5wYM • Caching 101: Caching on the JVM (and beyond) by Louis Jacomet & Aurelien Broszniowski (Devoxx UK) • Youtube: https://www.youtube.com/watch?v=FQfd8x29Ud8 • Ehcache3 documentation: http://www.ehcache.org/ • Ehcache3 and Terracotta Server demos: https://github.com/ehcache/ehcache3-samples • The essence of caching, by Greg Luck • Youtube: https://www.youtube.com/watch?v=TszcAWgCXD0
  • 27.
    WHO HAS LEARNEDSOMETHING TODAY? ?
  • 28.
    © 2018 SoftwareAG. All rights reserved. For internal use only"28 http://ehcache.org @ehcache http://terracotta.org @terracottatech http://montreal-jug.org @montrealjug http://easymock.org @easymock Questions? Anthony Dahanne @anthonydahanne blog.dahanne.net Henri Tremblay @henri_tremblay blog.tremblay.pro