Phase 3 of 4 · Study weeks 9-12
Async, performance and durability
This phase makes the third checkpoint possible: a service that holds its latency target and loses no acknowledged event.
Topics every week
- Week 98 h
- The event loop and
asyncio - Tasks, cancellation and timeouts
- Blocking calls and
to_thread - Bounded queues and backpressure
Hand inAn async service with a bounded queue and a test that blocking stalls it
- The event loop and
- Week 108 h
- Web layer with FastAPI
- Dependency injection
- Sessions and transactions with SQLAlchemy
- Batched writes
Hand inBatched inserts and a measured gain over one insert per event
- Week 118 h
- Durability: acknowledge after commit
- Idempotent writes
- Crash tests with
kill -9 - Graceful shutdown
Hand inThe 20-kill table
- Week 128 h
- Load testing
- Latency percentiles and spread
- Tracing an async service
- Moving hot code out of Python (NumPy, Polars, Rust through PyO3)
Hand inCheckpoint 3, with predicted and measured numbers side by side
Practice projects
- Practice 1, the blocked loop (Week 9): call a blocking function inside a coroutine. Done when: a trace shows the stall, and
to_threadremoves it. - Practice 2, the batch size (Week 10): write 1, 10, 100 and 1,000 events per insert. Done when: a table shows throughput and p99 for each and you pick the size with a reason.
- Practice 3, the crash (Week 11): kill the process in the middle of a load run, 20 times. Done when: every acknowledged event is present after restart and the table shows the result.
- Optional, the vectorised transform: rewrite the hot transform with NumPy or Polars. Done when: a table compares pure Python with the vectorised version and says when the gain stops.
GitHub repos to study
Existence and last commit were checked on 22 Nov 2026 by shallow clone. Every repository below had a commit in the last 12 months.
| Repo | What to read | Week |
|---|---|---|
| fastapi/fastapi | Dependency injection and the OpenAPI generation | 10 |
| sqlalchemy/sqlalchemy | Sessions, the unit of work and the Core layer | 10 |
| MagicStack/uvloop | What a faster event loop changes and what it does not | 9 |
| pola-rs/polars | The lazy engine and expression API | 12 |
| PyO3/pyo3 | How the interpreter lock is handled in a Rust extension | 12 |
Milestone checkpoint
Build Checkpoint 3. Write the load script, your latency prediction and the 20-kill script first. Add the async service, the queue, the pool and the batched writes. Seed a blocking call in the loop and find it from a trace. Hand in the latency report with spread, the kill table, ADR-5 (the acknowledgement rule) and ADR-6 (the batch size and queue bound).
Check yourself
Answer each question in your head first, then open it to check.
What happens when a coroutine calls a blocking function?
The event loop is stuck until it returns, so every other task waits. Useawaiton an async call orto_threadfor blocking work.Why use a bounded queue?
A full queue pushes back on the producer. An unbounded queue moves the overload into memory until the process dies.When is it safe to acknowledge an event?
After the commit that makes it durable. An acknowledgement before the commit can be lost on a crash.Why do batched inserts raise throughput?
One round trip and one commit cover many rows, so the fixed cost is shared.When is moving code out of Python the right call?
When a profile shows one hot loop on large data that a vectorised library or an extension does much faster, and the cost of maintaining it is lower than the cost of the slowness.