System DesignMastery
--Core Components — মূল বিল্ডিং ব্লক

Database Fundamentals

Duration৬০-৯০ মিনিট
LevelIntermediate
FocusTheory & Labs
001Why It Matters

Database কেন এত গুরুত্বপূর্ণ?

একটা system এর performance এর ৮০% নির্ভর করে database design এর উপর। ভুল database choose করলেন, index না দিলে, বা wrong schema করলেন — ১০০ জন user এলেই সব crash করে।

Interview এ "Design Instagram" বললে — কোন database? SQL নাকি NoSQL? Photos কোথায়? Follow relationship কীভাবে? Feed কীভাবে fast? — সব database question। Database choice ই system এর সবচেয়ে critical decision।

DEFINITION

Database হলো organized data storage system। শুধু store নয় — efficiently retrieve, update, delete এবং manage করাই database এর কাজ। একই query milliseconds এ return করবেন নাকি seconds এ — সেটা নির্ভর করে database design এর উপর।

002SQL vs NoSQL

SQL vs NoSQL — সবচেয়ে বড় decision

প্রতিটি project এর শুরুতে এই decision নিতে হয়। কোনো একটা "সবসময় better" না — use case বুঝে decide করতে হয়।

📊 SQL — Relational

Structured data, ACID transactions, complex JOINs। Fixed schema। Banking, e-commerce orders, user accounts এর জন্য best।

🌊 NoSQL — Non-Relational

Flexible schema, horizontal scaling, high throughput। Social feed, caching, IoT, product catalog এর জন্য best।

বিষয়SQLNoSQL
SchemaFixed (ALTER TABLE লাগে)Flexible
ScalingVertical (harder horizontal)Horizontal (সহজ)
ACIDFull ACID supportEventual consistency
JOINComplex JOINs সহজJOIN কঠিন বা অসম্ভব
Query LanguageStandard SQL (সব DB তে একই)Database-specific API
Best ForBanking, Orders, Auth, ReportingFeed, Cache, IoT, Search

NoSQL এর ৪টি Type — কোনটা কী কাজে

TypeDatabaseBest For
Key-ValueRedis, DynamoDBCaching, Sessions, Shopping cart, Rate limiting
DocumentMongoDB, FirestoreUser profiles, Product catalog, Blog posts
Column-familyCassandra, HBaseTime-series, IoT sensor data, Analytics
GraphNeo4j, Amazon NeptuneSocial network, Fraud detection, Recommendation

🎯 Interview এ এটা বলুন

"আমি SQL দিয়ে শুরু করবো কারণ ACID guarantee এবং mature tooling। Data model unclear বা massive write scale দরকার হলে NoSQL consider করবো। Real production systems এ সাধারণত দুটোই use হয় — Polyglot Persistence।" এটাই industry standard answer।

003ACID Properties

ACID — Transaction এর ৪টি Guarantee

ACID মানে Atomicity, Consistency, Isolation, Durability। Bank transfer ভাবুন — টাকা একটা account থেকে কাটলে অবশ্যই অন্যটায় যোগ হতে হবে। এই guarantee দেয় ACID।

AATOMICITYAll or Nothingসব হবে না হয়কিছুই নাCCONSISTENCYBusiness rulesসবসময় validstate maintainIISOLATIONConcurrent txnএকে অন্যকেaffect করে নাDDURABILITYCommitted datacrash এ হারাবে না(WAL log দিয়ে)
bank-transfer.sql
-- Bank Transfer — ACID transaction এর classic example
BEGIN TRANSACTION;

-- Step 1: Sender থেকে টাকা কাটো
UPDATE accounts SET balance = balance - 5000
WHERE account_id = 101;

-- Step 2: Balance negative হলে সব undo — Atomicity!
IF (SELECT balance FROM accounts WHERE account_id = 101) < 0 THEN
    ROLLBACK;  -- দুটো UPDATE এর কোনোটাই হবে না
    RAISE EXCEPTION 'Insufficient balance';
END IF;

-- Step 3: Receiver এ টাকা যোগ করুন
UPDATE accounts SET balance = balance + 5000
WHERE account_id = 202;

-- Step 4: Audit log রাখুন
INSERT INTO transfer_log (from_id, to_id, amount, created_at)
VALUES (101, 202, 5000, NOW());

COMMIT;  -- সব ঠিক থাকলে permanent save — Durability!
004Indexing

Indexing — Database এর Superpower

Index হলো book এর index page এর মতো। পুরো book না পড়ে সরাসরি page এ যানয়া যায়। Database এ index না থাকলে প্রতিটি query তে পুরো table scan করতে হয় — ১০ লক্ষ row এ এটা অনেক slow।

❌ WITHOUT INDEXO(n) — Full Table ScanRow 1: a@x.com — check... ✗Row 2: b@x.com — check... ✗Row 3: c@x.com — check... ✗· · · 1,000,000 rows · · ·~1 second 😱✅ WITH B-TREE INDEXO(log n) — Binary SearchB-Tree Roota-m branchn-z branchabc@mail ✓ FOUND~1ms ⚡ (1000x faster!)

✅ Index দিন যখন

WHERE clause এ frequently query হয়। High cardinality column (email, phone)। Foreign key columns। Read-heavy workload।

❌ Index এড়াও যখন

Write-heavy table (INSERT/UPDATE slow হয়)। ১০০০ row এর কম small table। Low cardinality (true/false, status)। Rarely queried columns।

indexing-examples.sql
-- 1. Basic index — single column
CREATE INDEX idx_users_email ON users(email);

-- 2. Composite index — দুটো column একসাথে filter করলেন
--    WHERE email = ? AND created_at > ?
CREATE INDEX idx_users_email_date ON users(email, created_at);

-- 3. Partial index — শুধু active users (ছোট এবং fast)
CREATE INDEX idx_active_users ON users(email) WHERE status = 'active';

-- 4. Query plan দেখুন — index use হচ্ছে কি?
EXPLAIN ANALYZE
SELECT * FROM users WHERE email = 'test@example.com';
-- "Index Scan" → ভালো ✅  |  "Seq Scan" → index নেই ⚠️
005Database Scaling

Sharding এবং Replication

Read Replica — Read Load কমাও

সব database operation এর ৮০-৯০% হলো READ। Primary database এর copy (Replica) রাখুন — Read requests Replica তে পাঠাও। Primary শুধু Write handle করবেন। এভাবে Primary এর load অনেক কমে।

AppServerWRITEREADPRIMARYWrite hereREPLICA 1Read onlyREPLICA 2 (Read)async replication→ Replica 1 synced→ Replica 2 synced

Sharding — Write Load ভাগ করুন

একটা বড় table কে multiple databases এ ভাগ করা। User ID 1-1M → DB1, 1M-2M → DB2। প্রতিটি shard আলাদাভাবে serve করতে পারে।

Approachকীভাবে কাজ করেকখন ব্যবহার করুন
Read ReplicaPrimary → Replica async sync। Read → ReplicaRead-heavy app (80%+ read)
Vertical Shardingআলাদা table → আলাদা DB (users DB, orders DB)Microservices separation
Horizontal Shardingএকই table rows ভাগ করে multiple DB তেBillions of rows, write scale

⚠️ Sharding এর সমস্যা

Cross-shard JOIN কঠিন — User A Shard 1 এ, User B Shard 3 এ, এদের data JOIN করতে application layer এ করতে হয়। Resharding complex। Hotspot shard হতে পারে। তাই Sharding সবচেয়ে শেষে করুন — আগে Read Replica, Caching, Query optimization দিয়ে দেখুন।

006Practical Implementation

DB Pool & Caching Pattern

Backend এ database handle করার সময় connection pool এবং caching strategy (Cache-aside) ব্যবহার করা industry standard। Connection বারবার create করা অনেক expensive, তাই pool ব্যবহার করা হয়।

db-pool-cache.js
const { Pool } = require('pg');
const redis = require('redis');

// Connection Pool — connection বারবার create করা expensive!
const primaryDB = new Pool({ host: 'primary-db', max: 20 });
const replicaDB = new Pool({ host: 'replica-db', max: 40 });
const cache = redis.createClient();

// Cache-aside Pattern — সবচেয়ে common caching strategy
async function getUser(userId) {
  const cacheKey = `user:${userId}`;

  // 1. Cache check করুন
  const cached = await cache.get(cacheKey);
  if (cached) return JSON.parse(cached);  // Cache HIT ✅

  // 2. Cache miss — Replica থেকে read (Primary এ load দিও না)
  const result = await replicaDB.query(
    'SELECT * FROM users WHERE id = $1', [userId]
  );
  const user = result.rows[0];

  // 3. Cache এ store করুন (TTL: 1 hour)
  await cache.setEx(cacheKey, 3600, JSON.stringify(user));

  return user;
}

// Write — Primary তে
async function updateUser(userId, data) {
  await primaryDB.query('UPDATE users SET name=$1 WHERE id=$2', [data.name, userId]);
  await cache.del(`user:${userId}`);  // Cache invalidate করুন!
}
007Tools Comparison

Database Tools — কোনটা কখন ব্যবহার করবেন

DatabaseTypeACIDScaleBest For
PostgreSQLSQL✅ FullVertical + Read ReplicaComplex queries, JSON, GIS
MySQLSQL✅ FullMaster-slave replicationWeb apps, E-commerce
MongoDBDocument NoSQL⚠️ PartialHorizontal shardingFlexible schema, JSON docs
RedisKey-Value NoSQL❌ NoCluster modeCaching, Sessions, Pub/Sub
CassandraColumn NoSQL⚠️ EventualLinear horizontalTime-series, IoT, High write
008Real World Examples

বড় কোম্পানিগুলো কীভাবে করেছেনে

📱 Instagram এর Database Journey

Instagram শুরু করেছেনিল PostgreSQL দিয়ে। User base ১০M থেকে ১B+ হওয়ার সময় PostgreSQL sharding করে। User ID based consistent hashing দিয়ে ১০০০+ shards। Photos (media files) আলাদাভাবে S3 (object storage) তে রাখে — database তে শুধু metadata। Django ORM এর উপরে custom sharding layer।

🏦 bKash / Banking — ACID Critical

Financial systems এ SQL database অপরিহার্য। Transaction isolation level "SERIALIZABLE" রাখতে হয়। Double-spending prevent করতে database-level locks। Audit log সব transactions এর। Strong consistency mandatory — কোনো eventual consistency চলবে না।

Polyglot Persistence

Real production systems এ এক ধরনের database দিয়ে সব হয় না। Instagram: PostgreSQL (users/follow) + Cassandra (activity feed) + Redis (session/cache) + S3 (photos)। প্রতিটির আলাদা strength কে কাজে লাগানো হয়। এটাই Polyglot Persistence।
009Lesson Summary

SUMMARY — আজকে যা শিখলাম

Conceptএক লাইনে
SQL vs NoSQLSQL = ACID, structured। NoSQL = flexible, scale। Use case দেখে choose করুন
ACIDAtomicity (all/nothing), Consistency, Isolation, Durability
IndexB-Tree দিয়ে O(n) → O(log n)। Read fast, Write slow হয়
Read ReplicaRead → Replica, Write → Primary। Read load কমে
ShardingTable rows ভাগ করুন। Write scale করুন। Cross-shard JOIN কঠিন
N+1 ProblemLoop এ query করুন না। JOIN বা eager loading ব্যবহার করুন
010Knowledge Check
011Assignments
012Practical Lab