Match day, 10,000 fans
Your football app is quiet all week. On match day, 10,000 fans open it at once.
A good app can add more copies of itself in seconds. When the busy time ends, it removes them. It keeps a clear record of what happened. It starts fast and stops without losing work.
These are the last six factors.
Key words
| Word | What it means | Example |
|---|---|---|
| Database | An organised store of data that programs can search and change. | |
| Port binding | The app listens on a port number itself and offers its service there. | port 8000 |
| Process | One running copy of a program. | |
| Load balancer | A tool that shares the users between your processes. | |
| Scale out | Handle more users by running more copies of the process. | |
| Disposable | Easy to start and easy to stop at any time. | |
| Graceful shutdown | Finish the work in progress, then stop. | |
| Parity | Being alike. Dev, staging and production should be alike. | |
| Staging | A copy of the live system used for a last check before release. | |
| Log | A record of events, one line for each thing that happened. | |
| Event stream | Events that arrive one after another, in time order. |
The cells students fill in are left blank in this preview.
Six more habits
In part one you met codebase, dependencies, config, backing services, build and release, and processes.
Now try the idea behind factor 11. A log is a record of events. The app writes one line per event and does not care where it goes.
Try it: logs as a stream
def log(level, message):
print(level, message) # just print. Something else collects the lines.
log("INFO", "app started")
log("INFO", "fan joined")
log("ERROR", "score service did not answer")
# Add a fourth line of your own.
Factors 7 to 12
| Factor | Rule in plain words | Why |
|---|---|---|
| 7 Port binding | The app listens on a port and offers its own service | It does not need another program to run inside |
| 8 Concurrency | Scale out: run more processes, not a bigger machine | You add copies on match day |
| 9 Disposability | Start fast. Shut down gracefully | A process can come and go without losing work |
| 10 Dev and live alike | Keep development, staging and production as similar as you can | A bug that appears on live also appears in testing |
| 11 Logs | Write events as a stream of lines. Do not manage log files yourself | Tools can collect, search and watch the lines |
| 12 Admin tasks | Run one-off jobs as one-off processes, from the same code | A database fix uses the same code and settings as the app |
The cells students fill in are left blank in this preview.
Worked example: how many workers?
| Fans | Fans one worker can serve | Working | Workers needed |
|---|---|---|---|
| 250 | 100 | 100 + 100 = 200, not enough. 100 + 100 + 100 = 300, enough | 3 |
| 300 | 100 | 3 workers serve exactly 300 | 3 |
| 301 | 100 | 3 workers serve 300, one fan is left | 4 |
The cells students fill in are left blank in this preview.
Factor 7 in code: listen on a port
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
class Hello(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"Scores: Reds 2, Blues 1")
port = int(os.environ.get("PORT", "8000")) # the port is a setting too
HTTPServer(("0.0.0.0", port), Hello).serve_forever()
# Run this on your own computer, then open http://localhost:8000 in a browser.
# The app is its own web server. It needs no other program.
Which factor is it?
Have a go
Which factor is it?
5 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.