Friday night at the takeaway
A takeaway's ordering site works well all week. On Friday at 7pm, 2,000 people open the menu at once. The site becomes very slow.
The code is fine. The problem is how the parts fit together. Engineers sketch those parts as boxes and arrows before they change anything.
By the end of this lesson
Note
I can sketch how the parts of a simple web app fit together.
Key words
| Word | What it means |
|---|---|
| Client | the app or browser a person uses |
| Server | the computer that runs the backend |
| Database | where data is stored for good |
| Cache | a fast, short-term copy of data that is read often |
| Queue | a waiting list of jobs to do later |
| Worker | a program that takes jobs from a queue and does them |
The cells students fill in are left blank in this preview.
Every web app has three core parts
The client is what the customer holds: a phone app or a browser. It sends requests.
The server runs the backend. It checks each request and decides the answer.
The database keeps the data: menus, orders, customers. The client never talks to the database directly. It always goes through the server.
A cache saves work, a queue delays it
The menu changes once a day but is read 2,000 times a minute. A cache keeps a copy in fast memory, so the database is not asked every time.
A receipt email is slow to send, and the customer should not wait for it. The server puts it on a queue and answers at once. A worker sends it a moment later.
Worked example: a Friday order
| Step | Part | What happens | Time taken |
|---|---|---|---|
| 1 | Client | Opens the menu | Instant |
| 2 | Cache | Sends the menu copy | 2 ms |
| 3 | Server and database | Checks and saves the order | 40 ms |
| 4 | Queue | Holds the email job | 1 ms |
| 5 | Worker | Sends the receipt later | 800 ms, nobody waits |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a cache is where data is stored. A cache can be emptied at any moment. Keep the real data in the database, and only copy into the cache what you can rebuild.