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)