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)