Caching reboot: javax.cache & Ehcache 3

300 views

Published on

Session given on June 22nd 2015 during Devoxx Poland 2015

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
300
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Caching reboot: javax.cache & Ehcache 3

  1. 1. @ljacomet#ehcachedvx Caching reboot javax.cache & Ehcache 3
  2. 2. @ljacomet#ehcachedvx Agenda
  3. 3. @ljacomet#ehcachedvx Agenda • Why a cache?
  4. 4. @ljacomet#ehcachedvx Agenda • Why a cache? • JSR-107 • APIs and features • Provider extensions
  5. 5. @ljacomet#ehcachedvx Agenda • Why a cache? • JSR-107 • APIs and features • Provider extensions • Caching in an application • Cache aside • Cache through
  6. 6. @ljacomet#ehcachedvx Agenda • Why a cache? • JSR-107 • APIs and features • Provider extensions • Caching in an application • Cache aside • Cache through • Resilience strategy
  7. 7. @ljacomet#ehcachedvx Agenda • Why a cache? • JSR-107 • APIs and features • Provider extensions • Caching in an application • Cache aside • Cache through • Resilience strategy • Extras
  8. 8. @ljacomet#ehcachedvx Who am I? • Louis Jacomet • Software engineer, closer to 40 than 20 and still coding! • Engineer at Terracotta (Software AG) since 2013 • Working on Ehcache OS and EE mostly • team manager and fiddling in infrastructure • No idea how to do a (nice) UI - especially a web one • Enjoys concurrency, thinking hard about APIs, …
  9. 9. Why a cache?
  10. 10. @ljacomet#ehcachedvx Why a cache?
  11. 11. @ljacomet#ehcachedvx What were these numbers?
  12. 12. @ljacomet#ehcachedvx What were these numbers? • Minute: • L1$cache$access$$$$$$$$$$$$$$$0,5$s$$$One$heartbeat$ • Branch$misprediction$$$$$$$$$$5$$$s$$$Yawn$ • L2$cache$access$$$$$$$$$$$$$$$7$$$s$$$Long$yawn$ • Mutex$lock/unlock$$$$$$$$$$$$25$$$s$$$Coffee$preparation
  13. 13. @ljacomet#ehcachedvx What were these numbers? • Minute: • L1$cache$access$$$$$$$$$$$$$$$0,5$s$$$One$heartbeat$ • Branch$misprediction$$$$$$$$$$5$$$s$$$Yawn$ • L2$cache$access$$$$$$$$$$$$$$$7$$$s$$$Long$yawn$ • Mutex$lock/unlock$$$$$$$$$$$$25$$$s$$$Coffee$preparation • Hour: • RAM$access$$$$$$$$$$$$$$$$$$100$$$s$$$Brushing$your$teeth$ • Compressing$1K$$$$$$$$$$$$$$$50$$$min$One$TV$show$episode
  14. 14. @ljacomet#ehcachedvx What were these numbers? • Minute: • L1$cache$access$$$$$$$$$$$$$$$0,5$s$$$One$heartbeat$ • Branch$misprediction$$$$$$$$$$5$$$s$$$Yawn$ • L2$cache$access$$$$$$$$$$$$$$$7$$$s$$$Long$yawn$ • Mutex$lock/unlock$$$$$$$$$$$$25$$$s$$$Coffee$preparation • Hour: • RAM$access$$$$$$$$$$$$$$$$$$100$$$s$$$Brushing$your$teeth$ • Compressing$1K$$$$$$$$$$$$$$$50$$$min$One$TV$show$episode • Day: • 2KB$sent$on$1GB$network$$$$$$$5,5$h$$$Your$work$afternoon
  15. 15. @ljacomet#ehcachedvx What were these numbers?
  16. 16. @ljacomet#ehcachedvx What were these numbers? • Week: • Random$read$SSD$$$$$$$$$$$$$$$$$1,7$d$$$$$$$A$weekNend$ • Sequence$read$1$MB$from$RAM$$$$$2,9$d$$$$$$$A$long$weekNend$ • Round$trip$in$datacenter$$$$$$$$5,8$d$$$$$$$Holidays$ • Sequence$read$1MB$from$SSD$$$$$11,6$d$$$$$$$Two$weeks$delivery
  17. 17. @ljacomet#ehcachedvx What were these numbers? • Week: • Random$read$SSD$$$$$$$$$$$$$$$$$1,7$d$$$$$$$A$weekNend$ • Sequence$read$1$MB$from$RAM$$$$$2,9$d$$$$$$$A$long$weekNend$ • Round$trip$in$datacenter$$$$$$$$5,8$d$$$$$$$Holidays$ • Sequence$read$1MB$from$SSD$$$$$11,6$d$$$$$$$Two$weeks$delivery • Year: • Seek$on$rotational$HDD$$$$$$$$$16,5$weeks$$$School$semester$ • Sequence$read$1MB$from$HDD$$$$$$7,8$months$$Almost$a$baby
  18. 18. @ljacomet#ehcachedvx What were these numbers? • Week: • Random$read$SSD$$$$$$$$$$$$$$$$$1,7$d$$$$$$$A$weekNend$ • Sequence$read$1$MB$from$RAM$$$$$2,9$d$$$$$$$A$long$weekNend$ • Round$trip$in$datacenter$$$$$$$$5,8$d$$$$$$$Holidays$ • Sequence$read$1MB$from$SSD$$$$$11,6$d$$$$$$$Two$weeks$delivery • Year: • Seek$on$rotational$HDD$$$$$$$$$16,5$weeks$$$School$semester$ • Sequence$read$1MB$from$HDD$$$$$$7,8$months$$Almost$a$baby • Decade: • Data$roundNtrip$around$the$world$4,8$years$$PhD$thesis
  19. 19. @ljacomet#ehcachedvx So what is a cache?
  20. 20. @ljacomet#ehcachedvx So what is a cache? • Data structure holding a temporary copy of some data
  21. 21. @ljacomet#ehcachedvx So what is a cache? • Data structure holding a temporary copy of some data • Trade off between higher memory usage for reduced latency
  22. 22. @ljacomet#ehcachedvx So what is a cache? • Data structure holding a temporary copy of some data • Trade off between higher memory usage for reduced latency • Targets : • Data which is reused • Data which is expensive to compute or retrieve
  23. 23. @ljacomet#ehcachedvx Ehcache (3)
  24. 24. @ljacomet#ehcachedvx Ehcache (3) • New version, fully integrated with JSR-107 • Breaks compatibility with 2.x line
  25. 25. @ljacomet#ehcachedvx Ehcache (3) • New version, fully integrated with JSR-107 • Breaks compatibility with 2.x line • Developed in the open • https://github.com/ehcache/ehcache3
  26. 26. @ljacomet#ehcachedvx Ehcache (3) • New version, fully integrated with JSR-107 • Breaks compatibility with 2.x line • Developed in the open • https://github.com/ehcache/ehcache3 • Public dev meeting, almost weekly, on hangouts on air • Recordings available onYoutube
  27. 27. JSR-107 - javx.caching
  28. 28. @ljacomet#ehcachedvx JSR-107
  29. 29. @ljacomet#ehcachedvx JSR-107 • Ancient JSR • Started in 2001 • Approved a year ago (March 2014)
  30. 30. @ljacomet#ehcachedvx JSR-107 • Ancient JSR • Started in 2001 • Approved a year ago (March 2014) • javax.caching API + TCK + reference implementation • Eases integration of a cache in frameworks
  31. 31. @ljacomet#ehcachedvx JSR-107 • Ancient JSR • Started in 2001 • Approved a year ago (March 2014) • javax.caching API + TCK + reference implementation • Eases integration of a cache in frameworks • Ongoing discussions about a version 2.0 • Async API for example
  32. 32. @ljacomet#ehcachedvx JSR-107 : Features
  33. 33. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache
  34. 34. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update
  35. 35. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update •Integration •CacheLoader / Writer
  36. 36. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update •Integration •CacheLoader / Writer •CacheEntryListener •Created/Updated •Removed/Expired •Get old value
  37. 37. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update •Integration •CacheLoader / Writer •CacheEntryListener •Created/Updated •Removed/Expired •Get old value •Entry processor
  38. 38. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update •Integration •CacheLoader / Writer •CacheEntryListener •Created/Updated •Removed/Expired •Get old value •Entry processor •Annotations
  39. 39. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update •Integration •CacheLoader / Writer •CacheEntryListener •Created/Updated •Removed/Expired •Get old value •Entry processor •Annotations •MBeans, exposing •Configuration •Statistics
  40. 40. @ljacomet#ehcachedvx JSR-107 : Features •CacheManager / Cache •Expiration •Creation/Access/Update •Integration •CacheLoader / Writer •CacheEntryListener •Created/Updated •Removed/Expired •Get old value •Entry processor •Annotations •MBeans, exposing •Configuration •Statistics •No capacity control !
  41. 41. @YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx Demo
  42. 42. @YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx And my XML??
  43. 43. @ljacomet#ehcachedvx JSR-107 configuration • Programmatic only • Enter the world of provider extensions • Ehcache 3 expects CacheManager URI to resolve to a configuration file • From there, multiple scenarios are possible
  44. 44. @YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx Demo
  45. 45. Caching in an application
  46. 46. @ljacomet#ehcachedvx public BusinessObject computeAndWin(String param1, String param2) { 
 String key = createKey(param1, param2);
 BusinessObject cachedResult = cache.get(key);
 if (cachedResult == null) {
 cachedResult = loadAndCompute(param1, param2);
 cache.put(key, cachedResult);
 }
 return cachedResult;
 } Cache Aside
  47. 47. @ljacomet#ehcachedvx Cache Aside : not so easy
  48. 48. @ljacomet#ehcachedvx Cache Aside : not so easy • Requires synchronisation between • cache • and system of record
  49. 49. @ljacomet#ehcachedvx Cache Aside : not so easy • Requires synchronisation between • cache • and system of record • JSR-107 has no locking option
  50. 50. @ljacomet#ehcachedvx Cache Aside : not so easy • Requires synchronisation between • cache • and system of record • JSR-107 has no locking option • Potential to be hairy code quite fast
  51. 51. @ljacomet#ehcachedvx Cache Aside: options
  52. 52. @ljacomet#ehcachedvx Cache Aside: options • Rely on the abstraction of your framework • Spring Caching • Hibernate • …
  53. 53. @ljacomet#ehcachedvx Cache Aside: options • Rely on the abstraction of your framework • Spring Caching • Hibernate • … • Alternatives ?
  54. 54. @ljacomet#ehcachedvx public BusinessObject computeAndWin(String param1, String param2) { 
 return cache.get(createKey(param1, param2));
 } Cache Through
  55. 55. @ljacomet#ehcachedvx Cache Through Cache Application code RDBMS
  56. 56. @ljacomet#ehcachedvx Cache Through Cache Application code RDBMS
  57. 57. @ljacomet#ehcachedvx Cache Through Cache Application code RDBMS
  58. 58. @ljacomet#ehcachedvx Cache Through Cache Application code RDBMS
  59. 59. @ljacomet#ehcachedvx Cache Through Cache Application code RDBMS
  60. 60. @ljacomet#ehcachedvx Cache Through
  61. 61. @ljacomet#ehcachedvx Cache Through • get* => CacheLoader • cache miss indicates no data available
  62. 62. @ljacomet#ehcachedvx Cache Through • get* => CacheLoader • cache miss indicates no data available • put* => CacheWriter • Each use means writing to the system of record
  63. 63. @ljacomet#ehcachedvx Cache Through • get* => CacheLoader • cache miss indicates no data available • put* => CacheWriter • Each use means writing to the system of record • Constraints:APIs of CacheLoader / CacheWriter
  64. 64. @ljacomet#ehcachedvx Cache Through : JSR-107
  65. 65. @ljacomet#ehcachedvx Cache Through : JSR-107 • Peculiar behaviours
  66. 66. @ljacomet#ehcachedvx Cache Through : JSR-107 • Peculiar behaviours • putIfAbsent(K key,V value): boolean
  67. 67. @ljacomet#ehcachedvx Cache Through : JSR-107 • Peculiar behaviours • putIfAbsent(K key,V value): boolean • Ignores CacheLoader but not CacheWriter
  68. 68. @ljacomet#ehcachedvx Cache Through : JSR-107 • Peculiar behaviours • putIfAbsent(K key,V value): boolean • Ignores CacheLoader but not CacheWriter • Ehcache 3 allows to tweak this behaviour through configuration
  69. 69. @ljacomet#ehcachedvx Cache Through : JSR-107 • Peculiar behaviours • putIfAbsent(K key,V value): boolean • Ignores CacheLoader but not CacheWriter • Ehcache 3 allows to tweak this behaviour through configuration • Behaviour consistent across atomic operations
  70. 70. @ljacomet#ehcachedvx Cache Through : Write Behind
  71. 71. @ljacomet#ehcachedvx Cache Through : Write Behind • Asynchronous writes to the the system of record • User thread no longer suffers from the latency of the write
  72. 72. @ljacomet#ehcachedvx Cache Through : Write Behind • Asynchronous writes to the the system of record • User thread no longer suffers from the latency of the write • Introduces its own complexities • Write queue: persistent or not? • Mutation guarantee: once and only once? at least once? • What about cache eviction?
  73. 73. Resilience strategy
  74. 74. @ljacomet#ehcachedvx Resilience strategy
  75. 75. @ljacomet#ehcachedvx Resilience strategy • Proposition: “A cache level error should not be the cause for a user level error “
  76. 76. @ljacomet#ehcachedvx Resilience strategy • Proposition: “A cache level error should not be the cause for a user level error “ P.S.This is ongoing development and subject to change
  77. 77. @ljacomet#ehcachedvx Ehcache 3
  78. 78. @ljacomet#ehcachedvx Ehcache 3 • Handle errors internally as much as possible
  79. 79. @ljacomet#ehcachedvx Ehcache 3 • Handle errors internally as much as possible • In memory cache => No exceptions in the execution thread
  80. 80. @ljacomet#ehcachedvx Ehcache 3 • Handle errors internally as much as possible • In memory cache => No exceptions in the execution thread • Default is to log errors, will be configurable / replaceable
  81. 81. @ljacomet#ehcachedvx Ehcache 3 • Handle errors internally as much as possible • In memory cache => No exceptions in the execution thread • Default is to log errors, will be configurable / replaceable • Advanced setups (Distributed + write behind for example)
  82. 82. @ljacomet#ehcachedvx Ehcache 3 • Handle errors internally as much as possible • In memory cache => No exceptions in the execution thread • Default is to log errors, will be configurable / replaceable • Advanced setups (Distributed + write behind for example) • Force user to think about the use cases where the cache can become inconsistent
  83. 83. @ljacomet#ehcachedvx Ehcache 3 • Handle errors internally as much as possible • In memory cache => No exceptions in the execution thread • Default is to log errors, will be configurable / replaceable • Advanced setups (Distributed + write behind for example) • Force user to think about the use cases where the cache can become inconsistent • Will be required to configure a handler for these cases
  84. 84. Ehcache 3 and Terracotta
  85. 85. @ljacomet#ehcachedvx Offheap solution
  86. 86. @ljacomet#ehcachedvx Offheap solution • Solving Garbage Collector pauses on large heaps
  87. 87. @ljacomet#ehcachedvx Offheap solution • Solving Garbage Collector pauses on large heaps • Enables extreme cache scale up • Multi TerraBytes … as long as your server allows it
  88. 88. @ljacomet#ehcachedvx Offheap solution • Solving Garbage Collector pauses on large heaps • Enables extreme cache scale up • Multi TerraBytes … as long as your server allows it • Production tested implementation • 2.0.0 available on GitHub • https://github.com/Terracotta-OSS/offheap-store
  89. 89. @ljacomet#ehcachedvx Clustering solution
  90. 90. @ljacomet#ehcachedvx Clustering solution • Sharing of data across multiple JVMs
  91. 91. @ljacomet#ehcachedvx Clustering solution • Sharing of data across multiple JVMs • Terracotta 4.3.0 again has an OpenSource offering • Integration with Ehcache 2.10.0
  92. 92. @ljacomet#ehcachedvx Clustering solution • Sharing of data across multiple JVMs • Terracotta 4.3.0 again has an OpenSource offering • Integration with Ehcache 2.10.0 • Ehcache 3 will have clustering option in OpenSource • Code starting to appear on GitHub
  93. 93. @YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx Questions ?

×