- Sloth Bytes
- Posts
- š¦„The 3 Types of Caches
š¦„The 3 Types of Caches
Read on our website
Hello friends!
Welcome to this weekās Sloth Bytes. I hope you had a great week.
Design results
Option 1 is the winner!

More people preferred this!
Iām gonna refine it a bit and then Iāll start adding more visuals š
One more thing!
Recent newsletters have been getting clipped due to the length.
So I was thinking of potentially splitting the newsletter into 2 emails.
Email with only Advice + challenge
Email with only programming news + new/useful tools
Each email would be more in-depth with the content (more news, developer tools, etc.)
What do you think?
Should I split the newsletter into 2 emailsBoth emails would be more in-depth and useful! |

𦄠No sponsor this week, just vibes.
But if you want to reach 50,000+ developers, founders, and tech lovers who actually open their emails ā this is the place.

The 3 Types of Caches
You know whatās interesting?
A cache is probably one of the easiest performance win you'll ever get.
But most developers donāt understand caching.
To be fair, a lot of tools automatically do it for you.
The problem with that is you probably donāt understand what should be cached or how it works.
Disclaimer: Thereās A LOT more types of caches, but for programming & web dev, these are the most important ones for us.
Each layer caches different things at different locations.
What Is a Cache?
A cache is temporary storage for data you'll need again soon.
Instead of fetching/calculating data every time, you store it once and reuse it.
Youāre trading memory/storage for speed.
Itās like when you have multiple tabs open.
You leave them open because āyou might need it.ā
You donāt btw. Close those tabsā¦
1. Browser Cache
Where is this cache: User's browser
For What: HTML, CSS, JS, images
How it works: Server tells browser what to cache via HTTP headers:
Cache-Control: public, max-age=3153600
This says: "Cache this for 1 year (31536000 seconds). Don't ask me again."
Now next time when a user visits a website the browser checks if they have it.
If yes, they use cache version
If no, they download it again.
Cache Busting
Now what if the browser caches a broken version of your page.
Thatās not goodā¦
Even if you deploy a working version, it wonāt be used until the cache expires!
Now you have to cache bust.
One of the most common cache busting techniques is to add a version number to the file name itself.
Change styles.css to styles.v2.css
You could also add that version number as a file path or as a query string:
styles.css ā /v2/styles.css
https://example.com/styles.css ā https://example.com/styles.css?v=2
Yeah itās that simple.
Sloth that sounds annoying and miserable.
No itās not? Just donāt write any mistakes.
Iām kidding, you can automate this process with a build tool.
Itāll still be annoying and have edge cases, but hey itās automatedā¦
2. Memory Cache
This is the type that everyone talks about the most.
Where is this cache: Your computerās memory.
For What: Database results, API responses, calculations
How it works: You create a data storage layer that temporarily stores frequently used data.
Without cache:
app.get('/data/:id', async (req, res) => {
const expensiveData = await getExpensiveData(req.params.id); // Pretend this exists
res.json(expensiveData);
});With cache:
//Yep a cache can be this simple...
const cache = new Map();
app.get('/data/:id', async (req, res) => {
const key = `user:${req.params.id}`;
// If the cache already has the data, we don't have to fetch again
if (cache.has(key)) {
return res.json(cache.get(key)); // 2ms
}
//Not in cache so we have to fetch it
const expensiveData = await getExpensiveData(req.params.id);
cache.set(key, expensiveData);
res.json(expensiveData);
});Pretty easy to understand here. If youāre more curious about it you can read my redis post!
3. CDN Cache
Where is this cache: Servers worldwide near users
For what: Images, CSS, JS, videos
How it works: Storing copies of a website's content on a network of servers that are closer to users
Itās essentially the same as the browser cache where it uses HTTP headers.
The difference is instead of the origin server, it uses a server close to you
Without CDN: User in Australia ā Server in New York = 250ms latency
With CDN: User in Australia ā CDN in Sydney = 20ms
Bonus: Hardware Caches (CPU, GPU, DSPs)
Itās a bonus because I know nothing about hardware and refuse to study this right now, but you can learn about it here.
Fun cache test you can do right now
Open DevTools ā Network tab ā disable cache button
Refresh your website and see how long it takes to load.
Try again but with the cache on.



Thanks to everyone who submitted!
Dependable Jobs Schedule
Youāre given a number of jobs and a list of dependencies.
Each job is labeled from 0 to jobs - 1.
Each dependency [a, b] means job a can only start after job b is finished.
Return true if all jobs can be finished, or false if thereās a circular dependency.
finishAll(2, [[1, 0]])
output = True
finishAll(2, [[1, 0], [0, 1]])
output = False
## job 1 depends on job 0
## job 0 also depends on job 1
## ā circular dependency, cannot complete either job
finishAll(3, [[1, 0], [2, 1]])
output = True
## job 0 ā job 1 ā job 2
## no cycles, all jobs can be finished in order
finishAll(1, [])
output = true
## only one job (0) with no dependencies
## ā can be completed immediately
finishAll(11, [[6, 10], [4, 3], [9, 2], [2, 3], [6, 1], [2, 8], [10, 1], [10, 2], [5, 3], [0, 10], [7, 4], [6, 1]])
output = trueHow To Submit Answers
Reply with
A link to your solution (github, twitter, personal blog, portfolio, replit, etc)
or if youāre on the web version leave a comment!
If you want to be mentioned here, Iād prefer if you sent a GitHub link or Replit!

New silly video!
Thatās all from me!
Have a great week, be safe, make good choices, and have fun coding.
If I made a mistake or you have any questions, feel free to comment below or reply to the email!
See you all next week.
What'd you think of today's email? |
Want to advertise in Sloth Bytes?
If your company is interested in reaching an audience of developers and programming enthusiasts, you may want to advertise with us here.






Reply