DevScript Saga

Welcome to DevScript Saga!

A little Helping Hand for Devs

A cache is a temporary storage area that stores frequently accessed data in memory so that subsequent requests are served quickly. The application performance is greatly affected by calling database repeatedly, the cache can mitigate this problem.

Cache can be fitted in different layers such as Client side, DNS, Web server, application, database, CDN, CPU cache, disk cache etc.

Benefits of having separate cache tier include better system performance, ability to reduce database workloads, ability to scale cache tier independly.

Cache Invalidation:

If data is modified in the database, it should be invalidated in cache, if not, this can cause inconsistent application behaviour. There are 2 kinds of caching systems:

  1. Write through cache: writes are confirmed as successful only if both cache and DB are updated successfully. It gives complete data consistency at cost of higher latency for write operations.
  2. Write around cache: write directly goes to database bypassing the cache.
  3. Write back cache: write directly done to caching layer and write is confirmed as soon as write to cache completes. Cache then asynchronously sync this write to database. It gives quick write latency and high throughput but there is risk of loosing data in cache layer.

Considerations for using cache:

  1. Expiration policy: Once cached data is expired, it is removed from the cache. When there is no expiration policy, cached data will be stored in memory permanently. Expiration time should not be too short or too long.
  2. Eviction policy: Once cache is full, any request to add items to cache might remove existing items. This is called as cache eviction. Some of the common eviction policies are : LRU (Least Recently Used), LFU (Least Frequently used), FIFO (First in first out), LIFO (Last in First out), MRU (Lost Recently used), Random replacement.

Databases used for distributed caching include Redis, Memcached, Hazelcast, Apache Ignite.

Leave a comment