Tuesday, February 14, 2012

Cache explained - the milk story

Introduction

I came up with this little story when trying to explain what is caching, how to plan for caching and how to use it. Like many things in software, caching is something we use very widely in our day to day life without even knowing about it. Hopefully this example will give you a different, easier to understand point of view on caching that will help you get your head around it.

The milk story

No cache

Caching is like drinking milk.

When you want to drink milk you have to go to the store and buy it.

You can walk (56k modem) ride your bike (3G) or drive there in your corvette (high speed Internet).

You have to identify yourself, find and get the milk you want, check its expiration and pay for it. Since there are many things at the store it takes more time to find your milk.

Then you have to fight traffic back home. Pour the milk into a glass and drink it.

Caching options

Now, how do we make it better?

We notice that the store keeps the same milk for 10 days.

So, we can buy 10 days worth of milk ahead. We calculate our milk usage to be 0.5 liter a day, do we need 5 liters.

Wow - this is when we discover bulk discounts!

But - then we discover we need to store it all in the fridge. This type of storage is very expensive since it has many uses and is limited.

So we compromise and fetch only 2 liters at a time, knowing we will be back at the store once every 4 days.

Now, we notice we go to the store to get cookies with our milk. So when we go in for milk we use that same opportunity to get cookies as well.

Now every time we want milk we need to check our fridge first. If there is milk we can use it and we saved a lot of time and resources!

But, if there is no milk we have to go and get it. In the case we added some overhead by checking the fridge first...

Now, milk is known to go bad from time to time. So we have to check its expiration. If the milk gone bad we have to throw it.

That's another overhead every time we want milk.

Conclusion

Milk is your data.

The store is your data base.

Your fridge is your quick accessible cache storage.

Throwing out bad milk is flushing the cache when invalidated.

Cache is life ( ok, I went too far… ).

We use caching techniques every day!

No comments: