SlideShare a Scribd company logo
Architectural patterns for caching
microservices
Rafał Leszko
@RafalLeszko
rafalleszko.com
Hazelcast
About me
● Cloud-Native 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
About Hazelcast
● Distributed Company
● Open Source Software
● 140+ Employees
● 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
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
cache
cache
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
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
● 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)
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 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
Client-Server Cache
$ ./start.sh
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
CacheManager cacheManager() {
return new HazelcastCacheManager(HazelcastClient
.newHazelcastClient());
}
}
Starting Hazelcast Cache Server (Kubernetes)
$ helm install hazelcast/hazelcast
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.setClusterName("test-cluster");
return new HazelcastCacheManager(
HazelcastClient.newHazelcastClient(clientConfig));
}
}
Cloud (Cache as a Service)
DEMO
cloud.hazelcast.com
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)
● 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
Architectural patterns for caching microservices
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
Cache
Container
Application
Container
eth0
:80
Reverse Proxy Sidecar Cache
:80
Pod
:8000
lo
:80
Reverse Proxy Sidecar Cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Architectural patterns for caching microservices
Bad
Architectural patterns for caching microservices
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 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?
containers?
yes no
yes no
no
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
Embedded
(Distributed)
yes no
yes no
no
no
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
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
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
no
no
no
application-aware?
containers?
Reverse Proxy
Reverse Proxy
Sidecar
lot of data?
security restrictions?
language-agnostic?
containers?
Embedded
(Distributed)
Sidecar
cloud?
Client-Server
Cloud
yes no
yes
yes yes
yes no
no
no
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
rafalleszko.com

More Related Content

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
5 Levels of High Availability: From Multi-instance to Hybrid Cloud
PDF
Architectural patterns for high performance microservices in kubernetes
PDF
[jLove 2020] Where is my cache architectural patterns for caching microservi...
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
5 Levels of High Availability: From Multi-instance to Hybrid Cloud
Architectural patterns for high performance microservices in kubernetes
[jLove 2020] Where is my cache architectural patterns for caching microservi...

What's hot (20)

PDF
5 levels of high availability from multi instance to hybrid cloud
PDF
MySQL Cluster (NDB) - Best Practices Percona Live 2017
PDF
Clusternaut: Orchestrating  Percona XtraDB Cluster with Kubernetes
PDF
How to create a useful my sql bug report fosdem 2019
PDF
MySQL Aquarium Paris
PPTX
MariaDB und mehr - MariaDB Roadshow Summer 2014 Hamburg Berlin Frankfurt
PDF
Clusternaut: Orchestrating Percona XtraDB Cluster with Kubernetes.
PDF
High Availability Content Caching with NGINX
PDF
MongoDB .local Bengaluru 2019: New Encryption Capabilities in MongoDB 4.2: A ...
PDF
Using NGINX as an Effective and Highly Available Content Cache
PDF
Distributed applications using Hazelcast
PPTX
The New MariaDB Offering: MariaDB 10, MaxScale and More
PDF
ITB2017 - Nginx Effective High Availability Content Caching
PDF
NGINX: The Past, Present and Future of the Modern Web
PDF
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
PDF
Faster, better, stronger: The new InnoDB
PDF
Security practices in OpenShift
PDF
Best practices for MySQL High Availability Tutorial
PDF
Containerizing MongoDB with kubernetes
PPTX
Lamar University CAS HA
5 levels of high availability from multi instance to hybrid cloud
MySQL Cluster (NDB) - Best Practices Percona Live 2017
Clusternaut: Orchestrating  Percona XtraDB Cluster with Kubernetes
How to create a useful my sql bug report fosdem 2019
MySQL Aquarium Paris
MariaDB und mehr - MariaDB Roadshow Summer 2014 Hamburg Berlin Frankfurt
Clusternaut: Orchestrating Percona XtraDB Cluster with Kubernetes.
High Availability Content Caching with NGINX
MongoDB .local Bengaluru 2019: New Encryption Capabilities in MongoDB 4.2: A ...
Using NGINX as an Effective and Highly Available Content Cache
Distributed applications using Hazelcast
The New MariaDB Offering: MariaDB 10, MaxScale and More
ITB2017 - Nginx Effective High Availability Content Caching
NGINX: The Past, Present and Future of the Modern Web
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
Faster, better, stronger: The new InnoDB
Security practices in OpenShift
Best practices for MySQL High Availability Tutorial
Containerizing MongoDB with kubernetes
Lamar University CAS HA
Ad

Similar to Architectural patterns for caching microservices (20)

PDF
[DevopsDays India 2019] Where is my cache? Architectural patterns for caching...
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
Choose the Right Container Storage for Kubernetes
PPTX
Best practices for developing your Magento Commerce on Cloud
PDF
Second Skin: Real-Time Retheming a Legacy Web Application with Diazo in the C...
PPTX
Coherence RoadMap 2018
PDF
Deep dive into OpenStack storage, Sean Cohen, Red Hat
PDF
Deep Dive into Openstack Storage, Sean Cohen, Red Hat
PDF
Making Service Deployments to AWS a breeze with Nova
ODP
Cloud Native Java Development Patterns
PDF
Como creamos QuestDB Cloud, un SaaS basado en Kubernetes alrededor de QuestDB...
PPTX
High Performance Cloud-Native Microservices With Distributed Caching
PDF
What’s new in cas 4.2
PDF
CON6423: Scalable JavaScript applications with Project Nashorn
PPTX
GCP - Continuous Integration and Delivery into Kubernetes with GitHub, Travis...
ODP
The secret life of a dispatcher (Adobe CQ AEM)
PPTX
Veeam Webinar - Case study: building bi-directional DR
PDF
CloudStack and LINBIT SDS Integration
[DevopsDays India 2019] Where is my cache? Architectural patterns for caching...
Architectural caching patterns for kubernetes
Architectural caching patterns for kubernetes
Where is my cache architectural patterns for caching microservices by example
Choose the Right Container Storage for Kubernetes
Best practices for developing your Magento Commerce on Cloud
Second Skin: Real-Time Retheming a Legacy Web Application with Diazo in the C...
Coherence RoadMap 2018
Deep dive into OpenStack storage, Sean Cohen, Red Hat
Deep Dive into Openstack Storage, Sean Cohen, Red Hat
Making Service Deployments to AWS a breeze with Nova
Cloud Native Java Development Patterns
Como creamos QuestDB Cloud, un SaaS basado en Kubernetes alrededor de QuestDB...
High Performance Cloud-Native Microservices With Distributed Caching
What’s new in cas 4.2
CON6423: Scalable JavaScript applications with Project Nashorn
GCP - Continuous Integration and Delivery into Kubernetes with GitHub, Travis...
The secret life of a dispatcher (Adobe CQ AEM)
Veeam Webinar - Case study: building bi-directional DR
CloudStack and LINBIT SDS Integration
Ad

More from Rafał Leszko (13)

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
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
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
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Introduction to Artificial Intelligence
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPT
Introduction Database Management System for Course Database
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Odoo Companies in India – Driving Business Transformation.pdf
top salesforce developer skills in 2025.pdf
System and Network Administration Chapter 2
Softaken Excel to vCard Converter Software.pdf
Introduction to Artificial Intelligence
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Odoo POS Development Services by CandidRoot Solutions
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
ManageIQ - Sprint 268 Review - Slide Deck
Operating system designcfffgfgggggggvggggggggg
How to Migrate SBCGlobal Email to Yahoo Easily
Introduction Database Management System for Course Database
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises

Architectural patterns for caching microservices