DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

๐Ÿ—„๏ธ Caching Explained Like You're 5

Keeping snacks on your desk

Day 12 of 149

๐Ÿ‘‰ Full deep-dive with code examples


The Snack Drawer

You're working. You get hungry.

Without snack drawer:

  • Walk to kitchen (far!)
  • Open fridge
  • Get snack
  • Walk back
  • Takes a while ๐Ÿ˜ซ

With snack drawer:

  • Open desk drawer
  • Grab snack
  • Takes a few seconds! ๐ŸŽ‰

The snack drawer is your cache!


In Computers

Your app needs user data.

Without cache:

  • Ask database (slow!)
  • Database searches millions of records
  • Sends data back
  • Might take noticeably longer ๐Ÿ˜ซ

With cache:

  • Check cache (in memory, super fast!)
  • Data is already there
  • Often returns much faster ๐Ÿš€

How It Works

First time someone asks for data:

Browser โ†’ Server โ†’ Database โ†’ Data found!
                     โ†“
          Store in cache for later
                     โ†“
         Return to browser
Enter fullscreen mode Exit fullscreen mode

Next time:

Browser โ†’ Server โ†’ Check cache โ†’ Data found!
         Return immediately (skip database!)
Enter fullscreen mode Exit fullscreen mode

The Catch

What if data changes?

  • You cached "User has 5 friends"
  • User adds a friend
  • Cache still says 5 friends!

Solution: Caches expire after some time, or get cleared when data changes.


In One Sentence

Caching stores frequently-used data closer to you so you don't have to fetch it from far away every time.


๐Ÿ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)