SlideShare a Scribd company logo
Where is my cache?
Architectural patterns for caching
microservices by example
Rafał Leszko
@RafalLeszko
rafalleszko.com
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
● 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
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() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().getKubernetesConfig()
.setEnabled(true);
return new HazelcastCacheManager(HazelcastClient
.newHazelcastClient(clientConfig));
}
}
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.setGroupConfig(
new GroupConfig("test-cluster", "b2f984b5dd3314"));
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
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
Container
Application Container
Reverse Proxy Cache
Container
Kubernetes POD
Reverse Proxy Sidecar Cache
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
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 nat -A PREROUTING -p tcp -i eth0 --dport 80 -j REDIRECT
--to-port 8000
Reverse Proxy Sidecar Cache
Service 1
Service 2
v1
Service 2
v2
Service 1
Service 4
v1
Service 4
v2
Service 4
v3
Ruby
Where is my cache? Architectural patterns for caching microservices by example
Reverse Proxy Sidecar Cache (Istio)
Reverse Proxy Sidecar Cache (Istio)
Bad
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 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 nono
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
nono
no
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
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
Architectural patterns for caching microservices
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 caching patterns for kubernetes
PDF
Distributed Locking in Kubernetes
PDF
Architectural patterns for high performance microservices in kubernetes
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
Where is my cache architectural patterns for caching microservices by example
5 Levels of High Availability: From Multi-instance to Hybrid Cloud
Architectural caching patterns for kubernetes
Distributed Locking in Kubernetes
Architectural patterns for high performance microservices in kubernetes

What's hot (20)

PDF
[jLove 2020] Where is my cache architectural patterns for caching microservi...
PDF
5 levels of high availability from multi instance to hybrid cloud
PDF
Build your operator with the right tool
PDF
Architectural caching patterns for kubernetes
PDF
Optimizing {Java} Application Performance on Kubernetes
PDF
MySQL Cluster (NDB) - Best Practices Percona Live 2017
PDF
How to create a useful my sql bug report fosdem 2019
PDF
Distributed applications using Hazelcast
PDF
Clusternaut: Orchestrating  Percona XtraDB Cluster with Kubernetes
PPTX
MariaDB und mehr - MariaDB Roadshow Summer 2014 Hamburg Berlin Frankfurt
PDF
Clusternaut: Orchestrating Percona XtraDB Cluster with Kubernetes.
PDF
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
PDF
MySQL Aquarium Paris
PDF
Faster, better, stronger: The new InnoDB
PPTX
The New MariaDB Offering: MariaDB 10, MaxScale and More
PDF
MongoDB .local Bengaluru 2019: New Encryption Capabilities in MongoDB 4.2: A ...
PDF
Manuel Hurtado. Couchbase paradigma4oct
PDF
Oracle SOA suite and Coherence dehydration
PDF
AWS Lambda and serverless Java | DevNation Live
PPTX
From cache to in-memory data grid. Introduction to Hazelcast.
[jLove 2020] Where is my cache architectural patterns for caching microservi...
5 levels of high availability from multi instance to hybrid cloud
Build your operator with the right tool
Architectural caching patterns for kubernetes
Optimizing {Java} Application Performance on Kubernetes
MySQL Cluster (NDB) - Best Practices Percona Live 2017
How to create a useful my sql bug report fosdem 2019
Distributed applications using Hazelcast
Clusternaut: Orchestrating  Percona XtraDB Cluster with Kubernetes
MariaDB und mehr - MariaDB Roadshow Summer 2014 Hamburg Berlin Frankfurt
Clusternaut: Orchestrating Percona XtraDB Cluster with Kubernetes.
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
MySQL Aquarium Paris
Faster, better, stronger: The new InnoDB
The New MariaDB Offering: MariaDB 10, MaxScale and More
MongoDB .local Bengaluru 2019: New Encryption Capabilities in MongoDB 4.2: A ...
Manuel Hurtado. Couchbase paradigma4oct
Oracle SOA suite and Coherence dehydration
AWS Lambda and serverless Java | DevNation Live
From cache to in-memory data grid. Introduction to Hazelcast.
Ad

Similar to Where is my cache? Architectural patterns for caching microservices by example (20)

PDF
[DevopsDays India 2019] Where is my cache? Architectural patterns for caching...
PDF
Where is my cache architectural patterns for caching microservices by example
PDF
Second Skin: Real-Time Retheming a Legacy Web Application with Diazo in the C...
PDF
Making Service Deployments to AWS a breeze with Nova
PDF
CON6423: Scalable JavaScript applications with Project Nashorn
PDF
Como creamos QuestDB Cloud, un SaaS basado en Kubernetes alrededor de QuestDB...
PDF
What’s new in cas 4.2
PDF
Spring Native and Spring AOT
PDF
Deep dive into OpenStack storage, Sean Cohen, Red Hat
PDF
Deep Dive into Openstack Storage, Sean Cohen, Red Hat
PPTX
Coherence RoadMap 2018
PDF
Choose the Right Container Storage for Kubernetes
PPTX
High Performance Cloud-Native Microservices With Distributed Caching
PDF
Red Hat Container Development Kit
PPT
Four Ways to Improve ASP .NET Performance and Scalability
PDF
Scaling PHP apps
PPTX
Automating using Ansible
PDF
Docker vs. Kubernetes vs. Serverless
PPTX
Pivotal cloud cache for .net microservices
PDF
6 Months Sailing with Docker in Production
[DevopsDays India 2019] Where is my cache? Architectural patterns for caching...
Where is my cache architectural patterns for caching microservices by example
Second Skin: Real-Time Retheming a Legacy Web Application with Diazo in the C...
Making Service Deployments to AWS a breeze with Nova
CON6423: Scalable JavaScript applications with Project Nashorn
Como creamos QuestDB Cloud, un SaaS basado en Kubernetes alrededor de QuestDB...
What’s new in cas 4.2
Spring Native and Spring AOT
Deep dive into OpenStack storage, Sean Cohen, Red Hat
Deep Dive into Openstack Storage, Sean Cohen, Red Hat
Coherence RoadMap 2018
Choose the Right Container Storage for Kubernetes
High Performance Cloud-Native Microservices With Distributed Caching
Red Hat Container Development Kit
Four Ways to Improve ASP .NET Performance and Scalability
Scaling PHP apps
Automating using Ansible
Docker vs. Kubernetes vs. Serverless
Pivotal cloud cache for .net microservices
6 Months Sailing with Docker in Production
Ad

More from Rafał Leszko (12)

PDF
Build Your Kubernetes Operator with the Right Tool!
PDF
Mutation Testing with PIT
PDF
Mutation testing with PIT
PDF
Where is my cache architectural patterns for caching microservices by example
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
Mutation testing with PIT
Where is my cache architectural patterns for caching microservices by example
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)

PPTX
Online Work Permit System for Fast Permit Processing
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
ai tools demonstartion for schools and inter college
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Nekopoi APK 2025 free lastest update
PDF
AI in Product Development-omnex systems
PPTX
Transform Your Business with a Software ERP System
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
top salesforce developer skills in 2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPT
Introduction Database Management System for Course Database
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Operating system designcfffgfgggggggvggggggggg
Online Work Permit System for Fast Permit Processing
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
ai tools demonstartion for schools and inter college
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Nekopoi APK 2025 free lastest update
AI in Product Development-omnex systems
Transform Your Business with a Software ERP System
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Design an Analysis of Algorithms I-SECS-1021-03
2025 Textile ERP Trends: SAP, Odoo & Oracle
top salesforce developer skills in 2025.pdf
CHAPTER 2 - PM Management and IT Context
Introduction Database Management System for Course Database
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
Operating system designcfffgfgggggggvggggggggg

Where is my cache? Architectural patterns for caching microservices by example