4.4.24 · HinglishDatabases

NoSQL — document (MongoDB), key-value (Redis), column (Cassandra), graph (Neo4j)

2,208 words10 min readRead in English

4.4.24 · Coding › Databases


WHAT hain ye chaar families?

Figure — NoSQL — document (MongoDB), key-value (Redis), column (Cassandra), graph (Neo4j)

HOW kaam karta hai har ek (with WHY)

1. Document — MongoDB

// Ek single document — schema per-document hai, per-table nahi
{
  "_id": ObjectId("..."),
  "name": "Asha",
  "emails": ["asha@x.com"],            // arrays allowed
  "address": { "city": "Pune", "pin": 411001 }  // nested doc
}
db.users.find({ "address.city": "Pune" })   // nested fields mein query
  • Indexing: kisi bhi field pe B-tree indexes (nested bhi). Index ke bina query ek collection scan hai.
  • Tradeoff: data aksar denormalised/duplicated hota hai → fast reads, lekin duplicated data ko update karna mushkil hota hai.

2. Key-Value — Redis

SET session:abc  "{...}"  EX 3600     # value with 1-hour TTL
GET session:abc
INCR page:views                       # atomic counter
LPUSH queue task1                     # built-in list/set/sorted-set types
  • Use cases: caching, rate-limiting, leaderboards (sorted sets), pub/sub, queues.
  • Tradeoff: limited query power (key pata honi chahiye); data mostly memory mein fit hona chahiye.

3. Wide-Column — Cassandra

CREATE TABLE sensor_data (
  sensor_id text,
  ts        timestamp,
  value     double,
  PRIMARY KEY (sensor_id, ts)   -- partition key = sensor_id, clustering key = ts
);
  • Partition key → kaunsa node; clustering key → ek partition ke andar sort order.
  • Writes ek log-structured merge (LSM) path use karti hain (commit log + memtable mein append → SSTable mein flush), isliye writes bahut fast aur append-only hoti hain.
  • Tradeoff: koi joins nahi, koi ad-hoc queries nahi — query patterns pehle se pata hone chahiye.

4. Graph — Neo4j

MATCH (a:Person {name:'Asha'})-[:FRIEND]->(b)-[:FRIEND]->(c)
RETURN c.name   // friends-of-friends do saste hops mein
  • Tradeoff: huge bulk aggregations ke liye nahi bana; machines pe shard karna mushkil hai.

Theory glue: CAP & BASE


Worked examples


Common mistakes


Flashcards

NoSQL ka full form kya hai?
"Not Only SQL" — non-relational databases ki ek family jo specific data models/scale ke liye optimised hai.
Chaar NoSQL families ke naam batao ek-ek example ke saath.
Document(MongoDB), Key-Value(Redis), Wide-Column(Cassandra), Graph(Neo4j).
Redis ~ lookups kyun achieve karta hai?
Yeh ek in-memory hash map hai jo exact keys se keyed hai.
Graph DBs mein "index-free adjacency" kya hai?
Har node apne neighbours ke direct pointers store karta hai, isliye ek hop hai graph size se independent.
Cassandra mein partition key kya decide karta hai vs clustering key?
Partition key → kaunsa node row store karta hai; clustering key → us partition ke andar sort order.
CAP theorem state karo.
Network partition ke dauran aap sirf Consistency YA Availability rakh sakte ho, dono nahi.
Strong consistency ke liye quorum condition batao.
(read aur write replica sets overlap karte hain).
ke saath, ek strong-consistency quorum do.
(kyunki ).
BASE ka full form kya hai?
Basically Available, Soft state, Eventually consistent.
Practice mein CAP mein "P" kyun assume karna padta hai?
Network partitions unavoidable hain, isliye real choice CP vs AP hai.
MongoDB SQL joins ko kab beat karta hai?
Jab aap ek nested entity fetch karo — yeh ek single document read hai bina joins ke.
Cassandra writes ke liye fast kyun hai?
LSM: commit log + memtable mein append, baad mein immutable SSTables mein flush.
Recall Feynman: 12-saal ke bachche ko samjhao

Socho apna samaan rakhne ke chaar tarike. Redis numbered lockers ki ek row hai — locker number pata hai, toh turant pakad lo, super fast, lekin number yaad rakhna hoga. MongoDB ek folder hai jahan har khilona apne complete box mein aata hai, saare chhote parts andar — ek box uthao, sab mil gaya. Cassandra ek giant warehouse hai kai workers ke saath; naye boxes workers ke beech split ho jaate hain taki kabhi overwhelmed na hon, lekin store karne se pehle boxes label karne padte hain. Neo4j ek friendship map hai: har bachche ne ek string pakdi hai har dost ki taraf, isliye "dosto ke dost dhundho" sirf strings follow karna hai — poora school search karne ki zaroorat nahi. Aur CAP: agar do warehouses ke beech phone lines band ho jaayein, to ya toh wait karo jab tak reconnect ho (sahi lekin slow) ya possibly-purani info ke saath answer do (fast lekin maybe stale). Dono ek saath nahi ho sakta.


Connections

  • SQL Relational Databases — ACID, JOINs, normalization (contrast class)
  • CAP Theorem / BASE vs ACID
  • Database Indexing (B-tree, LSM) — Mongo reads vs Cassandra writes kyun
  • Hashing and Hash Maps — key-value & partitioning ki basis
  • Sharding and Replication
  • Caching Strategies — Redis as a cache layer
  • Graph Traversal Algorithms (BFS/DFS) — Neo4j kya optimise karta hai

Concept Map

drops guarantees for

match model to

family

family

family

family

stores

gives

tradeoff

is a

gives

hashes

enables

models

optimised for

NoSQL Not Only SQL

Flexible schema and scale

Access pattern

Document MongoDB

Key-Value Redis

Wide-Column Cassandra

Graph Neo4j

Nested JSON BSON docs

One read returns entity

Denormalised duplication

In-memory hash map

Average O 1 lookup

Partition key spreads writes

Write-heavy linear scale

Nodes and edges

Fast traversals