Um momento
0x50Lesson 6 of 13

Choose and scale a database

Pick SQL or NoSQL by access pattern, then add indexes and replicas.

20 min 6-question quiz 1 code exercise
By the end of this lesson you can
  • Choose between relational and NoSQL stores based on requirements.
  • Explain what an index trades.
  • Describe leader-follower replication and replication lag.

Choosing a data store is about access patterns and guarantees, not fashion. Relational databases (PostgreSQL, MySQL) offer ACID transactions, joins, and a strict schema - a strong default. NoSQL stores trade some of that for a specific shape: key-value (Redis, DynamoDB) for lookups by key, document (MongoDB) for flexible nested records, wide-column (Cassandra) for huge write-heavy datasets partitioned by key, and graph databases for relationship traversal.

Indexes and replication

An index (usually a B-tree) lets the database find rows without scanning the whole table, and supports range queries such as “posts from the last week.” The cost: extra storage, and every write must also update each index.

In leader-follower replication, one leader accepts writes and streams them to followers, which serve reads. This scales reads and adds redundancy. With asynchronous replication, followers can lag behind, so a read from a follower may return stale data.

design.py
1leader = {"bio": "v2"}
2follower = {"bio": "v1"}  # has not applied the latest write yet
3print("leader read:", leader["bio"])
4print("follower read:", follower["bio"])
Output
leader read: v2
follower read: v1

A common fix for lag is read-your-writes consistency: for a short window after a user writes, route that user’s reads to the leader. Failover promotes a follower when the leader dies; with asynchronous replication, writes the old leader acknowledged but had not yet replicated can be lost. Multi-leader and leaderless replication accept writes in several places, at the cost of resolving conflicts.

Key takeaways

  • Choose the store by access pattern and guarantees; relational is a strong default.

  • Indexes speed reads at the cost of writes and space.

  • Read replicas scale reads but introduce lag; route recent writers to the leader.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: simulate system design building blocks

Use small Python programs to estimate capacity and simulate caches, load balancers, hash rings, and rate limiters. These exercises run locally in your browser.

Exercise 1

Route reads for read-your-writes

+25 XP

Read a window in seconds, then a count n, then n lines of user last_write_time now. Route the read to leader if the user wrote less than window seconds ago (strictly less), otherwise to follower. Print user -> leader or user -> follower for each line.

  • Mixed users
  • Just wrote
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: