0% completed
Thinking in Patterns
On This Page
What a Pattern Is
A Technology Is Not a Pattern
Every Pattern Has a Cost
Patterns Interact
The Three Habits
Patterns in a Design Interview
Check Yourself
The store's product page has become slow. Every time a buyer opens a product, the page reads its name, price, and stock from the store database. At the busiest hour, that is 8,000 reads per second, and the database can serve about 5,000. The extra queries wait in a queue, so a page that used to load at once now takes seconds.
Two engineers suggest a fix. The first says, "Add Redis." The second says, "Use cache-aside. The page checks a copy of the product in memory first, and it reads the database only when the copy is missing. A copy can be up to 60 seconds old, so a new price can take a minute to appear."
Both engineers might build the same thing. However, only the second answer can be checked. It names the problem (too many reads), the solution (a copy that the page checks first), and the cost (a price that can be a minute old). The first answer names only a product, Redis, which is a program that keeps data in memory.
This lesson explains what a pattern is, how a pattern differs from a technology, why every pattern has a cost, and why the hardest decisions happen where patterns interact. It ends with three habits that the rest of the course uses.
What a Pattern Is
A system design pattern is a known solution to a problem that appears in many systems, described together with what it costs. The word "pattern" fits because the problem repeats. Thousands of systems have a database that receives more reads than it can serve, and they solve it in the same few ways.
Every pattern has three parts:
- Problem: the situation, with numbers when possible. At the store, 8,000 reads per second reach a database that serves 5,000.
- Solution: the mechanism, described well enough that someone could build it. The page checks a copy in memory first. On a miss, it reads the database and saves a copy that expires after 60 seconds.
- Cost: what the solution makes worse. A buyer can see a price up to 60 seconds old, and the team runs one more system.
Compare the two panels. The left panel, "Add Redis," fills one row with the name of a product and leaves the problem and the cost empty. The right panel fills all three rows with the store's own numbers, so a reviewer can check each one.
A complete answer also gives the team a clear question to decide. Are prices that are 60 seconds old acceptable on a product page? That is a business question, and the people who own pricing can answer it. "Add Redis" gives the team no such question to decide.
The second engineer's pattern has a name, cache-aside. A cache is a fast, temporary copy of data that is read often. "Aside" means the cache sits beside the database, not in front of it, and the application itself decides when to read each one. The Cache-Aside lesson builds this pattern in full.
A Technology Is Not a Pattern
Redis is a technology. It stores keys and values in memory. A team can use Redis for cache-aside. It can also use Redis as a list of jobs waiting for workers, as a lock that only one server holds at a time, or as a store for login sessions. Those are four patterns with four different costs, so "add Redis" does not say which one the engineer means.
The reverse is also true, because cache-aside does not need Redis. The same pattern works with Memcached, with a copy kept in each server's own memory, or with a cache service from a cloud provider. The problem, the solution, and the cost stay the same in every case.
This is why the course teaches patterns, not products. Products change their names, prices, and features over time, but the problem of too many reads does not change. A design that names only technologies cannot be reviewed, because nobody can see what each part solves or what it costs. Name the pattern first, and then choose a technology for it.
Every Pattern Has a Cost
No pattern is free. The cost is why a pattern is a choice and not a rule. A system that does not have the problem pays the cost and gets nothing back.
Cache-aside shows three common kinds of cost:
| Kind of cost | Cache-aside at the store |
|---|---|
| <span style="white-space:nowrap">Correctness</span> | A buyer can see a price up to 60 seconds old |
| Speed on a miss | A miss reads the cache, then the database, then saves a copy, so it is slower than having no cache |
| Operations | One more system to run, watch, and size |
The cost decides where the pattern fits. A product description changes rarely, so a copy that is a minute old is harmless. A price that is a minute old matters only if the buyer pays it. Therefore, checkout reads the price from the database again before it charges. An old copy can then change what the page shows, but never what the buyer pays.
Because every pattern has a cost, a team can also choose to wait. This module adds no pattern to the store, so checkout keeps calling inventory (30 ms), payment (150 ms), the orders database (20 ms), and fulfillment (50 ms), one after another, for 250 ms in total. The store adds a pattern only when the numbers show the problem that the pattern solves.
Patterns Interact
A real design uses many patterns together, and the way they connect matters as much as each pattern alone. Two patterns that work well alone can create a new problem when they are used together. Designing a system is therefore choosing patterns and connecting them correctly.
Here is an example at the store. A buyer named Maya places order 1042 for $40, and checkout calls payment, which normally answers in 150 ms. Suppose the team gives the call a time limit of 1 second (a timeout), so a payment service that stops answering cannot keep checkout waiting forever. The team also tries the call once more after a timeout (a retry), because many failures last only a moment. Each pattern is reasonable alone.
Together, they create a new problem. Suppose payment charges Maya's card and then its answer is lost on the network. After 1 second, checkout stops waiting and retries. Payment receives a second request to charge 40 for order 1042, and it charges the card again. Maya pays 80 for a $40 order.
Read each lane from top to bottom. Both lanes start the same way: the first charge succeeds, its answer is lost, and checkout retries after 1 second. In the top lane, payment treats the retry as a new charge, and Maya pays 80. In the bottom lane, the retry carries key 1042, so payment returns the first result, and Maya pays 40.
The fix is a third pattern. An operation is idempotent when doing it twice has the same effect as doing it once.
Checkout sends a unique ID with every charge, called an idempotency key, and the order ID, 1042, serves as that key. Payment records every key it has processed. When a request arrives with a key it has seen, payment returns the first result instead of charging again. The Idempotency lesson builds this check.
The important decision was neither "add a timeout" nor "add a retry." It was what happens when a retry is added to a call that moves money. The same thing happens throughout the course:
- Retries need idempotency, or a retry can repeat a charge.
- Caches need stampede control. When the 60-second copy of a popular product expires, hundreds of requests can miss at the same moment and all read the database (a cache stampede). The Cache Stampede Prevention lesson covers this.
- Sagas need compensation. A saga runs one task, like an order, as several steps in different services. When a later step fails after the charge, the store must refund the money. A step that undoes an earlier step is called compensation, and the Saga lesson covers it.
Key decisions happen where patterns interact. Each time you add a pattern, ask what it changes for the patterns already in the design.
The Three Habits
Three habits turn the ideas above into a method. Every later lesson uses them, and each one comes with a tool that supports it.
Each card in the diagram shows the habit, one sentence from this lesson that follows it, and the tool that supports it.
- Numbers before boxes. Before you draw a part, state the numbers that create its problem, like requests per second, data size, and reads per write. "8,000 reads per second against 5,000" is the reason cache-aside appears at all. The Back-of-the-Envelope Math lesson shows how to do these calculations.
- Every pattern pays its price aloud. State the cost in the same sentence as the pattern: "cache-aside, with prices up to 60 seconds old." This is the name-the-cost rule. Every pattern lesson in this course names its pattern's cost, so you always have the words.
- Question four wins interviews. The Five Questions lesson gives a fixed list of questions for any design, and the fourth is "What happens when a part fails?" Most designs work when every part works. A design is judged by what the user sees when one part is slow or down. In the Maya example, the double charge happened only when the network lost an answer.
Patterns in a Design Interview
In a design interview, the interviewer sees only what you say and draw. Pattern names make your explanation short, because "cache-aside" says in two words what would otherwise take a paragraph. However, a name without its problem and cost is no better than the first engineer's "Add Redis."
Each time you add a part, say one sentence with four pieces: the number, the pattern, the cost, and the interaction. For the product page, it sounds like this: "The page sends 8,000 reads per second to a database that serves 5,000, so I add cache-aside with a 60-second lifetime. Prices on the page can be a minute old, so checkout reads the price again before it charges. When a popular product's copy expires, many requests miss together, so I add stampede control."
Expect the interviewer to ask about the cost. If you have already named it, the follow-up becomes a discussion of a choice you made. If you have not, the interviewer has to ask for it, and the design looks unfinished.
Knowing the patterns has one limit. It does not tell you where to start on a blank whiteboard, or which problem to look for first. The next lesson, The Five Questions, gives an order to follow.
Check Yourself
Q1. Five teams want to know about every new order: fulfillment, email, analytics, fraud, and loyalty. A teammate proposes, "Put Kafka in." Rewrite the proposal as a pattern with a problem, a solution, and a cost. Then name one interaction with another pattern.
<details> <summary>Show answer</summary>Problem: at 200 orders per second, calling five teams from checkout adds 5 × 200 = 1,000 calls per second. Each call also adds waiting time and one more way for checkout to fail.
Solution: publish-subscribe. Checkout publishes one order.placed event to a broker, a server that receives events and passes them on. Each of the five teams subscribes and receives its own copy. Kafka is one broker that could carry the events, and that technology choice comes last.
Cost: the store runs one more system. The teams learn about each order a moment later instead of during checkout, and the broker can deliver an event more than once.
Interaction: because an event can arrive twice, each subscriber must skip events it has already processed. Otherwise loyalty could award Maya's points twice for order 1042. The Publish-Subscribe lesson builds this design.
</details>Q2. A small shop's product page sends 500 reads per second to a database that serves 5,000. An engineer proposes cache-aside with a 60-second copy, "because caching is a best practice." What is wrong with the proposal?
<details> <summary>Show answer</summary>The shop does not have the problem that cache-aside solves. Its database uses 10 percent of its capacity, so a cache removes no real risk. However, the shop would pay the full cost: prices up to 60 seconds old, slower reads on every miss, and one more system to run. A pattern is a choice made from numbers, not a rule, so the right answer here is to wait.
</details>Q3 (incident). Suppose the store adds the 1-second timeout and the single retry from this lesson to the payment call, with no idempotency key. During a 3-minute network problem between checkout and payment, 120 buyers are charged twice. Checkout's logs show one retry for each of those orders. What happened, and what should change?
<details> <summary>Show answer</summary>For each of the 120 orders, payment charged the card, and its answer was lost on the network. After 1 second, checkout retried, and payment treated the retry as a new charge. Each pattern worked as designed, and the defect appeared when the retry was added to a call that moves money.
The fix is an idempotency key: checkout sends the order ID with every charge, and payment returns the first result when it sees a key again. The team should also refund the 120 second charges. Naming the retry's cost aloud, "a retry can repeat the call," would have shown the problem before it reached a buyer.
</details>💡 In the interview: Name each pattern with its problem and its cost in one sentence. A strong answer sounds like this: "The page sends 8,000 reads per second to a database that serves 5,000, so I add cache-aside with a 60-second copy. Prices can be a minute old, so checkout reads the price again before it charges." Expect the follow-up "Why not put a cache in front of everything?" Answer with the cost: every copy can be out of date, every miss is slower, and a part without a read problem gains nothing.
Key takeaway: A pattern is a problem, a solution, and a cost, and a technology like Redis is only one way to build it. "Cache-aside, with a copy up to 60 seconds old, for 8,000 reads per second against 5,000" can be reviewed, but "add Redis" cannot. The hardest decisions happen where patterns interact, like a retry of Maya's $40 charge that needs idempotency key 1042. Use three habits in every design: numbers before boxes, every pattern pays its price aloud, and question four wins interviews.
Seif Elbayomi
· 4 months ago
A handy sheet of all cards in the course would be really helpful or at least a page collecting all of them
Reading Progress
0%
On This Page
What a Pattern Is
A Technology Is Not a Pattern
Every Pattern Has a Cost
Patterns Interact
The Three Habits
Patterns in a Design Interview
Check Yourself