SQL: what you actually get
PostgreSQL, MySQL: tables with a fixed schema, and three superpowers:
- ACID transactions: transfer money = debit + credit; both happen or neither. Atomicity, Consistency, Isolation, Durability. Non-negotiable for payments, bookings, inventory.
- Joins + ad-hoc queries: ask new questions of old data without redesigning it.
- 50 years of maturity: tooling, replication, backups, edge cases already solved.
The costs: horizontal scaling (sharding) is manual, and schema changes on huge tables are operational events.
NoSQL is four different databases wearing a trenchcoat
- Key-value (Redis, DynamoDB): get/put by key, single-digit-ms at any scale. Sessions, carts, rate-limit counters, caching. No joins, no rich queries: you design around the key.
- Document (MongoDB): JSON documents, flexible schema, secondary indexes. Product catalogs, user profiles, content with varying shapes. Related data is embedded in the document instead of joined.
- Wide-column (Cassandra, HBase, Bigtable): rows partitioned across nodes, tuned for massive write volume and time-series: chat messages, IoT sensor data, event logs. Multi-datacenter replication built in; queries must match the partition key design.
- Graph (Neo4j): relationships are first-class: friends-of-friends, fraud rings, recommendations are one traversal instead of 14 joins.
The decision framework (use it out loud in interviews)
- Data shape: highly relational with transactions (orders, payments) = SQL. Self-contained flexible records = document. Pure key lookups = key-value. Time-series/firehose writes = wide-column. The queries ARE relationships = graph.
- Scale: sustained millions of writes/sec or multi-region writes = Cassandra/DynamoDB. Normal web app = Postgres, which scales much further than people think (tens of thousands of reads/sec with replicas).
- Transactions: money and inventory = ACID SQL. Counters, likes, presence = NoSQL is fine.
Polyglot persistence: the real-world answer
One production e-commerce system might run: Postgres for orders and payments (ACID), Redis for sessions + cart + hot cache, Elasticsearch for product search (it is a search engine, not a primary store), Cassandra or Kafka for the event/clickstream firehose, S3 for images. "Which database is best?" is the wrong question; "which is best for THIS data and access pattern?" is the job.
Worked example: chat app storage
Messages: write-heavy, time-ordered, read by conversation, billions of rows: wide-column (Cassandra), partition key = conversation_id, clustering by timestamp. User profiles and contacts: Postgres. Online presence: Redis with TTL heartbeats. Media: S3 + CDN. Four databases, each chosen by access pattern.
Common interview traps
- MongoDB "because it scales" for clearly relational, transactional data.
- "NoSQL can't do transactions" (many now offer scoped ones) or "SQL can't scale" (read replicas go very far).
- Picking by hype instead of access pattern.
Interview Q&A
Q: When would you pick Cassandra over Postgres?
A: When the workload is write-dominated time-series at a scale a single primary cannot absorb (hundreds of thousands of writes/sec), multi-region writes are required, and queries are known in advance and partition-key friendly. Chat messages and sensor data are the textbook fits.
Q: Why not just use Elasticsearch as the main database?
A: It is a search index: near-real-time refresh, weaker durability and transactional guarantees, expensive as a source of truth. Pattern: source of truth in Postgres, search index in Elasticsearch, synced via a pipeline.
SQL: asal mein kya milta hai
PostgreSQL, MySQL: fixed schema wali tables, aur teen superpowers:
- ACID transactions: paise transfer = debit + credit; dono honge ya koi nahi. Atomicity, Consistency, Isolation, Durability. Payments, bookings, inventory ke liye non-negotiable.
- Joins + ad-hoc queries: purane data se naye sawaal poochho, redesign kiye bina.
- 50 saal ki maturity: tooling, replication, backups, edge cases sab solved.
Keemat: horizontal scaling (sharding) manual hai, aur badi tables par schema changes operational events hote hain.
NoSQL ek nahi, chaar databases hain
- Key-value (Redis, DynamoDB): key se get/put, kisi bhi scale par single-digit-ms. Sessions, carts, rate-limit counters, caching. Joins nahi, rich queries nahi: design key ke around karna padta hai.
- Document (MongoDB): JSON documents, flexible schema, secondary indexes. Product catalogs, user profiles, alag-alag shape ka content. Related data join ki jagah document ke andar embed hota hai.
- Wide-column (Cassandra, HBase, Bigtable): rows nodes par partitioned, massive write volume aur time-series ke liye tuned: chat messages, IoT sensor data, event logs. Multi-datacenter replication built-in; queries partition key design se match karni chahiye.
- Graph (Neo4j): relationships first-class hain: friends-of-friends, fraud rings, recommendations ek traversal hain, 14 joins nahi.
Decision framework (interview mein zor se bolna)
- Data shape: highly relational + transactions (orders, payments) = SQL. Self-contained flexible records = document. Pure key lookups = key-value. Time-series/firehose writes = wide-column. Queries hi relationships hain = graph.
- Scale: sustained millions writes/sec ya multi-region writes = Cassandra/DynamoDB. Normal web app = Postgres, jo log sochte hain usse kahin zyada scale karta hai (replicas ke saath tens of thousands reads/sec).
- Transactions: paise aur inventory = ACID SQL. Counters, likes, presence = NoSQL chalega.
Polyglot persistence: asli duniya ka jawab
Ek production e-commerce system mein ho sakta hai: orders aur payments ke liye Postgres (ACID), sessions + cart + hot cache ke liye Redis, product search ke liye Elasticsearch (ye search engine hai, primary store nahi), event/clickstream firehose ke liye Cassandra ya Kafka, images ke liye S3. "Best database kaunsa?" galat sawaal hai; "IS data aur access pattern ke liye best kaunsa?" yehi kaam hai.
Worked example: chat app storage
Messages: write-heavy, time-ordered, conversation se read, billions rows: wide-column (Cassandra), partition key = conversation_id, timestamp se clustering. User profiles aur contacts: Postgres. Online presence: Redis, TTL heartbeats ke saath. Media: S3 + CDN. Chaar databases, har ek access pattern se chuni hui.
Common interview traps
- Saaf relational, transactional data ke liye MongoDB "kyunki scale karta hai".
- "NoSQL transactions nahi kar sakta" (kayii ab scoped transactions dete hain) ya "SQL scale nahi karta" (read replicas bahut door jaate hain).
- Access pattern ki jagah hype se choose karna.
Interview Q&A
Q: Postgres ki jagah Cassandra kab?
A: Jab workload write-dominated time-series ho aise scale par jo ek primary absorb nahi kar sakta (lakhon writes/sec), multi-region writes chahiye, aur queries pehle se pata hon aur partition-key friendly hon. Chat messages aur sensor data textbook fit hain.
Q: Elasticsearch ko hi main database kyun nahi bana dete?
A: Ye search index hai: near-real-time refresh, kamzor durability aur transactional guarantees, source of truth ke liye mehenga. Pattern: source of truth Postgres mein, search index Elasticsearch mein, pipeline se sync.