Caching
Cache is a special storage sits closer to the
resource that often needs it saves the data read
from remote origin ...
Goal
The goal of caching is never having to generate the
same response twice for the same request
res1
req1
req2
1res
res2...
Cache Server
Web Cache that sits between one or more webserver and the
client and caches the response for a given request ...
Benefits of Cache
• To reduce latency – Request is served from the
cache instead of origin server and hence it
takes less ...
Limitations of Cache
• If cache data are not validated properly, there
is a possibility of serving stale data.
• There is ...
HTTP Cache
HTTP provides headers to manage the cache
effectively. HTTP defines following three
mechanism to control caches...
Max-age Header
userA
userA
cache
cache
server
sever
Get/welco
me Get/welco
me
200 ok cache-control : max-
age=600 Hello Wo...
Max-age Header
userB
userB
cache
cache
server
sever
Get/welco
me
200 ok cache-control : max-
age=600 Hello World!
Validations
userA
userA
cache
cache
server
sever
Get/welco
me Get/welco
me
200 ok last-modified: …,
Etag: ae2.. Hello Worl...
Validations
userB
userB
cache
cache
server
sever
Get/welco
me Get/welco
me
304 Not Modified.
Generate
validator.
No
repons...
Cache implementation
3 things to consider before designing the cache
• Loading data to cache
• Keep Cache data in Sync wit...
Pre-loading Cache
Pre-loading – Load the cache with data when
the system initialize and even before serving the
request. I...
Lazy-Loading
In Lazy loading, data is loaded into the cache
when the request is made the first time to fetch
data from the...
Keep Cache data in Sync
In order to keep the data in the cache in sync
with the remote system whose data can be
changed in...
Expiry Time
Expiry Time: One way to prevent serving stale
data from cache is to expire the data at certain
time interval.
...
Active Expiry
By active expiration, actively expire the data in
the cache in the event of update happen at the
remote syst...
Cache size
• Since cost of managing the cache is high, there
is always limitation on the amount data that
cache can hold a...
Eviction Policy
• Time Based Eviction – Data will be evicted as
soon data expires.
• LRU – least recently used data will b...
Tiers of Cache
1. Cache like Memcache between Application
server and Database to reduce database load
2. Cache services li...
Database
Memecache/Redis
Application server
Reverse proxy
Application server Application server
Redis
service
Redis
servic...
Caching Frameworks
and tools
• Redis as Cache
• EHCache
• MemCache
• Guava
Redis
• Redis and memcache both are kev-value store
and support persistence.
• Memcache supports string as key where as in...
The End
Thanks!
Upcoming SlideShare
Loading in …5
×

Caching

279 views

Published on

Basic introduction to cache and how it works. It als discuss about cache types and implementation techniques. Its advantages and drawbacks. Caching frameworks and tools.

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
279
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Caching

  1. 1. Caching Cache is a special storage sits closer to the resource that often needs it saves the data read from remote origin server under the assumption that same data will be requested again. Cache typically can keep data in memory or disk
  2. 2. Goal The goal of caching is never having to generate the same response twice for the same request res1 req1 req2 1res res2 res2 req1 Client Cache Server
  3. 3. Cache Server Web Cache that sits between one or more webserver and the client and caches the response for a given request and never hit the origin server if another request comes for same URL Cache server Origin ServerClient req req resres
  4. 4. Benefits of Cache • To reduce latency – Request is served from the cache instead of origin server and hence it takes less time to get the response. • Reduce network bandwidth – Because the responses are reused, it reduce the network bandwidth and keep the bandwidth requirement lower. • Improves performance and scalability without adding much cost.
  5. 5. Limitations of Cache • If cache data are not validated properly, there is a possibility of serving stale data. • There is a limitation on size data that can be cached.
  6. 6. HTTP Cache HTTP provides headers to manage the cache effectively. HTTP defines following three mechanism to control caches 1. Freshness, Validation, Invalidation Max-age, Expires header checks for the expiry of the cached data. Last-Modified, if-modified-since and Etag are used to perform validation on cached data. If a URL associated with cached response gets a subsequent Post/Put/Delete request, cache needs to be invalidated.
  7. 7. Max-age Header userA userA cache cache server sever Get/welco me Get/welco me 200 ok cache-control : max- age=600 Hello World! max-age Hello World!200 ok cache-control : max- age=600 Hello World!
  8. 8. Max-age Header userB userB cache cache server sever Get/welco me 200 ok cache-control : max- age=600 Hello World!
  9. 9. Validations userA userA cache cache server sever Get/welco me Get/welco me 200 ok last-modified: …, Etag: ae2.. Hello World! Generate validator Hello World! 200 ok last-modified: …, Etag: ae2.. Hello World!
  10. 10. Validations userB userB cache cache server sever Get/welco me Get/welco me 304 Not Modified. Generate validator. No reponse 200 ok last-modified: …, Etag: ae2.. Hello World!
  11. 11. Cache implementation 3 things to consider before designing the cache • Loading data to cache • Keep Cache data in Sync with Remote Server • Cache size.
  12. 12. Pre-loading Cache Pre-loading – Load the cache with data when the system initialize and even before serving the request. It will be very fast since the data is loaded during initialization or system startup. Drawback is that we may not know what data to be cached upfront. You may cache data that is never used.
  13. 13. Lazy-Loading In Lazy loading, data is loaded into the cache when the request is made the first time to fetch data from the remote system. First request will be slower than the subsequent one since it takes time to fetch data from remote server and insert into the cache.
  14. 14. Keep Cache data in Sync In order to keep the data in the cache in sync with the remote system whose data can be changed independent of cache. It is recommended to expire the data in the cache. To avoid stale data. Two types: Time Expiry and Active Expiry
  15. 15. Expiry Time Expiry Time: One way to prevent serving stale data from cache is to expire the data at certain time interval. When the data has expired, it will be removed from the cache and data will be fetched again from the remote server.
  16. 16. Active Expiry By active expiration, actively expire the data in the cache in the event of update happen at the remote system. When the remote system is updated, send a message to cache instructing to expire the data that was just updated.
  17. 17. Cache size • Since cost of managing the cache is high, there is always limitation on the amount data that cache can hold at a time. • In order to effectively use the available cache, we need a mechanism to evict the old data to make space for new data.
  18. 18. Eviction Policy • Time Based Eviction – Data will be evicted as soon data expires. • LRU – least recently used data will be evicted • MRU – Most recently used data will be evicted.
  19. 19. Tiers of Cache 1. Cache like Memcache between Application server and Database to reduce database load 2. Cache services like Redis cache used within the application server to reduce the overhead of assembling API responses 3. An API Proxy like reverse proxy that can cache the complete API response, reducing the load on the application server 4. CDN like Akamai that can provide extra caching layer for large files that do not change often.
  20. 20. Database Memecache/Redis Application server Reverse proxy Application server Application server Redis service Redis service Redis service
  21. 21. Caching Frameworks and tools • Redis as Cache • EHCache • MemCache • Guava
  22. 22. Redis • Redis and memcache both are kev-value store and support persistence. • Memcache supports string as key where as in redis we can use any object as keys.
  23. 23. The End Thanks!

×