Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Twelve factors, part two

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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

WordWhat it meansExample
DatabaseAn organised store of data that programs can search and change.
Port bindingThe app listens on a port number itself and offers its service there.port 8000
ProcessOne running copy of a program.
Load balancerA tool that shares the users between your processes.
Scale outHandle more users by running more copies of the process.
DisposableEasy to start and easy to stop at any time.
Graceful shutdownFinish the work in progress, then stop.
ParityBeing alike. Dev, staging and production should be alike.
StagingA copy of the live system used for a last check before release.
LogA record of events, one line for each thing that happened.
Event streamEvents 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

Code: python
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

FactorRule in plain wordsWhy
7 Port bindingThe app listens on a port and offers its own serviceIt does not need another program to run inside
8 ConcurrencyScale out: run more processes, not a bigger machineYou add copies on match day
9 DisposabilityStart fast. Shut down gracefullyA process can come and go without losing work
10 Dev and live alikeKeep development, staging and production as similar as you canA bug that appears on live also appears in testing
11 LogsWrite events as a stream of lines. Do not manage log files yourselfTools can collect, search and watch the lines
12 Admin tasksRun one-off jobs as one-off processes, from the same codeA 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?

FansFans one worker can serveWorkingWorkers needed
250100100 + 100 = 200, not enough. 100 + 100 + 100 = 300, enough3
3001003 workers serve exactly 3003
3011003 workers serve 300, one fan is left4

The cells students fill in are left blank in this preview.

Factor 7 in code: listen on a port

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

3 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.