Chapter 3: Data Architecture Deep Dive

This is often the “make or break” section of the interview. A generic “I’ll use a database” answer is a guaranteed fail. You must define which database and why.

SQL vs. NoSQL Decision Matrix

No single database fits every use case. Use this matrix to guide your decision.

Feature Relational (SQL) Non-Relational (NoSQL)
Structure Rigid Schema (Tables, Rows, Columns, Foreign Keys). Flexible Schema (Documents, Key-Value, Graphs).
Scaling Vertical scaling (bigger machine) is easiest. Horizontal scaling (sharding) is complex and often manual. Horizontal scaling (sharding) is often built-in and seamless across commodity hardware.
Consistency ACID (Strong Consistency). Crucial for transactions. BASE (Eventual Consistency) mostly. Prioritizes availability.
Querying Powerful SQL (JOINs, Aggregations). Varies (JSON-based, simple lookups). JOINs often handled in app code.
Best For Financials, Inventory, Structured Data, Complex Reporting. Massive Scale, Unstructured inputs, Rapid prototyping, Real-time feeds.

⚔️ PostgreSQL vs. MongoDB

PostgreSQL (The Reliable Workhorse)

MongoDB (The Flexible Scaler)

🐘 Cassandra & Wide-Column Stores

The “Discord” Example Cassandra is designed for Writes. Massive, relentless streams of writes (e.g., Logs, Chat history, IoT sensor data).

Case Study: Discord’s Migration

🧱 Interview Q&A

Interviewer: “Design the data layer for a user profile system where fields change often (social links, bio, themes).”

Candidate: "For a user profile with a flexible schema, I would choose a Document Store (NoSQL) like MongoDB or DynamoDB.

Interviewer: “We are building a stock trading engine. We need to handle millions of transactions per second with zero data loss. Speed is key.”

Candidate: "Be careful with the word ‘Speed’. For a trading engine:

  1. Order Matching: This needs extreme low latency. I would use an In-Memory structure (like a custom engine or Redis) for the order book.
  2. Ledger (Ownership): This needs strict ACID Consistency. You cannot have ‘eventual consistency’ in valid ownership. I would use a specialized high-performance SQL layer (like PostgreSQL or TimescaleDB for tick data) or a dedicated ledger database (like TigerBeetle) that prioritizes consistency over availability (CP system).
  3. I would never use MongoDB or Cassandra for the core ledger due to their default eventual consistency models."

Interviewer: “We need to store ‘Who follows whom’ for 1 Billion users. We need to find ‘Friends of Friends of Friends’ efficiently.”

Candidate: "This is a classic Graph Traversal problem.

Interviewer: “We deploy 10,000 IoT sensors. Each sends temperature every second. We need to graph the average temperature per minute.”

Candidate: "This is a Time-Series problem.