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
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 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
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 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)
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 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
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 patterns for high performance microservices in kubernetes
PDF
[jLove 2020] Where is my cache architectural patterns for caching microservi...
PDF
5 levels of high availability from multi instance to hybrid cloud
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 patterns for high performance microservices in kubernetes
[jLove 2020] Where is my cache architectural patterns for caching microservi...
5 levels of high availability from multi instance to hybrid cloud

What's hot (20)

PDF
MySQL Cluster (NDB) - Best Practices Percona Live 2017
PDF
Distributed applications using Hazelcast
PDF
Clusternaut: Orchestrating  Percona XtraDB Cluster with Kubernetes
PDF
Clusternaut: Orchestrating Percona XtraDB Cluster with Kubernetes.
PPTX
Scylla on Kubernetes: Introducing the Scylla Operator
PDF
In-memory No SQL- GIDS2014
PDF
Introducing the R2DBC async Java connector
PPTX
How we switched to columnar at SpendHQ
PPTX
Oem12c db12c and You
PDF
Faster, better, stronger: The new InnoDB
PPTX
MariaDB und mehr - MariaDB Roadshow Summer 2014 Hamburg Berlin Frankfurt
PDF
Introducing the ultimate MariaDB cloud, SkySQL
PPTX
From cache to in-memory data grid. Introduction to Hazelcast.
PPTX
How Pixid dropped Oracle and went hybrid with MariaDB
PDF
Couchbase Singapore Meetup #2: Why Developing with Couchbase is easy !!
PDF
MongoDB .local Bengaluru 2019: New Encryption Capabilities in MongoDB 4.2: A ...
PDF
What to expect from MariaDB Platform X5, part 1
PPTX
The New MariaDB Offering: MariaDB 10, MaxScale and More
PPTX
Redis Labs and SQL Server
PDF
Welcome to icehouse
MySQL Cluster (NDB) - Best Practices Percona Live 2017
Distributed applications using Hazelcast
Clusternaut: Orchestrating  Percona XtraDB Cluster with Kubernetes
Clusternaut: Orchestrating Percona XtraDB Cluster with Kubernetes.
Scylla on Kubernetes: Introducing the Scylla Operator
In-memory No SQL- GIDS2014
Introducing the R2DBC async Java connector
How we switched to columnar at SpendHQ
Oem12c db12c and You
Faster, better, stronger: The new InnoDB
MariaDB und mehr - MariaDB Roadshow Summer 2014 Hamburg Berlin Frankfurt
Introducing the ultimate MariaDB cloud, SkySQL
From cache to in-memory data grid. Introduction to Hazelcast.
How Pixid dropped Oracle and went hybrid with MariaDB
Couchbase Singapore Meetup #2: Why Developing with Couchbase is easy !!
MongoDB .local Bengaluru 2019: New Encryption Capabilities in MongoDB 4.2: A ...
What to expect from MariaDB Platform X5, part 1
The New MariaDB Offering: MariaDB 10, MaxScale and More
Redis Labs and SQL Server
Welcome to icehouse
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
Architectural caching patterns for kubernetes
PDF
Architectural caching patterns for kubernetes
PDF
Where is my cache architectural patterns for caching microservices by example
PPTX
High Performance Cloud-Native Microservices With Distributed Caching
PPTX
Cache first cloud native microservices
PPTX
Distributed Caching in Kubernetes with Hazelcast
PPTX
High Performance Cloud-Native Microservices IndyCloudConf 2020
PDF
Hazelcast 101
PDF
Caching and JCache with Greg Luck 18.02.16
PPTX
Hazelcast and MongoDB at Cloud CMS
PDF
Hazelcast for Terracotta Users
PDF
Building scalable applications with hazelcast
PDF
Building scalable applications with hazelcast
PPTX
Geek Nights Hong Kong
PDF
Hazelcast 3.6 Roadmap Preview
PPTX
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
PPTX
Hazelcast Essentials
PDF
Web session replication with Hazelcast
PPTX
ConFoo - 3 performance improvements
[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
High Performance Cloud-Native Microservices With Distributed Caching
Cache first cloud native microservices
Distributed Caching in Kubernetes with Hazelcast
High Performance Cloud-Native Microservices IndyCloudConf 2020
Hazelcast 101
Caching and JCache with Greg Luck 18.02.16
Hazelcast and MongoDB at Cloud CMS
Hazelcast for Terracotta Users
Building scalable applications with hazelcast
Building scalable applications with hazelcast
Geek Nights Hong Kong
Hazelcast 3.6 Roadmap Preview
IMC Summit 2016 Breakout - Greg Luck - How to Speed Up Your Application Using...
Hazelcast Essentials
Web session replication with Hazelcast
ConFoo - 3 performance improvements
Ad

More from Rafał Leszko (14)

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
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
Distributed Locking in Kubernetes
Mutation testing with PIT
Build your operator with the right tool
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)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
L1 - Introduction to python Backend.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
ai tools demonstartion for schools and inter college
PDF
Digital Strategies for Manufacturing Companies
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
medical staffing services at VALiNTRY
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Introduction to Artificial Intelligence
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Which alternative to Crystal Reports is best for small or large businesses.pdf
L1 - Introduction to python Backend.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
How Creative Agencies Leverage Project Management Software.pdf
Operating system designcfffgfgggggggvggggggggg
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Migrate SBCGlobal Email to Yahoo Easily
ai tools demonstartion for schools and inter college
Digital Strategies for Manufacturing Companies
Adobe Illustrator 28.6 Crack My Vision of Vector Design
medical staffing services at VALiNTRY
Upgrade and Innovation Strategies for SAP ERP Customers
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Introduction to Artificial Intelligence
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Online Work Permit System for Fast Permit Processing
CHAPTER 2 - PM Management and IT Context
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx

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