SlideShare a Scribd company logo
Where is my cache?
Architectural patterns for caching
microservices by example
Rafał Leszko
@RafalLeszko
Hazelcast
About me
● Cloud Software Engineer 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
About Hazelcast
● Distributed Company
● Open Source Software
● 140+ Employees
● Hiring (Remote)!
● Recently Raised $21M
● Products:
○ Hazelcast IMDG
○ Hazelcast Jet
○ Hazelcast Cloud
@Hazelcast
www.hazelcast.com
● Introduction
● Caching Architectural Patterns
○ Embedded
○ Embedded Distributed
○ Client-Server
○ Cloud
○ Sidecar
○ Reverse Proxy
○ Reverse Proxy Sidecar
● Summary
Agenda
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
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Microservice World
cache
cache
cache
cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Microservice World cache
cache
cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Microservice World
cache
cache
cache
● Introduction ✔
● Caching Architectural Patterns
○ Embedded
○ Embedded Distributed
○ Client-Server
○ Cloud
○ Sidecar
○ Reverse Proxy
○ Reverse Proxy Sidecar
● Summary
Agenda
1. Embedded
Application
Load Balancer
Cache
Application
Cache
Request
Embedded Cache
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)
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)
● 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!
CacheBuilder.newBuilder()
.initialCapacity(300)
.expireAfterAccess(Duration.ofMinutes(10))
.maximumSize(1000)
.build();
Embedded Cache (Java libraries)
Embedded Cache (Java libraries)
CacheBuilder.newBuilder()
.initialCapacity(300)
.expireAfterAccess(Duration.ofMinutes(10))
.maximumSize(1000)
.build();
Caching Application Layer
@Service
public class BookService {
@Cacheable("books")
public String getBookNameByIsbn(String isbn) {
return findBookInSlowSource(isbn);
}
}
Caching Application Layer
@Service
public class BookService {
@Cacheable("books")
public String getBookNameByIsbn(String isbn) {
return findBookInSlowSource(isbn);
}
}
Be Careful, Spring uses ConcurrentHashMap by default!
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(
Hazelcast.newHazelcastInstance());
}
}
Embedded Distributed Cache (Spring with Hazelcast)
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 flexible management
(scaling, backup)
● Limited to JVM-based
applications
● Data collocated with
applications
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server
○ Cloud
○ Sidecar
○ Reverse Proxy
○ Reverse Proxy Sidecar
● Summary
Agenda
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) scaling
● security
Ops Team
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
Application
Load Balancer
Application
Request
Cache Server
Client-Server Cache
Ops Team
Separate Management:
● backups
● (auto) scaling
● security
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
Ops Team
Application
Load Balancer
Application
Request
Cloud (Cache as a Service)
Management:
● backups
● (auto) scaling
● security
Ops Team
Application
Load Balancer
Application
Request
Cloud (Cache as a Service)
@Configuration
public class HazelcastCloudConfiguration {
@Bean
CacheManager cacheManager() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().getCloudConfig()
.setEnabled(true)
.setDiscoveryToken("KSXFDTi5HXPJGR0wRAjLgKe45tvEEhd");
clientConfig.setGroupConfig(
new GroupConfig("test-cluster", "b2f984b5dd3314"));
return new HazelcastCacheManager(
HazelcastClient.newHazelcastClient(clientConfig));
}
}
Cloud (Cache as a Service)
Client-Server (Cloud) Cache
Pros Cons
● Data separate from
applications
● Separate management
(scaling, backup)
● Programming-language
agnostic
● Separate Ops effort
● Higher latency
● Server network requires
adjustment (same region,
same VPC)
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server ✔
○ Cloud ✔
○ Sidecar
○ Reverse Proxy
○ Reverse Proxy Sidecar
● Summary
Agenda
3. Sidecar
Kubernetes Service
(Load Balancer)
Request
Hazelcast
Cluster
Kubernetes POD
Application Container
Cache Container
Application Container
Cache Container
Kubernetes POD
Sidecar Cache
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
@Configuration
public class HazelcastSidecarConfiguration {
@Bean
CacheManager cacheManager() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig()
.addAddress("localhost:5701");
return new HazelcastCacheManager(HazelcastClient
.newHazelcastClient(clientConfig));
}
}
Sidecar Cache
Sidecar Cache
apiVersion: apps/v1
kind: Deployment
...
spec:
template:
spec:
containers:
- name: application
image: leszko/application
- name: hazelcast
image: hazelcast/hazelcast
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
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server ✔
○ Cloud ✔
○ Sidecar ✔
○ Reverse Proxy
○ Reverse Proxy Sidecar
● Summary
Agenda
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
4*. Reverse Proxy Sidecar
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
[DevopsDays India 2019] Where is my cache? Architectural patterns for caching microservices by example
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-networking
image: leszko/init-networking
containers:
- name: caching-proxy
image: leszko/caching-proxy
- name: application
image: leszko/application
Reverse Proxy Sidecar Cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Bad
[DevopsDays India 2019] Where is my cache? Architectural patterns for caching microservices by example
Reverse Proxy Cache
@CacheEvict(value = "someValue", allEntries = true)
public void evictAllCacheValues() {}
Application Cache:
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;
}
}
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)
● Introduction ✔
● Caching Architectural Patterns
○ Embedded ✔
○ Embedded Distributed ✔
○ Client-Server ✔
○ Cloud ✔
○ Sidecar ✔
○ Reverse Proxy ✔
○ Reverse Proxy Sidecar ✔
● Summary
Agenda
Summary
application-aware?
application-aware?
containers?
no
application-aware?
containers?
Reverse Proxy
no
no
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
no
yes no
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
lot of data?
security restrictions?
yes no
yes no
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
yes no
yes nono
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
Embedded
(Distributed)
yes no
yes no
no
no
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
Embedded
(Distributed)
Sidecar
yes no
yes
yes no
no
no
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
Embedded
(Distributed)
Sidecar
cloud?
yes no
yes
yes
yes no
no
no
application-aware?
containers?
Reverse ProxyReverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
Embedded
(Distributed)
Sidecar
cloud?
Client-Server
yes no
yes
yes
yes no
nono
no
application-aware?
containers?
Reverse ProxyReverse 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
Resources
● Hazelcast Sidecar Container Pattern:
https://hazelcast.com/blog/hazelcast-sidecar-container-pattern/
● Hazelcast Reverse Proxy Sidecar Caching Prototype:
https://github.com/leszko/caching-injector
● Caching Best Practices:
https://vladmihalcea.com/caching-best-practices/
● NGINX HTTP Reverse Proxy Caching:
https://www.nginx.com/resources/videos/best-practices-for-caching
/
Thank You!
Rafał Leszko
@RafalLeszko

More Related Content

PDF
Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-...
PPTX
Linkerd – Service mesh with service Discovery backend
PDF
API Gateway study
PDF
Introduction to Kong API Gateway
PDF
Mastering Microservices with Kong (CodeMotion 2019)
PPTX
Gatekeeper: API gateway
PDF
Kong API
Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-...
Linkerd – Service mesh with service Discovery backend
API Gateway study
Introduction to Kong API Gateway
Mastering Microservices with Kong (CodeMotion 2019)
Gatekeeper: API gateway
Kong API

What's hot (20)

PDF
Manage your APIs and Microservices with an API Gateway
PDF
Mastering Microservices with Kong (DevoxxUK 2019)
PDF
Flexible, hybrid API-led software architectures with Kong
PPTX
API Gateway: Nginx way
PDF
A sail in the cloud
PDF
WTF Do We Need a Service Mesh?
PPTX
Using an API Gateway for Microservices
PDF
Using NGINX and NGINX Plus as a Kubernetes Ingress
PPTX
Cloud Native Spring - The role of Spring Cloud after Kubernetes became a main...
PDF
Developing Serverless Applications on Kubernetes with Knative
PPTX
Introducing envoy-based service mesh at Booking.com
PDF
The Good, the Bad and the Ugly of Migrating Hundreds of Legacy Applications ...
PDF
Ambassador Kubernetes-Native API Gateway
PPTX
An Open-Source Platform to Connect, Manage, and Secure Microservices
PDF
Load Balancing for Containers and Cloud Native Architecture
PPT
Docker pipelines
ODP
Kong API Gateway
PDF
A microservice architecture based on golang
PDF
Relevez les défis Kubernetes avec NGINX
PPTX
The Challenges of Becoming Cloud Native
Manage your APIs and Microservices with an API Gateway
Mastering Microservices with Kong (DevoxxUK 2019)
Flexible, hybrid API-led software architectures with Kong
API Gateway: Nginx way
A sail in the cloud
WTF Do We Need a Service Mesh?
Using an API Gateway for Microservices
Using NGINX and NGINX Plus as a Kubernetes Ingress
Cloud Native Spring - The role of Spring Cloud after Kubernetes became a main...
Developing Serverless Applications on Kubernetes with Knative
Introducing envoy-based service mesh at Booking.com
The Good, the Bad and the Ugly of Migrating Hundreds of Legacy Applications ...
Ambassador Kubernetes-Native API Gateway
An Open-Source Platform to Connect, Manage, and Secure Microservices
Load Balancing for Containers and Cloud Native Architecture
Docker pipelines
Kong API Gateway
A microservice architecture based on golang
Relevez les défis Kubernetes avec NGINX
The Challenges of Becoming Cloud Native
Ad

Similar to [DevopsDays India 2019] Where is my cache? Architectural patterns for caching microservices by example (20)

PDF
Where is my cache architectural patterns for caching microservices by example
PDF
Where is my cache? Architectural patterns for caching microservices by example
PDF
Where is my cache? Architectural patterns for caching microservices by example
PDF
Where is my cache? Architectural patterns for caching microservices by example
PDF
Where is my cache architectural patterns for caching microservices by example
PDF
Architectural patterns for caching microservices
PDF
[jLove 2020] Where is my cache architectural patterns for caching microservi...
PDF
Architectural caching patterns for kubernetes
PDF
Architectural caching patterns for kubernetes
PDF
Where is my cache architectural patterns for caching microservices by example
PDF
Architectural patterns for high performance microservices in kubernetes
PDF
Second Skin: Real-Time Retheming a Legacy Web Application with Diazo in the C...
PDF
Choose the Right Container Storage for Kubernetes
PPTX
Coherence RoadMap 2018
PDF
WordCamp RVA
PDF
WordCamp RVA
PDF
WordCamp RVA 2011 - Performance & Tuning.pdf
PDF
WordCamp RVA 2011 - Performance & Tuning.pdf
PDF
Como creamos QuestDB Cloud, un SaaS basado en Kubernetes alrededor de QuestDB...
PPTX
Veeam Webinar - Case study: building bi-directional DR
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
Where is my cache? Architectural patterns for caching microservices by example
Where is my cache architectural patterns for caching microservices by example
Architectural patterns for caching microservices
[jLove 2020] Where is my cache architectural patterns for caching microservi...
Architectural caching patterns for kubernetes
Architectural caching patterns for kubernetes
Where is my cache architectural patterns for caching microservices by example
Architectural patterns for high performance microservices in kubernetes
Second Skin: Real-Time Retheming a Legacy Web Application with Diazo in the C...
Choose the Right Container Storage for Kubernetes
Coherence RoadMap 2018
WordCamp RVA
WordCamp RVA
WordCamp RVA 2011 - Performance & Tuning.pdf
WordCamp RVA 2011 - Performance & Tuning.pdf
Como creamos QuestDB Cloud, un SaaS basado en Kubernetes alrededor de QuestDB...
Veeam Webinar - Case study: building bi-directional DR
Ad

More from Rafał Leszko (15)

PDF
Build Your Kubernetes Operator with the Right Tool!
PDF
Mutation Testing with PIT
PDF
Distributed Locking in Kubernetes
PDF
Mutation testing with PIT
PDF
Build your operator with the right tool
PDF
5 levels of high availability from multi instance to hybrid cloud
PDF
5 Levels of High Availability: From Multi-instance to Hybrid Cloud
PDF
Stream Processing in the Cloud - Athens Kubernetes Meetup 16.07.2019
PDF
Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
PDF
Mutation Testing - Voxxed Days Cluj-Napoca 2017
PDF
Continuous Delivery - Voxxed Days Cluj-Napoca 2017
PDF
Continuous Delivery - Voxxed Days Bucharest 2017
PDF
Mutation Testing - Voxxed Days Bucharest 10.03.2017
PDF
Continuous Delivery - Devoxx Morocco 2016
PDF
Continuous Delivery - Voxxed Days Thessaloniki 21.10.2016
Build Your Kubernetes Operator with the Right Tool!
Mutation Testing with PIT
Distributed Locking in Kubernetes
Mutation testing with PIT
Build your operator with the right tool
5 levels of high availability from multi instance to hybrid cloud
5 Levels of High Availability: From Multi-instance to Hybrid Cloud
Stream Processing in the Cloud - Athens Kubernetes Meetup 16.07.2019
Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
Mutation Testing - Voxxed Days Cluj-Napoca 2017
Continuous Delivery - Voxxed Days Cluj-Napoca 2017
Continuous Delivery - Voxxed Days Bucharest 2017
Mutation Testing - Voxxed Days Bucharest 10.03.2017
Continuous Delivery - Devoxx Morocco 2016
Continuous Delivery - Voxxed Days Thessaloniki 21.10.2016

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPT
Introduction Database Management System for Course Database
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
AI in Product Development-omnex systems
PPTX
Introduction to Artificial Intelligence
PDF
Nekopoi APK 2025 free lastest update
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
Introduction Database Management System for Course Database
Operating system designcfffgfgggggggvggggggggg
Upgrade and Innovation Strategies for SAP ERP Customers
ManageIQ - Sprint 268 Review - Slide Deck
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
AI in Product Development-omnex systems
Introduction to Artificial Intelligence
Nekopoi APK 2025 free lastest update
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Understanding Forklifts - TECH EHS Solution
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
How to Migrate SBCGlobal Email to Yahoo Easily
Design an Analysis of Algorithms I-SECS-1021-03
Adobe Illustrator 28.6 Crack My Vision of Vector Design
VVF-Customer-Presentation2025-Ver1.9.pptx

[DevopsDays India 2019] Where is my cache? Architectural patterns for caching microservices by example