Where is my cache? Architectural patterns for caching microservices by example
Rafał Leszko Cloud Software Engineer Hazelcast
Where is my cache? Architectural patterns for caching microservices - - PowerPoint PPT Presentation
Where is my cache? Architectural patterns for caching microservices by example Rafa Leszko Cloud Software Engineer Hazelcast About me Cloud Software Engineer at Hazelcast Worked at Google and CERN Author of the book
Rafał Leszko Cloud Software Engineer Hazelcast
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
cache cache cache cache
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
cache cache cache
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
cache cache cache
Application Load Balancer Cache Application Cache Request
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; }
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; }
CacheBuilder.newBuilder() .initialCapacity(300) .expireAfterAccess(Duration.ofMinutes(10)) .maximumSize(1000) .build();
CacheBuilder.newBuilder() .initialCapacity(300) .expireAfterAccess(Duration.ofMinutes(10)) .maximumSize(1000) .build();
@Service public class BookService { @Cacheable("books") public String getBookNameByIsbn(String isbn) { return findBookInSlowSource(isbn); } }
@Service public class BookService { @Cacheable("books") public String getBookNameByIsbn(String isbn) { return findBookInSlowSource(isbn); } }
Application Load Balancer Cache Application Cache Request
Application Application Load Balancer Cache Cache Request Hazelcast Cluster
@Configuration public class HazelcastConfiguration { @Bean CacheManager cacheManager() { return new HazelcastCacheManager( Hazelcast.newHazelcastInstance()); } }
Application Application Load Balancer Cache Cache Request
Hazelcast Cluster
Application Load Balancer Application Request Cache Server
Application Load Balancer Application Request Cache Server
Application Load Balancer Application Request Cache Server
Separate Management:
Ops Team
Application Load Balancer Application Request Cache Server
Application Load Balancer Application Request Cache Server
$ ./start.sh
Starting Hazelcast Cache Server (standalone)
Starting Hazelcast Cache Server (Kubernetes)
$ helm install hazelcast/hazelcast
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
Ops Team Separate Management:
Application Load Balancer Application Request
Application Load Balancer Application Request
Management:
Ops Team
Application Load Balancer Application Request
Management:
Ops Team
Application Load Balancer Application Request
@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)); } }
Kubernetes Service (Load Balancer) Request Kubernetes POD Application Container Cache Container Application Container Cache Container Kubernetes POD
Hazelcast Cluster
Similar to Embedded:
Similar to Client-Server:
@Configuration public class HazelcastSidecarConfiguration { @Bean CacheManager cacheManager() { ClientConfig clientConfig = new ClientConfig(); clientConfig.getNetworkConfig() .addAddress("localhost:5701"); return new HazelcastCacheManager(HazelcastClient .newHazelcastClient(clientConfig)); } }
apiVersion: apps/v1 kind: Deployment ... spec: template: spec: containers:
image: leszko/application
image: hazelcast/hazelcast
Application Load Balancer Cache Application Request
http { ... proxy_cache_path /data/nginx/cache keys_zone=one:10m; ... }
Kubernetes Service (Load Balancer) Request Kubernetes POD Application Container Reverse Proxy Cache Container Application Container Reverse Proxy Cache Container Kubernetes POD
Hazelcast Cluster
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
apiVersion: apps/v1 kind: Deployment ... spec: template: spec: initContainers:
image: leszko/init-networking containers:
image: leszko/caching-proxy
image: leszko/application
Service 1 Service 2 v1 Service 2 v2 Service 1 Service 4 v1 Service 4 v2 Service 4 v3 Ruby
@CacheEvict(value = "someValue", allEntries = true) public void evictAllCacheValues() {}
Application Cache:
@CacheEvict(value = "someValue", allEntries = true) public void evictAllCacheValues() {}
Application Cache: Proxy Cache:
http { ... location / { add_header Cache-Control public; expires 86400; etag on; } }
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