Caching Simplified: Effective Eviction and Invalidation Policies with Real-Life Examples

Jaspreet Singh Sodhi
3 min readJul 30, 2024

--

What is Caching?

Caching is like having a small, frequently accessed storage space to keep things you need often, so you don’t have to go to the main storage (like a pantry or a grocery store) each time you need something.

Real-Life Example:

Imagine your refrigerator is a cache and your pantry is the main storage. When you frequently use certain items (like milk, eggs, or butter), you keep them in the fridge (cache) because it’s more convenient than going to the pantry (main storage) every time you need them.

Cache Hit vs. Cache Miss

  • Cache Hit: This occurs when you find what you need in the fridge. For example, if you open the fridge and find the milk right there, that’s a cache hit. You don’t need to go to the pantry to get it.
  • Cache Miss: This occurs when the item you need is not in the fridge, so you have to go to the pantry to get it. If you open the fridge and there’s no milk, you experience a cache miss and have to go to the pantry to fetch more milk.

Cache Eviction & Cache Invalidation

  • Cache Eviction: This is the process of making room in the fridge by removing items. For instance, if your fridge is full and you need to put in a new carton of milk, you might need to take out an old jar of pickles that hasn’t been used in a while to make space.
  • Cache Invalidation: This ensures that the items in your fridge are fresh and up-to-date. For example, if the milk in the fridge goes bad, you need to throw it out and replace it with a new carton. Similarly, if you get a fresh batch of vegetables, you might need to replace the old ones in the fridge.

Cache Eviction Policies

  1. Least Recently Used (LRU): Remove items that haven’t been used in the longest time. Like taking out the old jar of pickles that hasn’t been touched in months.
  2. First In First Out (FIFO): Remove the oldest items first, regardless of how often they are used. For example, taking out the oldest carton of milk.
  3. Least Frequently Used (LFU): Remove items that are used the least. If you haven’t used a certain sauce in weeks, you might decide to take it out.
  4. Random Replacement (RR): Remove any random item to make space. This might be like just picking any item out of the fridge at random, which is less common in practical use.

Cache Invalidation Policies :

1. Time-Based Invalidation:

  • Concept: Invalidates items after a specific period.
  • Example: The coffee shop might decide that any open container of cream should be discarded after 24 hours to ensure freshness, regardless of whether it’s finished or not.

2. Event-Based Invalidation:

  • Concept: Invalidates items based on specific events.
  • Example: If a new batch of cookies arrives, the coffee shop might remove all existing cookies from the display to ensure only the freshest ones are available. The arrival of new cookies triggers the invalidation.

3. Manual Invalidation:

  • Concept: Requires manual intervention to invalidate items.
  • Example: A barista notices that a certain syrup has changed color, indicating it’s no longer good. They manually remove and replace it, ensuring the quality of drinks served.

By using this intuitive example, you can see how caching works in everyday life and why managing it effectively is important for maintaining efficiency and convenience.

That’s it!. Feel free to follow me and share your thoughts on what else I can improve.

Do checkout the my complete ongoing series on System Design —

jaspreetsodhi02.medium.com/list/system-design-series-f5054fed26fd

See you in the next part! 😊

--

--

Jaspreet Singh Sodhi

Full Stack Software Engineer | Curating Top-Notch Content @jaspreet.dev on Instagram ✨