Cache
Read Through Cache¶
- Upon receiving a request, a web server checks if the cache has the available response.
- If it has, it sends the data back to the client
- It id doesn't, it queries the database, stores the response in cache, and sends it back to the client.
How to interact with caches?¶
- Most cache servers will provide you with APIs for using them
- E.g. Memcached APIs #todo
Things to Consider when Caching:¶
- When and if to use Cache?
- When data is read frequently and modified infrequently
- Cache server contains temporary data (No Persistent data), all data is lost if server restarts. Important data must be stored in persistent data stores.
- Expiration Policy
- Cached data is removed from cache when cached data is expired.
- If no expiration policy,
- data will be stored in memory permanently till storage fills up
- If expiration policy
- too short, higher reloading of data from the database too frequently.
- too long, data might become stale
- Consistency
- This involves keeping the cache and the data-store (Database) in sync
- Scaling across multiple regions, maintaining consistency between data store and cache is a challenging task. (Reference -> Scaling Memcache at Facebook) #todo
- Handling Failures
- Single cache server can be a victim of Single Point of Failure(SPOF) #todo
- Overprovision the required memory by certain percentages.
- Make additional memory available than what is to be expected. This will help in case of errors and bugs needing more memory than expected.
- Eviction Policy
- How do you remove content from the cache? Which algorithm to use?
- Least Recently Used (LRU)
- Least Frequently Used (LFU)
- First In First Out (FIFO)
- One of the above or many other algorithms is used to decide the way using which new data would remove existing data in the cache.
- How do you remove content from the cache? Which algorithm to use?