Sub topic 2
Know Your Building Blocks
Assume you understand client-server architecture, HTTP APIs, databases, and basic Python. This chapter connects those pieces into a system design.
Know your building blocks
A typical service combines a few standard components:
- A client sends requests.
- A load balancer distributes requests across application servers.
- Application servers validate requests and apply business rules.
- A cache stores frequently requested data in fast memory.
- A database provides durable storage.
- A message queue holds work that can happen asynchronously.
- Object storage holds large files.
- Monitoring records metrics, logs, and traces.
Choose a component because it satisfies a requirement. Do not add it just because it appears in familiar diagrams.
Sketch the API and the data model
Start with the actions clients need. An API is the interface through which clients call a service. The API reveals access patterns, which strongly influence the data model.
POST /v1/items
Content-Type: application/json
{"owner_id": "user:42", "body": "hello"}
GET /v1/items/item:99
GET /v1/users/user:42/items?limit=20&cursor=abc123The data model should support these queries directly. An items record might contain item_id, owner_id, body, and created_at. An index on (owner_id, created_at) supports a user's newest items without scanning the entire table.
Cursor-based pagination returns a position for the next page. It is usually more stable than page numbers when records are added during browsing.
Draw the high-level architecture
Connect each request path to the components it uses:
Keep synchronous work on the request path small. Send email, indexing, or analytics to workers when the client does not need an immediate result.
Choose storage that fits the access pattern
A relational database fits structured records, transactions, and queries involving relationships. A key-value or document store fits simple lookups at very high scale when the access patterns are known. Object storage fits large blobs such as images.
Storage choice depends on query shape, consistency needs, record size, and operational limits. Do not choose based only on expected request volume.
Scale with replication, partitioning, and caching
Replication keeps copies of data on multiple nodes. It improves read capacity and availability, but replicas can be temporarily stale.
Partitioning, or sharding, splits data across nodes. A good partition key spreads traffic evenly and keeps common queries within one partition. A poor key creates a hot partition.
Caching reduces database reads. Define expiration and invalidation rules, and decide what stale data the product can tolerate.
Exercise
Which design choice should come first: selecting a database or defining the API's main read and write patterns?
Show answer
Define the access patterns first. They provide the evidence for choosing storage and indexes.