System Design vs. Algorithms

[!NOTE] This module explores the core principles of System Design vs. Algorithms, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. The Hook: Two Different Brains

Algorithms (LeetCode):

  • Input fits in RAM.
  • Single Thread.
  • Answer is Correct or Incorrect.
  • Goal: Minimize Big O.

System Design:

  • Input is Petabytes.
  • Thousands of servers.
  • No “Correct” answer, only Trade-offs.
  • Goal: Availability, Reliability, Scalability.

2. The Intersection

Where do they meet? At the Node Level. A Distributed System is just a graph of nodes where every node runs an Algorithm.

  • Databases run B-Trees (Search).
  • Load Balancers run Consistent Hashing.
  • Network Routers run Dijkstra (Shortest Path).

The Interview Trap: Applying Algorithm thinking to System Design.

  • “I will use a HashMap to store all users.”
  • Interviewer: “We have 1 billion users. That doesn’t fit in RAM.”
  • Solution: Sharding + Distributed Key-Value Store (Dynamo/Cassandra).

3. Scale Shift Table

Scale Tool Pattern
Small (N < 100k) In-Memory (ArrayList, HashMap) Iterate, Filter. Simple Code.
Medium (N < 10M) SQL Database (Index, B-Tree) Joins, ACID Transactions.
Large (N > 100M) NoSQL, Sharding, Caching Eventual Consistency, MapReduce.

4. Deep Dive Strategy Lab: The Mindset Shift

Intuition Through Analogy

Building a Shed vs. Building a Skyscraper.

  • Shed: You can hold the plan in your head. If you make a mistake, you cut a new wood board. (Algorithm).
  • Skyscraper: You need blueprints, cranes, and logistics. If the foundation cracks, the building falls. You assume wind (failures) will happen. (System Design).

Mathematical Anchor: The CAP Theorem

You cannot have it all. In a distributed system with partitions (P), you must choose:

  • Consistency (C): Everyone sees the same data at same time. (Fail if network breaks).
  • Availability (A): Everyone can write/read always. (Might see old data). Algorithms (local) usually assume C and A are free. Systems folks know they are expensive.