Skip to content

Commit 65b4bb6

Browse files
authored
Merge pull request #223 from codeharborhub/dev-1
Added Caching docs for beginners tutorial
2 parents 531b7cc + 2a758ae commit 65b4bb6

File tree

15 files changed

+820
-2
lines changed

15 files changed

+820
-2
lines changed

absolute-beginners/backend-beginner/caching/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Caching",
3-
"position": 9,
3+
"position": 7,
44
"link": {
55
"type": "generated-index",
66
"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."
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Database Foundations",
3+
"position": 9,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Master the heart of any application: the Database. Learn how to store, organize, and retrieve data efficiently using industry standards."
7+
}
8+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
sidebar_position: 9
3+
title: "ACID Properties"
4+
sidebar_label: "9. ACID Properties"
5+
description: "Learn the four key properties—Atomicity, Consistency, Isolation, and Durability—that guarantee database reliability."
6+
---
7+
8+
When you are building an application like an E-commerce store or a Banking app, you cannot afford "glitches." If a user pays for a course but the database crashes halfway through, they might lose their money without getting the course.
9+
10+
**ACID** is a set of four properties that guarantee database transactions are processed reliably.
11+
12+
## 1. Atomicity (All or Nothing)
13+
14+
Think of a transaction as an "Atom"—it cannot be split. Either the **entire** transaction succeeds, or the **entire** transaction fails. There is no "halfway."
15+
16+
* **Real-World Example:** You transfer ₹500 to a friend.
17+
1. Money is deducted from your account.
18+
2. Money is added to your friend's account.
19+
* **If Step 2 fails (network error):** Atomicity ensures that Step 1 is "rolled back" so you don't lose your money!
20+
21+
## 2. Consistency (Following the Rules)
22+
23+
Consistency ensures that a transaction only takes the database from one valid state to another. Any data written to the database must follow all defined rules (Constraints).
24+
25+
* **Example:** If you have a rule that "Account Balance cannot be negative," and a transaction tries to withdraw more money than you have, the database will **reject** the transaction to stay "Consistent."
26+
27+
## 3. Isolation (Work in Private)
28+
29+
In a popular app like **CodeHarborHub**, thousands of people might be buying courses at the exact same time. **Isolation** ensures that concurrent transactions don't "trip over" each other.
30+
31+
* **How it works:** Even if 100 people hit the "Buy" button at the same second, the database processes them in a way that feels like they happened one after another. This prevents "Double Spending" or incorrect inventory counts.
32+
33+
## 4. Durability (Saved Forever)
34+
35+
Once a transaction is "Committed" (finished successfully), it is permanent. Even if the power goes out or the server crashes one second later, the data is safe.
36+
37+
* **How it works:** Databases use a **Transaction Log**. Before updating the main data, they write the change to a permanent log file on the hard drive. If the system crashes, it reads the log to recover the lost work.
38+
39+
## ACID Summary Table
40+
41+
| Property | Key Concept | Simple Goal |
42+
| :--- | :--- | :--- |
43+
| **Atomicity** | All or Nothing | Prevent partial updates. |
44+
| **Consistency** | Validity | Prevent "illegal" data. |
45+
| **Isolation** | Independence | Prevent data mix-ups. |
46+
| **Durability** | Permanence | Prevent data loss after crashes. |
47+
48+
## Summary Checklist
49+
* [x] I understand that **Atomicity** means no partial transactions.
50+
* [x] I know that **Consistency** keeps the data within the rules.
51+
* [x] I understand that **Isolation** handles multiple users at once.
52+
* [x] I know that **Durability** ensures data is saved to the disk forever.
53+
54+
:::info The Trade-off
55+
Relational databases (SQL) are famous for being strictly **ACID** compliant. Many NoSQL databases trade some ACID properties for extreme speed (often called **BASE**). At **CodeHarborHub**, we start with ACID because data safety is our #1 priority!
56+
:::
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
sidebar_position: 6
3+
title: "Database Migrations"
4+
sidebar_label: "6. Database Migrations"
5+
description: "Learn how to manage changes to your database schema safely and consistently across your entire team."
6+
---
7+
8+
Imagine you are working in a team of 5 developers at **CodeHarborHub**. You decide to add a `profile_picture` column to the `Users` table. You run the SQL command on your local computer, and everything works.
9+
10+
But when your teammate pulls your code, their app **crashes**. Why? Because their local database doesn't have that new column. **Migrations** solve this "It works on my machine" problem.
11+
12+
## What is a Migration?
13+
14+
A migration is a small file (usually JS, TS, or SQL) that describes a specific change to the database. These files are saved in your Git repository along with your code.
15+
16+
Each migration file usually has two parts:
17+
1. **Up:** The instructions to apply the change (e.g., `ADD COLUMN bio`).
18+
2. **Down:** The instructions to undo the change (e.g., `DROP COLUMN bio`).
19+
20+
## The Migration Workflow
21+
22+
Instead of manually typing SQL in a terminal, a professional developer follows this cycle:
23+
24+
1. **Create Migration:** You run a command to generate a new file.
25+
2. **Write Logic:** You define the change (e.g., "Create a table called `Lessons`").
26+
3. **Run Migration:** The tool executes the SQL and updates your local DB.
27+
4. **Commit to Git:** Your teammates pull the file and run it. **Now everyone is perfectly synced!**
28+
29+
## Why use Migrations?
30+
31+
<Tabs>
32+
<TabItem value="history" label="📜 History" default>
33+
You can see exactly who changed the database, when they changed it, and why.
34+
</TabItem>
35+
<TabItem value="safety" label="🛡️ Safety">
36+
If a database change breaks the app, you can "Rollback" to the previous version in seconds.
37+
</TabItem>
38+
<TabItem value="automation" label="🤖 Automation">
39+
When you deploy your app to a server (like AWS or Vercel), the server can automatically update the database during the build process.
40+
</TabItem>
41+
</Tabs>
42+
43+
## Example: A Migration File (Prisma)
44+
45+
At **CodeHarborHub**, we often use **Prisma**. Here is what a small migration looks like in the background:
46+
47+
```sql
48+
-- Migration: Add bio to Users
49+
-- UP
50+
ALTER TABLE "Users" ADD COLUMN "bio" TEXT;
51+
52+
-- DOWN
53+
ALTER TABLE "Users" DROP COLUMN "bio";
54+
```
55+
56+
## Summary Checklist
57+
58+
* [x] I understand that migrations are "Version Control" for the database.
59+
* [x] I know that migration files should be saved in Git.
60+
* [x] I understand the difference between **Up** (Apply) and **Down** (Undo).
61+
* [x] I recognize that migrations prevent "schema mismatch" errors in teams.
62+
63+
:::warning The Golden Rule
64+
**Never** manually change your database structure in a production environment. Always use a migration. If you skip a migration, your code and your database will eventually "drift" apart, leading to nightmare bugs!
65+
:::
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
sidebar_position: 7
3+
title: "Database Indexes & Performance"
4+
sidebar_label: "7. Indexes & Performance"
5+
description: "Learn how to make your database queries 100x faster by using Indexes and avoiding common performance traps."
6+
---
7+
8+
As your app at **CodeHarborHub** grows from 10 users to 10,000, your database will naturally slow down. A query that took **5ms** might suddenly take **2 seconds**. **Indexing** is the primary way we fix this.
9+
10+
## The "Book Index" Analogy
11+
12+
Imagine you have a 500-page book on "Node.js." You want to find the chapter on "Middleware."
13+
14+
1. **Without an Index (Full Table Scan):** You start at page 1 and flip through every single page until you find the word "Middleware." 🐢 (Very Slow)
15+
2. **With an Index:** You flip to the back of the book, look up "M" for Middleware, see it's on page 245, and jump straight there. ⚡ (Very Fast)
16+
17+
## How an Index Works
18+
19+
When you create an index on a column (like `email`), the database creates a separate, hidden "mini-table" that is sorted alphabetically.
20+
21+
Instead of looking through the main table, the database searches this sorted list first. Since it's sorted, it can use a **Binary Search** to find the data instantly.
22+
23+
```sql
24+
-- How to add an index in SQL
25+
CREATE INDEX idx_user_email ON Users(email);
26+
```
27+
28+
## The Trade-off: Read vs. Write
29+
30+
Indexes aren't "free." Every time you add an index, you are making a trade-off.
31+
32+
| Action | Impact with Index | Why? |
33+
| :--- | :--- | :--- |
34+
| **SELECT** (Read) | 100x Faster | The database jumps straight to the data. |
35+
| **INSERT** (Write) | Slower | The DB must update the table **and** the index. |
36+
| **STORAGE** | Increased | The index takes up extra space on the disk. |
37+
38+
## When should you use an Index?
39+
40+
Don't index every single column! At **CodeHarborHub**, follow these rules:
41+
42+
<Tabs>
43+
<TabItem value="do" label="✅ Do Index" default>
44+
1. **Primary Keys:** Most databases index these automatically.
45+
2. **Foreign Keys:** Columns used in `JOIN` operations.
46+
3. **Search Columns:** Columns used in `WHERE` clauses (e.g., `email`, `username`).
47+
</TabItem>
48+
<TabItem value="dont" label="❌ Don't Index">
49+
1. **Small Tables:** If a table has only 50 rows, an index is overkill.
50+
2. **Frequently Changing Data:** Columns that get updated 100 times a second.
51+
3. **Low Cardinality:** Columns with very few options (e.g., a `Gender` column with only 3 options).
52+
</TabItem>
53+
</Tabs>
54+
55+
## Tools for Performance
56+
57+
How do you know if your query is slow? Use the `EXPLAIN` command.
58+
59+
```sql
60+
EXPLAIN ANALYZE SELECT * FROM Users WHERE email = 'ajay@example.com';
61+
```
62+
63+
*This will tell you if the database used an index or if it had to read every single row.*
64+
65+
## Summary Checklist
66+
67+
* [x] I understand that an Index is a sorted pointer to data.
68+
* [x] I know that Indexes make **Reads** faster but **Writes** slower.
69+
* [x] I understand that `JOIN` columns and `WHERE` columns are the best candidates for indexing.
70+
* [x] I know how to use `EXPLAIN` to check query performance.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
sidebar_position: 1
3+
title: "Introduction to Databases"
4+
sidebar_label: "1. What is a Database?"
5+
description: "Understand the core concept of databases and why they are the backbone of every modern application."
6+
---
7+
8+
In the early days of computing, programmers saved data in simple text files. But imagine trying to find one specific user in a file containing 10 million names! It would be slow, prone to errors, and impossible for two people to edit at the same time.
9+
10+
A **Database** is an organized collection of structured information, or data, typically stored electronically in a computer system.
11+
12+
## The DBMS: The Librarian
13+
14+
When we talk about "The Database," we usually mean the **Database Management System (DBMS)**.
15+
16+
Think of the Database as a **Library** and the DBMS as the **Librarian**. You don't walk into the stacks and grab books yourself; you ask the Librarian (DBMS) to find, add, or update information for you.
17+
18+
### Why do we need a DBMS?
19+
20+
* **Massive Scale:** Handles millions of rows without breaking a sweat.
21+
* **Concurrency:** Allows 1,000+ users to read and write data at the exact same millisecond.
22+
* **Data Integrity:** Ensures that your "Age" column only contains numbers, not "Mango."
23+
* **Security:** Controls exactly who can see or change specific pieces of data.
24+
25+
## The 4 Core Operations (CRUD)
26+
27+
No matter how complex an application like **CodeHarborHub** becomes, almost every interaction with a database boils down to these four actions:
28+
29+
| Action | SQL Command | Real-World Example |
30+
| :--- | :--- | :--- |
31+
| **C**reate | `INSERT` | A new student signs up for a course. |
32+
| **R**ead | `SELECT` | You view your dashboard to see your progress. |
33+
| **U**pdate | `UPDATE` | You change your profile picture or password. |
34+
| **D**elete | `DELETE` | You unsubscribe or remove a post. |
35+
36+
## Database Categories
37+
38+
Before you write your first line of code, you must choose the "Type" of database. At **CodeHarborHub**, we primarily focus on the first two:
39+
40+
<Tabs>
41+
<TabItem value="relational" label="📊 Relational (SQL)" default>
42+
43+
### Structured & Strict
44+
Data is organized into tables (like Excel) with fixed columns. Great for complex relationships.
45+
* **Examples:** PostgreSQL, MySQL, SQLite.
46+
47+
</TabItem>
48+
<TabItem value="non-relational" label="📄 NoSQL">
49+
50+
### Flexible & Fast
51+
Data is stored in "Documents" (like JSON). Great for rapidly changing data or massive real-time feeds.
52+
* **Examples:** MongoDB, Cassandra, Firebase.
53+
54+
</TabItem>
55+
</Tabs>
56+
57+
---
58+
59+
## Summary Checklist
60+
* [x] I understand that a database is more than just a "file"; it's a managed system.
61+
* [x] I know that the **DBMS** is the software that manages the data.
62+
* [x] I can name the four **CRUD** operations.
63+
* [x] I recognize the difference between **SQL** (Tables) and **NoSQL** (Documents).
64+
65+
:::tip Career Insight
66+
In your journey at **CodeHarborHub**, you will find that 90% of "Backend Development" is actually just moving data in and out of a database efficiently. Master the database, and you master the backend!
67+
:::
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
sidebar_position: 11
3+
title: "The N+1 Query Problem"
4+
sidebar_label: "11. N+1 Problem"
5+
description: "Learn how to identify and fix the N+1 query problem to prevent your database from slowing down as your data grows."
6+
---
7+
8+
As a developer at **CodeHarborHub**, you want your app to be fast. However, many beginners accidentally write code that forces the database to do 100 times more work than necessary. This is known as the **N+1 Problem**.
9+
10+
## 🧐 What is the N+1 Problem?
11+
12+
This problem occurs when your application makes **one** initial query to fetch a list of items (N), and then makes **one additional query for each** item to fetch related data.
13+
14+
### The "Grocery Store" Analogy
15+
Imagine you need to buy 10 different items from the store:
16+
* **The N+1 Way:** You drive to the store, buy milk, and drive home. Then you drive back, buy bread, and drive home. You repeat this **10 times**. (Exhausting!)
17+
* **The Efficient Way:** You make a list of all 10 items, drive to the store **once**, buy everything, and drive home. (Fast!)
18+
19+
## Seeing it in Code
20+
21+
Imagine we want to display a list of 10 **Users** and their **Posts**.
22+
23+
### The Bad Way (N+1)
24+
If you use a loop to fetch related data, you create an N+1 disaster.
25+
26+
```javascript
27+
// 1 Query to get 10 users
28+
const users = await prisma.user.findMany();
29+
30+
// 10 separate queries (one inside each loop)
31+
for (const user of users) {
32+
const posts = await prisma.post.findMany({
33+
where: { userId: user.id }
34+
});
35+
console.log(`${user.name} has ${posts.length} posts.`);
36+
}
37+
// TOTAL QUERIES: 1 + 10 = 11
38+
````
39+
40+
If you had 1,000 users, your app would hit the database **1,001 times** just to load one page!
41+
42+
## The Solution: Eager Loading
43+
44+
Instead of fetching related data inside a loop, we tell the database to join the tables and give us everything in **one single trip**.
45+
46+
### The Good Way (Eager Loading)
47+
48+
In modern ORMs like **Prisma**, we use the `include` keyword.
49+
50+
```javascript
51+
// ONE SINGLE QUERY to get 10 users AND all their posts
52+
const usersWithPosts = await prisma.user.findMany({
53+
include: {
54+
posts: true,
55+
},
56+
});
57+
58+
// Now the data is already in memory! No more DB hits.
59+
usersWithPosts.forEach(user => {
60+
console.log(`${user.name} has ${user.posts.length} posts.`);
61+
});
62+
// TOTAL QUERIES: 1
63+
```
64+
65+
## Comparison: Why it matters
66+
67+
| Feature | N+1 Strategy | Eager Loading (The Fix) |
68+
| :--- | :--- | :--- |
69+
| **Database Trips** | 1 + N (Many) | 1 (Single) |
70+
| **Performance** | Slows down as data grows | Consistently fast |
71+
| **Network Latency** | High (Multiple Roundtrips) | Low (One Roundtrip) |
72+
| **Server Load** | Very High | Minimal |
73+
74+
## How to Spot it?
75+
76+
1. **Check your Logs:** If you see the same `SELECT` statement repeating 20 times in your console, you have an N+1 problem.
77+
2. **Use Tools:** Tools like **Prisma Studio**, **Hibernate Profiler**, or even simple `console.log` can help you count your queries.
78+
79+
## Summary Checklist
80+
81+
* [x] I understand that N+1 means making a separate query for every item in a list.
82+
* [x] I know that loops + database queries = **Performance Disaster**.
83+
* [x] I understand that **Eager Loading** (Joins) is the primary solution.
84+
* [x] I can identify N+1 problems by looking at my server logs.
85+
86+
:::success You've Mastered the Foundations!
87+
By understanding the N+1 problem, you've moved past "beginner" coding. You are now thinking about **Scalability** and **Efficiency**—the marks of a true Senior Backend Engineer at **CodeHarborHub**.
88+
:::

0 commit comments

Comments
 (0)