Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Where is my cache?
Architectural patterns for caching
microservices
Rafał Leszko
@RafalLeszko
rafalleszko.com
Hazelcast
About me
● Integration Team Lead at Hazelcast
● Worked at Google and CERN
● Author of the book "Continuous Delivery
with D...
About Hazelcast
● Distributed Company
● Open Source Software
● 140+ Employees
● Products:
○ Hazelcast IMDG
○ Hazelcast Jet...
● Introduction
● Caching Architectural Patterns
○ Embedded
○ Embedded Distributed
○ Client-Server
○ Cloud
○ Sidecar
○ Reve...
Why Caching?
● Performance
○ Decrease latency
○ Reduce load
● Resilience
○ High availability
○ Lower downtime
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Microservice World
Microservice World
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
cache
cache
c...
Microservice World
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
cache
cache
c...
Microservice World
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
cache
cache
c...
● Introduction ✔
● Caching Architectural Patterns
○ Embedded
○ Embedded Distributed
○ Client-Server
○ Cloud
○ Sidecar
○ Re...
1. Embedded
Application
Load Balancer
Cache
Application
Cache
Request
Embedded Cache
private ConcurrentHashMap<String, String> cache =
new ConcurrentHashMap<>();
private String processRequest(String request)...
private ConcurrentHashMap<String, String> cache =
new ConcurrentHashMap<>();
private String processRequest(String request)...
● No Eviction Policies
● No Max Size Limit
(OutOfMemoryError)
● No Statistics
● No built-in Cache Loaders
● No Expiration ...
CacheBuilder.newBuilder()
.initialCapacity(300)
.expireAfterAccess(Duration.ofMinutes(10))
.maximumSize(1000)
.build();
Em...
Embedded Cache (Java libraries)
CacheBuilder.newBuilder()
.initialCapacity(300)
.expireAfterAccess(Duration.ofMinutes(10))...
Caching Application Layer
@Service
public class BookService {
@Cacheable("books")
public String getBookNameByIsbn(String i...
Caching Application Layer
@Service
public class BookService {
@Cacheable("books")
public String getBookNameByIsbn(String i...
Application
Load Balancer
Cache
Application
Cache
Request
Embedded Cache
1*. Embedded Distributed
Application
Application
Load Balancer
Cache
Cache
Request
Hazelcast
Cluster
Embedded Distributed Cache
@Configuration
public class HazelcastConfiguration {
@Bean
CacheManager cacheManager() {
return new HazelcastCacheManager(...
DEMO
Hazelcast Discovery Plugins
Hazelcast Discovery Plugins
Hazelcast Discovery Plugins
Application
Application
Load Balancer
Cache
Cache
Request
Hazelcast
Cluster
Embedded Distributed Cache
Embedded Cache
Pros Cons
● Simple configuration /
deployment
● Low-latency data access
● No separate Ops Team
needed
● Not...
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server
○ Cloud
○ Sidecar
...
2. Client-Server
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Separate Management:
● backups
● (auto) sca...
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Client-Server Cache
Client-Server Cache
Client-Server Cache
$ hz start
Starting Hazelcast Cache Server (standalone)
Client-Server Cache
Starting Hazelcast Cache Server (Kubernetes)
$ helm install hazelcast/hazelcast
Client-Server Cache
Hazelcast Client (Kubernetes):
@Configuration
public class HazelcastClientConfiguration {
@Bean
CacheM...
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Ops Team
Separate Management:
● backups
● (...
2*. Cloud
Application
Load Balancer
Application
Request
Cloud (Cache as a Service)
Application
Load Balancer
Application
Request
Cloud (Cache as a Service)
Management:
● backups
● (auto) scaling
● security...
Application
Load Balancer
Application
Request
Cloud (Cache as a Service)
Management:
● backups
● (auto) scaling
● security...
Application
Load Balancer
Application
Request
Cloud (Cache as a Service)
@Configuration
public class HazelcastCloudConfiguration {
@Bean
CacheManager cacheManager() {
ClientConfig clientConfig = ...
DEMO
cloud.hazelcast.com
Client-Server (Cloud) Cache
Pros
● Data separate from
applications
● Separate management
(scaling, backup)
● Programming-l...
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server ✔
○ Cloud ✔
○ Side...
3. Sidecar
Kubernetes Service
(Load Balancer)
Request
Hazelcast
Cluster
Kubernetes POD
Application Container
Cache Container
Applicat...
Sidecar Cache
Similar to Embedded:
● the same physical machine
● the same resource pool
● scales up and down together
● no...
@Configuration
public class HazelcastSidecarConfiguration {
@Bean
CacheManager cacheManager() {
ClientConfig clientConfig ...
Sidecar Cache
apiVersion: apps/v1
kind: Deployment
...
spec:
template:
spec:
containers:
- name: application
image: leszko...
Sidecar Cache
Pros Cons
● Simple configuration
● Programming-language
agnostic
● Low latency
● Some isolation of data and
...
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server ✔
○ Cloud ✔
○ Side...
4. Reverse Proxy
Application
Load
Balancer
Cache
Application
Request
Reverse Proxy Cache
Reverse Proxy Cache
http {
...
proxy_cache_path /data/nginx/cache
keys_zone=one:10m;
...
}
● Only for HTTP
● Not distributed
● No High Availability
● Data stored on the disk
NGINX Reverse Proxy Cache Issues
NGINX Reverse Proxy Cache Issues
● Only for HTTP
● Not distributed
● No High Availability
● Data stored on the disk
4*. Reverse Proxy Sidecar
Kubernetes Service
(Load Balancer)
Request
Hazelcast
Cluster
Kubernetes POD
Application Container
Reverse Proxy Cache
Cont...
Good
Reverse Proxy Sidecar Cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Reverse Proxy Sidecar Cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Reverse Proxy Sidecar Cache
apiVersion: apps/v1
kind: Deployment
...
spec:
template:
spec:
initContainers:
- name: init-ne...
Reverse Proxy
Cache
Container
Application
Container
eth0
:80
Reverse Proxy Sidecar Cache
:80
Pod
:8000
lo
:80
Reverse Proxy Sidecar Cache
#!/bin/bash
# Forward TCP traffic on port 80 to port 8000 on the eth0 interface.
iptables -t n...
Reverse Proxy Sidecar Cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Reverse Proxy Sidecar Cache (Istio)
Reverse Proxy Sidecar Cache (Istio)
Bad
Reverse Proxy Cache
@CacheEvict(value = "someValue", allEntries = true)
public void evictAllCacheValues() {}
Application C...
Reverse Proxy Cache
@CacheEvict(value = "someValue", allEntries = true)
public void evictAllCacheValues() {}
Application C...
Reverse Proxy (Sidecar) Cache
Pros Cons
● Configuration-based (no
need to change
applications)
● Programming-language
agno...
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server ✔
○ Cloud ✔
○ Side...
Summary
application-aware?
application-aware?
containers?
no
application-aware?
containers?
Reverse Proxy
no
no
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
no
yes no
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
yes no
yes no
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
...
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
...
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
...
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
...
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
...
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
...
Resources
● Code for this talk:
https://github.com/leszko/caching-patterns
● Hazelcast Sidecar Container Pattern:
https://...
Thank You!
Rafał Leszko
@RafalLeszko
rafalleszko.com
Where is my cache  architectural patterns for caching microservices by example
Where is my cache  architectural patterns for caching microservices by example
Where is my cache  architectural patterns for caching microservices by example
Upcoming SlideShare
Loading in …5
×

of

Where is my cache  architectural patterns for caching microservices by example Slide 1 Where is my cache  architectural patterns for caching microservices by example Slide 2 Where is my cache  architectural patterns for caching microservices by example Slide 3 Where is my cache  architectural patterns for caching microservices by example Slide 4 Where is my cache  architectural patterns for caching microservices by example Slide 5 Where is my cache  architectural patterns for caching microservices by example Slide 6 Where is my cache  architectural patterns for caching microservices by example Slide 7 Where is my cache  architectural patterns for caching microservices by example Slide 8 Where is my cache  architectural patterns for caching microservices by example Slide 9 Where is my cache  architectural patterns for caching microservices by example Slide 10 Where is my cache  architectural patterns for caching microservices by example Slide 11 Where is my cache  architectural patterns for caching microservices by example Slide 12 Where is my cache  architectural patterns for caching microservices by example Slide 13 Where is my cache  architectural patterns for caching microservices by example Slide 14 Where is my cache  architectural patterns for caching microservices by example Slide 15 Where is my cache  architectural patterns for caching microservices by example Slide 16 Where is my cache  architectural patterns for caching microservices by example Slide 17 Where is my cache  architectural patterns for caching microservices by example Slide 18 Where is my cache  architectural patterns for caching microservices by example Slide 19 Where is my cache  architectural patterns for caching microservices by example Slide 20 Where is my cache  architectural patterns for caching microservices by example Slide 21 Where is my cache  architectural patterns for caching microservices by example Slide 22 Where is my cache  architectural patterns for caching microservices by example Slide 23 Where is my cache  architectural patterns for caching microservices by example Slide 24 Where is my cache  architectural patterns for caching microservices by example Slide 25 Where is my cache  architectural patterns for caching microservices by example Slide 26 Where is my cache  architectural patterns for caching microservices by example Slide 27 Where is my cache  architectural patterns for caching microservices by example Slide 28 Where is my cache  architectural patterns for caching microservices by example Slide 29 Where is my cache  architectural patterns for caching microservices by example Slide 30 Where is my cache  architectural patterns for caching microservices by example Slide 31 Where is my cache  architectural patterns for caching microservices by example Slide 32 Where is my cache  architectural patterns for caching microservices by example Slide 33 Where is my cache  architectural patterns for caching microservices by example Slide 34 Where is my cache  architectural patterns for caching microservices by example Slide 35 Where is my cache  architectural patterns for caching microservices by example Slide 36 Where is my cache  architectural patterns for caching microservices by example Slide 37 Where is my cache  architectural patterns for caching microservices by example Slide 38 Where is my cache  architectural patterns for caching microservices by example Slide 39 Where is my cache  architectural patterns for caching microservices by example Slide 40 Where is my cache  architectural patterns for caching microservices by example Slide 41 Where is my cache  architectural patterns for caching microservices by example Slide 42 Where is my cache  architectural patterns for caching microservices by example Slide 43 Where is my cache  architectural patterns for caching microservices by example Slide 44 Where is my cache  architectural patterns for caching microservices by example Slide 45 Where is my cache  architectural patterns for caching microservices by example Slide 46 Where is my cache  architectural patterns for caching microservices by example Slide 47 Where is my cache  architectural patterns for caching microservices by example Slide 48 Where is my cache  architectural patterns for caching microservices by example Slide 49 Where is my cache  architectural patterns for caching microservices by example Slide 50 Where is my cache  architectural patterns for caching microservices by example Slide 51 Where is my cache  architectural patterns for caching microservices by example Slide 52 Where is my cache  architectural patterns for caching microservices by example Slide 53 Where is my cache  architectural patterns for caching microservices by example Slide 54 Where is my cache  architectural patterns for caching microservices by example Slide 55 Where is my cache  architectural patterns for caching microservices by example Slide 56 Where is my cache  architectural patterns for caching microservices by example Slide 57 Where is my cache  architectural patterns for caching microservices by example Slide 58 Where is my cache  architectural patterns for caching microservices by example Slide 59 Where is my cache  architectural patterns for caching microservices by example Slide 60 Where is my cache  architectural patterns for caching microservices by example Slide 61 Where is my cache  architectural patterns for caching microservices by example Slide 62 Where is my cache  architectural patterns for caching microservices by example Slide 63 Where is my cache  architectural patterns for caching microservices by example Slide 64 Where is my cache  architectural patterns for caching microservices by example Slide 65 Where is my cache  architectural patterns for caching microservices by example Slide 66 Where is my cache  architectural patterns for caching microservices by example Slide 67 Where is my cache  architectural patterns for caching microservices by example Slide 68 Where is my cache  architectural patterns for caching microservices by example Slide 69 Where is my cache  architectural patterns for caching microservices by example Slide 70 Where is my cache  architectural patterns for caching microservices by example Slide 71 Where is my cache  architectural patterns for caching microservices by example Slide 72 Where is my cache  architectural patterns for caching microservices by example Slide 73 Where is my cache  architectural patterns for caching microservices by example Slide 74 Where is my cache  architectural patterns for caching microservices by example Slide 75 Where is my cache  architectural patterns for caching microservices by example Slide 76 Where is my cache  architectural patterns for caching microservices by example Slide 77 Where is my cache  architectural patterns for caching microservices by example Slide 78 Where is my cache  architectural patterns for caching microservices by example Slide 79 Where is my cache  architectural patterns for caching microservices by example Slide 80 Where is my cache  architectural patterns for caching microservices by example Slide 81 Where is my cache  architectural patterns for caching microservices by example Slide 82 Where is my cache  architectural patterns for caching microservices by example Slide 83 Where is my cache  architectural patterns for caching microservices by example Slide 84 Where is my cache  architectural patterns for caching microservices by example Slide 85 Where is my cache  architectural patterns for caching microservices by example Slide 86 Where is my cache  architectural patterns for caching microservices by example Slide 87 Where is my cache  architectural patterns for caching microservices by example Slide 88 Where is my cache  architectural patterns for caching microservices by example Slide 89 Where is my cache  architectural patterns for caching microservices by example Slide 90 Where is my cache  architectural patterns for caching microservices by example Slide 91 Where is my cache  architectural patterns for caching microservices by example Slide 92 Where is my cache  architectural patterns for caching microservices by example Slide 93 Where is my cache  architectural patterns for caching microservices by example Slide 94 Where is my cache  architectural patterns for caching microservices by example Slide 95 Where is my cache  architectural patterns for caching microservices by example Slide 96
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

0 Likes

Share

Download to read offline

Where is my cache architectural patterns for caching microservices by example

Download to read offline

Where is my cache architectural patterns for caching microservices by example

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Where is my cache architectural patterns for caching microservices by example

  1. 1. Where is my cache? Architectural patterns for caching microservices Rafał Leszko @RafalLeszko rafalleszko.com Hazelcast
  2. 2. About me ● Integration Team Lead at Hazelcast ● Worked at Google and CERN ● Author of the book "Continuous Delivery with Docker and Jenkins" ● Trainer and conference speaker ● Live in Kraków, Poland
  3. 3. About Hazelcast ● Distributed Company ● Open Source Software ● 140+ Employees ● Products: ○ Hazelcast IMDG ○ Hazelcast Jet ○ Hazelcast Cloud @Hazelcast www.hazelcast.com
  4. 4. ● Introduction ● Caching Architectural Patterns ○ Embedded ○ Embedded Distributed ○ Client-Server ○ Cloud ○ Sidecar ○ Reverse Proxy ○ Reverse Proxy Sidecar ● Summary Agenda
  5. 5. Why Caching? ● Performance ○ Decrease latency ○ Reduce load ● Resilience ○ High availability ○ Lower downtime
  6. 6. Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby Microservice World
  7. 7. Microservice World Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby cache cache cache cache
  8. 8. Microservice World Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby cache cache cache
  9. 9. Microservice World Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby cache cache cache
  10. 10. ● Introduction ✔ ● Caching Architectural Patterns ○ Embedded ○ Embedded Distributed ○ Client-Server ○ Cloud ○ Sidecar ○ Reverse Proxy ○ Reverse Proxy Sidecar ● Summary Agenda
  11. 11. 1. Embedded
  12. 12. Application Load Balancer Cache Application Cache Request Embedded Cache
  13. 13. private ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>(); private String processRequest(String request) { if (cache.contains(request)) { return cache.get(request); } String response = process(request); cache.put(request, response); return response; } Embedded Cache (Pure Java implementation)
  14. 14. private ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>(); private String processRequest(String request) { if (cache.contains(request)) { return cache.get(request); } String response = process(request); cache.put(request, response); return response; } Embedded Cache (Pure Java implementation)
  15. 15. ● No Eviction Policies ● No Max Size Limit (OutOfMemoryError) ● No Statistics ● No built-in Cache Loaders ● No Expiration Time ● No Notification Mechanism Java Collection is not a Cache!
  16. 16. CacheBuilder.newBuilder() .initialCapacity(300) .expireAfterAccess(Duration.ofMinutes(10)) .maximumSize(1000) .build(); Embedded Cache (Java libraries)
  17. 17. Embedded Cache (Java libraries) CacheBuilder.newBuilder() .initialCapacity(300) .expireAfterAccess(Duration.ofMinutes(10)) .maximumSize(1000) .build();
  18. 18. Caching Application Layer @Service public class BookService { @Cacheable("books") public String getBookNameByIsbn(String isbn) { return findBookInSlowSource(isbn); } }
  19. 19. Caching Application Layer @Service public class BookService { @Cacheable("books") public String getBookNameByIsbn(String isbn) { return findBookInSlowSource(isbn); } } Be Careful, Spring uses ConcurrentHashMap by default!
  20. 20. Application Load Balancer Cache Application Cache Request Embedded Cache
  21. 21. 1*. Embedded Distributed
  22. 22. Application Application Load Balancer Cache Cache Request Hazelcast Cluster Embedded Distributed Cache
  23. 23. @Configuration public class HazelcastConfiguration { @Bean CacheManager cacheManager() { return new HazelcastCacheManager( Hazelcast.newHazelcastInstance()); } } Embedded Distributed Cache (Spring with Hazelcast)
  24. 24. DEMO
  25. 25. Hazelcast Discovery Plugins
  26. 26. Hazelcast Discovery Plugins
  27. 27. Hazelcast Discovery Plugins
  28. 28. Application Application Load Balancer Cache Cache Request Hazelcast Cluster Embedded Distributed Cache
  29. 29. Embedded Cache Pros Cons ● Simple configuration / deployment ● Low-latency data access ● No separate Ops Team needed ● Not flexible management (scaling, backup) ● Limited to JVM-based applications ● Data collocated with applications
  30. 30. ● Introduction ✔ ● Caching Architectural Patterns ○ Embedded ✔ ○ Embedded Distributed ✔ ○ Client-Server ○ Cloud ○ Sidecar ○ Reverse Proxy ○ Reverse Proxy Sidecar ● Summary Agenda
  31. 31. 2. Client-Server
  32. 32. Application Load Balancer Application Request Cache Server Client-Server Cache
  33. 33. Application Load Balancer Application Request Cache Server Client-Server Cache
  34. 34. Application Load Balancer Application Request Cache Server Client-Server Cache Separate Management: ● backups ● (auto) scaling ● security Ops Team
  35. 35. Application Load Balancer Application Request Cache Server Client-Server Cache
  36. 36. Application Load Balancer Application Request Cache Server Client-Server Cache
  37. 37. Client-Server Cache
  38. 38. Client-Server Cache
  39. 39. Client-Server Cache $ hz start Starting Hazelcast Cache Server (standalone)
  40. 40. Client-Server Cache Starting Hazelcast Cache Server (Kubernetes) $ helm install hazelcast/hazelcast
  41. 41. Client-Server Cache Hazelcast Client (Kubernetes): @Configuration public class HazelcastClientConfiguration { @Bean CacheManager cacheManager() { return new HazelcastCacheManager(); } } Starting Hazelcast Cache Server (Kubernetes) $ helm install hazelcast/hazelcast
  42. 42. Application Load Balancer Application Request Cache Server Client-Server Cache Ops Team Separate Management: ● backups ● (auto) scaling ● security
  43. 43. 2*. Cloud
  44. 44. Application Load Balancer Application Request Cloud (Cache as a Service)
  45. 45. Application Load Balancer Application Request Cloud (Cache as a Service) Management: ● backups ● (auto) scaling ● security Ops Team
  46. 46. Application Load Balancer Application Request Cloud (Cache as a Service) Management: ● backups ● (auto) scaling ● security Ops Team
  47. 47. Application Load Balancer Application Request Cloud (Cache as a Service)
  48. 48. @Configuration public class HazelcastCloudConfiguration { @Bean CacheManager cacheManager() { ClientConfig clientConfig = new ClientConfig(); clientConfig.getNetworkConfig().getCloudConfig() .setEnabled(true) .setDiscoveryToken("KSXFDTi5HXPJGR0wRAjLgKe45tvEEhd"); clientConfig.setClusterName("test-cluster"); return new HazelcastCacheManager( HazelcastClient.newHazelcastClient(clientConfig)); } } Cloud (Cache as a Service)
  49. 49. DEMO cloud.hazelcast.com
  50. 50. Client-Server (Cloud) Cache Pros ● Data separate from applications ● Separate management (scaling, backup) ● Programming-language agnostic Cons ● Separate Ops effort ● Higher latency ● Server network requires adjustment (same region, same VPC)
  51. 51. ● Introduction ✔ ● Caching Architectural Patterns ○ Embedded ✔ ○ Embedded Distributed ✔ ○ Client-Server ✔ ○ Cloud ✔ ○ Sidecar ○ Reverse Proxy ○ Reverse Proxy Sidecar ● Summary Agenda
  52. 52. 3. Sidecar
  53. 53. Kubernetes Service (Load Balancer) Request Hazelcast Cluster Kubernetes POD Application Container Cache Container Application Container Cache Container Kubernetes POD Sidecar Cache
  54. 54. Sidecar Cache Similar to Embedded: ● the same physical machine ● the same resource pool ● scales up and down together ● no discovery needed (always localhost) Similar to Client-Server: ● different programming language ● uses cache client to connect ● clear isolation between app and cache
  55. 55. @Configuration public class HazelcastSidecarConfiguration { @Bean CacheManager cacheManager() { ClientConfig clientConfig = new ClientConfig(); clientConfig.getNetworkConfig() .addAddress("localhost:5701"); return new HazelcastCacheManager(HazelcastClient .newHazelcastClient(clientConfig)); } } Sidecar Cache
  56. 56. Sidecar Cache apiVersion: apps/v1 kind: Deployment ... spec: template: spec: containers: - name: application image: leszko/application - name: hazelcast image: hazelcast/hazelcast
  57. 57. Sidecar Cache Pros Cons ● Simple configuration ● Programming-language agnostic ● Low latency ● Some isolation of data and applications ● Limited to container-based environments ● Not flexible management (scaling, backup) ● Data collocated with application PODs
  58. 58. ● Introduction ✔ ● Caching Architectural Patterns ○ Embedded ✔ ○ Embedded Distributed ✔ ○ Client-Server ✔ ○ Cloud ✔ ○ Sidecar ✔ ○ Reverse Proxy ○ Reverse Proxy Sidecar ● Summary Agenda
  59. 59. 4. Reverse Proxy
  60. 60. Application Load Balancer Cache Application Request Reverse Proxy Cache
  61. 61. Reverse Proxy Cache http { ... proxy_cache_path /data/nginx/cache keys_zone=one:10m; ... }
  62. 62. ● Only for HTTP ● Not distributed ● No High Availability ● Data stored on the disk NGINX Reverse Proxy Cache Issues
  63. 63. NGINX Reverse Proxy Cache Issues ● Only for HTTP ● Not distributed ● No High Availability ● Data stored on the disk
  64. 64. 4*. Reverse Proxy Sidecar
  65. 65. Kubernetes Service (Load Balancer) Request Hazelcast Cluster Kubernetes POD Application Container Reverse Proxy Cache Container Application Container Reverse Proxy Cache Container Kubernetes POD Reverse Proxy Sidecar Cache
  66. 66. Good
  67. 67. Reverse Proxy Sidecar Cache Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
  68. 68. Reverse Proxy Sidecar Cache Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
  69. 69. Reverse Proxy Sidecar Cache apiVersion: apps/v1 kind: Deployment ... spec: template: spec: initContainers: - name: init-networking image: leszko/init-networking containers: - name: caching-proxy image: leszko/caching-proxy - name: application image: leszko/application
  70. 70. Reverse Proxy Cache Container Application Container eth0 :80 Reverse Proxy Sidecar Cache :80 Pod :8000 lo :80
  71. 71. Reverse Proxy Sidecar Cache #!/bin/bash # Forward TCP traffic on port 80 to port 8000 on the eth0 interface. iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 80 -j REDIRECT --to-port 8000
  72. 72. Reverse Proxy Sidecar Cache Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
  73. 73. Reverse Proxy Sidecar Cache (Istio)
  74. 74. Reverse Proxy Sidecar Cache (Istio)
  75. 75. Bad
  76. 76. Reverse Proxy Cache @CacheEvict(value = "someValue", allEntries = true) public void evictAllCacheValues() {} Application Cache:
  77. 77. Reverse Proxy Cache @CacheEvict(value = "someValue", allEntries = true) public void evictAllCacheValues() {} Application Cache: Proxy Cache: http { ... location / { add_header Cache-Control public; expires 86400; etag on; } }
  78. 78. Reverse Proxy (Sidecar) Cache Pros Cons ● Configuration-based (no need to change applications) ● Programming-language agnostic ● Consistent with containers and microservice world ● Difficult cache invalidation ● No mature solutions yet ● Protocol-based (e.g. works only with HTTP)
  79. 79. ● Introduction ✔ ● Caching Architectural Patterns ○ Embedded ✔ ○ Embedded Distributed ✔ ○ Client-Server ✔ ○ Cloud ✔ ○ Sidecar ✔ ○ Reverse Proxy ✔ ○ Reverse Proxy Sidecar ✔ ● Summary Agenda
  80. 80. Summary
  81. 81. application-aware?
  82. 82. application-aware? containers? no
  83. 83. application-aware? containers? Reverse Proxy no no
  84. 84. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar no yes no
  85. 85. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? yes no yes no
  86. 86. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? language-agnostic? containers? yes no yes nono
  87. 87. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? language-agnostic? containers? Embedded (Distributed) yes no yes no no no
  88. 88. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? language-agnostic? containers? Embedded (Distributed) Sidecar yes no yes yes no no no
  89. 89. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? language-agnostic? containers? Embedded (Distributed) Sidecar cloud? yes no yes yes yes no no no
  90. 90. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? language-agnostic? containers? Embedded (Distributed) Sidecar cloud? Client-Server yes no yes yes yes no nono no
  91. 91. application-aware? containers? Reverse Proxy Reverse Proxy Sidecar lot of data? security restrictions? language-agnostic? containers? Embedded (Distributed) Sidecar cloud? Client-ServerCloud yes no yes yes yes yes no nono no
  92. 92. Resources ● Code for this talk: https://github.com/leszko/caching-patterns ● Hazelcast Sidecar Container Pattern: https://hazelcast.com/blog/hazelcast-sidecar-container-pattern/ ● Hazelcast Reverse Proxy Sidecar Caching Prototype: https://github.com/leszko/caching-injector ● NGINX HTTP Reverse Proxy Caching: https://www.nginx.com/resources/videos/best-practices-for-caching/
  93. 93. Thank You! Rafał Leszko @RafalLeszko rafalleszko.com

Where is my cache architectural patterns for caching microservices by example

Views

Total views

258

On Slideshare

0

From embeds

0

Number of embeds

3

Actions

Downloads

4

Shares

0

Comments

0

Likes

0

×