Sub topic 5
Know your building blocks
This chapter assumes you have read the earlier chapters on requirements and capacity estimation. It also assumes you know how HTTP APIs and databases work. Here you will learn the standard components that most designs are assembled from. You will also learn what problem each one solves and when to leave it out.
Why a vocabulary of components helps
Most large systems combine a small set of parts in different arrangements. If you know what each part does, what it costs, and how it fails, you can pick parts to match your requirements. Without that, you will tend to draw every box you have heard of.
Each component should answer a specific need from your requirements or estimates. If you cannot name the need, leave the component out.
The core components
| Component | Problem it solves | Main cost or risk |
|---|---|---|
| Load balancer | Spreads requests across servers and removes failed servers from rotation | Can itself become a single point of failure |
| Application server | Runs business logic, ideally with no local state | Local state makes scaling out hard |
| Relational database | Structured data, transactions, joins | Harder to scale writes horizontally |
| Key-value or document store | Simple lookups by key at high volume | Limited querying, weaker multi-record guarantees |
| Cache | Serves repeated reads quickly from memory | Stale data and invalidation bugs |
| Message queue | Decouples producers from consumers and absorbs bursts | Delivery semantics, ordering, retries |
| Object storage | Cheap, durable storage for large files | High latency, no in-place edits |
| CDN | Serves static content from locations near users | Cache invalidation, cost for rarely read content |
| Search index | Full-text and ranked queries | Usually eventually consistent with the primary data |
How the pieces fit together
A typical read-heavy design places stateless application servers behind a load balancer. The servers check a cache first and fall back to the database. Slow or bursty work goes onto a queue, and workers process it later.
Example: the cache-aside pattern
The most common way to use a cache is cache-aside. The application checks the cache, and on a miss it reads the database and stores the result. This sketch uses plain dictionaries as stand-ins.
cache = {}
db = {"item:1": "hello"}
def get_item(key):
if key in cache:
return cache[key], "hit"
value = db.get(key)
if value is not None:
cache[key] = value
return value, "miss"
print(get_item("item:1"))
print(get_item("item:1"))The first call prints a miss and the second prints a hit. In a real system you must also decide how long entries live and what happens when the underlying record changes.
Choosing a component
Tie each choice to a requirement or a number from your estimates.
- If read QPS is far above write QPS, consider a cache or read replicas.
- If users upload large files, store them in object storage and keep only metadata in the database.
- If a request triggers slow work such as sending email, put it on a queue and respond right away.
- If users need keyword search, add a search index, and accept that it lags behind the database.
- If users are spread across regions and content is static, add a CDN.
Common errors
- Adding a cache before measuring. A cache adds a consistency problem, so it needs a read-heavy workload to justify it.
- Treating a queue as free. Consumers must handle duplicate and out-of-order messages.
- Forgetting that the load balancer, cache, and queue can also fail. Say how you would run more than one of each.
- Storing session state on application servers, which prevents adding or removing servers freely.
- Using a search index as the source of truth instead of a derived copy.
Exercise
A photo-sharing service lets users upload 5 MB images and then view them many times. Which two building blocks would you add for the image data, and what does each do?
Show answer
Object storage holds the image files cheaply and durably, while the database keeps only metadata such as owner and file location. A CDN serves the frequently viewed images from locations near users, which reduces latency and load on the origin.