🔥 0
0
Lesson 5 of 10 22 min +175 XP

Database Design & Scaling

Your application is growing. Reads are slow. Writes are backing up. Your single database can't keep up. Time to scale.

SQL vs NoSQL: The Trade-offs

SQL (PostgreSQL, MySQL)

  • ACID transactions
  • Complex JOINs and queries
  • Fixed schema
  • Vertical scaling primarily
  • Strong consistency

NoSQL (MongoDB, Cassandra)

  • Flexible schema
  • Horizontal scaling built-in
  • High throughput
  • Eventual consistency (often)
  • No complex JOINs

When to Use What

Use Case Choice Why
Banking / Finance SQL ACID transactions critical
Social media posts NoSQL High volume, flexible schema
E-commerce inventory SQL Strong consistency needed
IoT sensor data NoSQL (time-series) High write throughput

Replication: Scaling Reads

Create copies of your database. Writes go to the primary (master), reads go to replicas.

Database replication: Primary with writes replicating to multiple replicas
Benefits:
  • Scale reads horizontally
  • Fault tolerance (promote replica if primary fails)
  • Geographic distribution
Limitation: Doesn't help with write scaling - all writes still go to one server.

Sharding: Scaling Writes

Split your data across multiple databases. Each shard holds a subset of the data.

Database sharding: data partitioned across multiple shards by user range

Shard Key Selection

The shard key determines which shard stores which data.

# Simple approach: modulo
shard_id = hash(user_id) % num_shards
Good shard keys:
  • user_id (for user-centric data)
  • geographic region
  • time-based (for time-series)
Bad shard keys:
  • Anything that creates hotspots (e.g., celebrity user IDs)

Consistent Hashing

The problem with key % num_shards:

Before: 3 shards → key 10 → shard 1 (10 % 3 = 1)
After:  4 shards → key 10 → shard 2 (10 % 4 = 2)

Adding one shard remaps almost all keys. Massive data migration!

Consistent hashing solves this by only remapping ~1/N keys when adding/removing a shard.

Real-World: Uber's Schemaless

In 2014, Uber was running out of database space due to trip growth. They built Schemaless:

Uber Schemaless
  • Key-value store built on sharded MySQL
  • Append-only writes with buffered updates
  • Migrated from Python to Go: 85% latency reduction

Uber Engineering Blog

Real-World: Twitter's Vitess Adoption

Twitter scaled their users database with Vitess:

Twitter Users DB

Migrated from Gizzard (old MySQL framework) to Vitess to handle strict SLOs on QPS, latency, success rates, and cross data center consistency.

Twitter Engineering Blog

Key Takeaways

  • SQL for ACID, NoSQL for scale and flexibility
  • Replication scales reads but not writes
  • Sharding scales both reads and writes by partitioning data
  • Consistent hashing minimizes data migration when scaling

Next up: CAP Theorem - Understanding the fundamental trade-offs in distributed systems.

🧠 Quick Quiz

Test your understanding of this lesson.

1

When is NoSQL a better choice than SQL?

2

What is database sharding?

3

Why does naive modulo sharding fail when adding servers?

Caching Strategies