Skip to content

Commit 531b7cc

Browse files
authored
Merge pull request #222 from codeharborhub/dev-1
Added Caching docs for beginners tutorial
2 parents fa84097 + 785149c commit 531b7cc

File tree

5 files changed

+327
-0
lines changed

5 files changed

+327
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Caching",
3+
"position": 9,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Master the art of high-speed data retrieval. Learn how to reduce database load and make your applications lightning-fast using Caching and Redis."
7+
}
8+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
sidebar_position: 3
3+
title: "Caching Strategies"
4+
sidebar_label: "3. Caching Strategies"
5+
description: "Learn the industry-standard logic for managing when to store, update, and delete cached data."
6+
---
7+
8+
"There are only two hard things in Computer Science: cache invalidation and naming things."
9+
10+
If you cache a user's profile and they change their name, but your cache still shows the old name, your app is "broken" in the user's eyes. This is called **Stale Data**. We use different strategies to prevent this.
11+
12+
## 1. TTL (Time to Live)
13+
14+
This is the simplest and most common strategy. You give every piece of data an "expiry date."
15+
16+
* **How it works:** When you save data to the cache (like Redis), you set a timer (e.g., 3600 seconds). Once the timer hits zero, the cache automatically deletes that data.
17+
* **Best For:** Data that changes occasionally but isn't "mission critical" (like weather updates or trending posts).
18+
19+
## 2. Write Strategies
20+
21+
How does the data get into the cache in the first place?
22+
23+
<Tabs>
24+
<TabItem value="cache-aside" label="🏹 Cache Aside (Lazy Loading)" default>
25+
26+
### The "Check First" Method
27+
28+
The application code is responsible for managing the cache.
29+
1. Check cache.
30+
2. If found (Hit), return.
31+
3. If not found (Miss), get from DB and **then** save to cache.
32+
33+
* **Pros:** Only requested data is cached.
34+
* **Cons:** The first user always experiences a "slow" request (Cache Miss).
35+
36+
</TabItem>
37+
<TabItem value="write-through" label="✍️ Write Through">
38+
39+
### The "Always Sync" Method
40+
41+
Data is written to the cache and the database **at the same time**.
42+
43+
* **Pros:** Cache is never stale; data is always ready.
44+
* **Cons:** Writing data takes slightly longer because you are saving it in two places.
45+
46+
</TabItem>
47+
</Tabs>
48+
49+
## 3. Cache Eviction (The Cleanup)
50+
51+
What happens when your cache (RAM) is full? You can't just stop saving data. You have to kick something out.
52+
53+
* **LRU (Least Recently Used):** The most popular strategy. It deletes the data that hasn't been touched in the longest time.
54+
* **LFU (Least Frequently Used):** Deletes data that is requested the least number of times.
55+
* **FIFO (First In, First Out):** Deletes the oldest data regardless of how often it's used.
56+
57+
## 4. Cache Invalidation (The "Kill" Switch)
58+
59+
This is the process of manually removing data from the cache because you know the source data has changed.
60+
61+
**Example at CodeHarborHub:**
62+
A student updates their "Bio."
63+
1. Your code updates the **Database**.
64+
2. Your code immediately runs `cache.delete('user_101_bio')`.
65+
3. The next time the user visits their profile, the app sees a **Cache Miss** and fetches the new bio from the DB.
66+
67+
## Summary Checklist
68+
* [x] I understand that **TTL** is a timer that automatically clears old data.
69+
* [x] I know that **Cache Aside** is the most common way to load data.
70+
* [x] I understand that **LRU** helps manage limited memory by deleting unused data.
71+
* [x] I recognize that manual **Invalidation** is necessary when data is updated.
72+
73+
:::info The 80/20 Rule
74+
In most applications, 80% of your traffic comes from 20% of your data. Focus your caching efforts on that 20% (like top-selling products or homepage banners) to get the biggest performance boost!
75+
:::
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
sidebar_position: 2
3+
title: "Client-Side vs. Server-Side Caching"
4+
sidebar_label: "2. Client vs. Server"
5+
description: "Compare the two primary locations where caching occurs and learn when to use each."
6+
---
7+
8+
Caching isn't a single "thing" you turn on; it’s a strategy that happens at different points along the journey of a web request.
9+
10+
## 1. Client-Side Caching (The Browser)
11+
12+
This happens directly on the user's device (Phone, Laptop). The browser decides to save certain files locally so it doesn't have to ask the server for them again.
13+
14+
### What is cached here?
15+
* **Static Assets:** Images, Logos, CSS stylesheets, and JavaScript files.
16+
* **HTML:** Sometimes the entire structure of a page.
17+
18+
### How do we control it?
19+
We use **HTTP Headers** sent from the server:
20+
* `Cache-Control: max-age=31536000` (Tells the browser: "Keep this for 1 year!")
21+
* `ETag`: A unique "fingerprint" of a file. If the fingerprint hasn't changed, the browser uses the local copy.
22+
23+
## 2. Server-Side Caching (The Backend)
24+
25+
This happens on your server (where your Node.js/Python code lives). Instead of the user saving data, **you** save the data to avoid doing heavy work.
26+
27+
### What is cached here?
28+
* **Database Queries:** Results of "Top 10 Courses" or "User Profile."
29+
* **API Responses:** The JSON output from a third-party API.
30+
* **Computed Data:** Results of a complex mathematical calculation.
31+
32+
### How do we control it?
33+
We use in-memory stores like **Redis** or **Memcached**. The server checks the cache before it ever touches the database.
34+
35+
## The Comparison
36+
37+
| Feature | Client-Side Caching | Server-Side Caching |
38+
| :--- | :--- | :--- |
39+
| **Location** | User's Browser/Device | Your Backend Infrastructure |
40+
| **Best For** | Images, UI, Scripts | Data, Logic, Database Results |
41+
| **Control** | Suggestion (via Headers) | Full Control (via Code) |
42+
| **Goal** | Faster Page Load Time | Reduced Database Load |
43+
44+
## The Hybrid Flow
45+
46+
In a professional app at **CodeHarborHub**, we use **both**.
47+
48+
```mermaid
49+
sequenceDiagram
50+
participant U as User Browser
51+
participant S as App Server
52+
participant C as Redis Cache
53+
participant D as Database
54+
55+
U->>U: Check Browser Cache (Found Image? Yes!)
56+
U->>S: GET /profile (Not in Browser Cache)
57+
S->>C: Check Redis (Found Profile? No.)
58+
C->>D: Query Database
59+
D-->>C: Return Profile Data
60+
C-->>S: Save in Redis for 1 Hour
61+
S-->>U: Send JSON + Cache-Control Headers
62+
```
63+
64+
## Summary Checklist
65+
66+
* [x] I know that Client-side caching saves files on the user's device.
67+
* [x] I understand that `Cache-Control` headers manage browser behavior.
68+
* [x] I know that Server-side caching prevents repeated database queries.
69+
* [x] I understand that tools like Redis are used for server-side caching.
70+
71+
:::warning The "Clear Cache" Problem
72+
If you update your CSS file but the user's browser has it cached for a year, they won't see your changes! This is why we use **Cache Busting** (adding a version number like `style.css?v=2`).
73+
:::
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
sidebar_position: 4
3+
title: "Introduction to Redis"
4+
sidebar_label: "4. Redis Basics"
5+
description: "Learn about Redis, the world's most popular in-memory data store for caching and real-time performance."
6+
---
7+
8+
In our previous lessons, we learned the theory of caching. Now, let's look at the industry-standard tool: **Redis**.
9+
10+
**Redis** stands for **RE**mote **DI**ctionary **S**erver. It is an open-source, in-memory data structure store. Unlike a traditional database that writes to a hard drive, Redis keeps everything in the **RAM**.
11+
12+
## Why is Redis so fast?
13+
14+
A traditional database (like PostgreSQL or MySQL) is like a **Warehouse**; it’s huge but takes time to retrieve items. Redis is like your **Kitchen Counter**; it's small, but everything is right there and ready to use.
15+
16+
* **In-Memory:** RAM access is 1,000x faster than SSD/Hard Drive access.
17+
* **Simple Data Structures:** It doesn't use complex tables or joins. It uses simple keys and values.
18+
* **Single-Threaded:** It processes commands one by one, which avoids "locks" and keeps things predictably fast.
19+
20+
## How we use Redis at CodeHarborHub
21+
22+
As a Backend Developer, you will primarily use Redis for three things:
23+
24+
1. **Database Caching:** Storing the result of a slow SQL query.
25+
2. **Session Management:** Keeping users logged in (storing JWTs or Session IDs).
26+
3. **Rate Limiting:** Stopping hackers from hitting your API 1,000 times a second.
27+
28+
## Common Redis Commands
29+
30+
Redis is a **Key-Value** store. Think of it like a giant JavaScript Object that lives in your RAM.
31+
32+
<Tabs>
33+
<TabItem value="strings" label="🔤 Strings" default>
34+
35+
The most basic type of data.
36+
37+
```bash
38+
SET user:101 "Ajay Dhangar" # Save data
39+
GET user:101 # Retrieve data -> "Ajay Dhangar"
40+
EXPIRE user:101 60 # Delete after 60 seconds (TTL)
41+
```
42+
43+
</TabItem>
44+
<TabItem value="hashes" label="📂 Hashes">
45+
46+
Perfect for storing objects (like a User profile).
47+
48+
```bash
49+
HSET student:1 name "Aryan" role "Mentor"
50+
HGET student:1 name # -> "Aryan"
51+
```
52+
53+
</TabItem>
54+
<TabItem value="lists" label="📜 Lists">
55+
56+
Ordered collections of strings (perfect for a "Latest News" feed).
57+
58+
```bash
59+
LPUSH notifications "New Course Added!"
60+
LPUSH notifications "Welcome to CodeHarborHub"
61+
```
62+
63+
</TabItem>
64+
</Tabs>
65+
66+
## The Implementation Logic (Code View)
67+
68+
Here is how you would typically implement Redis in a **Node.js + Express** application:
69+
70+
```javascript
71+
// Example: Caching a User Profile
72+
async function getUserProfile(userId) {
73+
// 1. Try to find user in Redis
74+
const cachedUser = await redis.get(`user:${userId}`);
75+
76+
if (cachedUser) {
77+
console.log("⚡ Cache Hit!");
78+
return JSON.parse(cachedUser);
79+
}
80+
81+
// 2. If not in Redis, get from SQL Database
82+
console.log("🐢 Cache Miss. Querying Database...");
83+
const user = await db.users.findUnique({ where: { id: userId } });
84+
85+
// 3. Store in Redis for 1 hour so next time it's fast
86+
await redis.set(`user:${userId}`, JSON.stringify(user), 'EX', 3600);
87+
88+
return user;
89+
}
90+
```
91+
92+
## Summary Checklist
93+
94+
* [x] I understand that Redis is an in-memory store (RAM).
95+
* [x] I know that Redis uses a Key-Value structure.
96+
* [x] I can explain why Redis is used for caching and sessions.
97+
* [x] I understand basic commands like `SET`, `GET`, and `EXPIRE`.
98+
99+
:::success 🏆 Caching Module Complete!
100+
You now have the knowledge to make any application scale to millions of users. By combining **Relational Databases** with **Redis Caching**, you are building backends like a senior engineer!
101+
:::
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
sidebar_position: 1
3+
title: "What is Caching?"
4+
sidebar_label: "1. Concept of Caching"
5+
description: "Learn the fundamental concept of caching and how it speeds up data retrieval in backend systems."
6+
---
7+
8+
In the world of Backend Development, **Speed is King**. If your website takes 10 seconds to load, users will leave. **Caching** is the most powerful tool we have to make applications feel lightning-fast.
9+
10+
## The "Notebook vs. Library" Analogy
11+
12+
Imagine you are a researcher in a giant library (the **Database**). You are studying "Modern Web Architecture."
13+
14+
1. **The Library (Database):** Contains millions of books. To find a specific fact, you have to walk to the correct floor, find the shelf, and flip through pages. It takes **5 minutes**.
15+
2. **Your Notebook (The Cache):** You find the fact and write it down in a small notebook on your desk.
16+
3. **The Next Time:** Someone asks you that same fact. Instead of walking back into the library, you just look at your notebook. It takes **2 seconds**.
17+
18+
**Caching is the act of storing a copy of data in a high-speed "notebook" so that future requests for that data can be served faster.**
19+
20+
## How it works in a Web App
21+
22+
When a user requests data (like a list of top-selling products), the server follows this logic:
23+
24+
```mermaid
25+
graph TD
26+
A[User Request] --> B{Check Cache}
27+
B -- "Data Found (Cache Hit)" --> C[Return Data Instantly]
28+
B -- "Data Not Found (Cache Miss)" --> D[Fetch from Database]
29+
D --> E[Store copy in Cache]
30+
E --> F[Return Data to User]
31+
```
32+
33+
## Where can we Cache?
34+
35+
Caching happens at multiple levels of the "Stack."
36+
37+
<Tabs>
38+
<TabItem value="browser" label="🌐 Browser Cache" default >
39+
Your browser saves images, CSS, and JS files on your computer's hard drive so it doesn't have to download them every time you refresh.
40+
</TabItem>
41+
<TabItem value="cdn" label="☁️ CDN Cache">
42+
Content Delivery Networks (like Cloudflare) store your website's files on servers physically close to the user (e.g., a server in Mumbai for a user in MP).
43+
</TabItem>
44+
<TabItem value="server" label="🖥️ Server Cache">
45+
The backend saves the result of heavy database queries in the RAM (using tools like **Redis**) so the database doesn't get overwhelmed.
46+
</TabItem>
47+
</Tabs>
48+
49+
## Why do we need Caching?
50+
51+
1. **Speed (Lower Latency):** Users get their data in milliseconds instead of seconds.
52+
2. **Reduced Load:** Your database doesn't have to work as hard, which saves money and prevents crashes.
53+
3. **Predictability:** Even if 1 million people visit your site at once, the cache can handle the traffic better than a traditional database.
54+
55+
## Key Vocabulary
56+
57+
* **Cache Hit:** When the data is found in the cache. ⚡ (Fast!)
58+
* **Cache Miss:** When the data is not in the cache and must be fetched from the source. 🐢 (Slower)
59+
* **Stale Data:** When the data in the cache is old and different from the database. (The "Enemy" of caching!)
60+
61+
## Summary Checklist
62+
63+
* [x] I understand that caching is about storing data closer to the user for speed.
64+
* [x] I can explain the difference between a Cache Hit and a Cache Miss.
65+
* [x] I know that caching can happen in the browser, on a CDN, or on the server.
66+
* [x] I understand that caching protects the database from heavy traffic.
67+
68+
:::info Fun Fact
69+
Google Search is so fast because almost everything you search for is already cached in a server near you!
70+
:::

0 commit comments

Comments
 (0)