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
Next time:
Browser โ Server โ Check cache โ Data found!
Return immediately (skip database!)
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)