Concurrency, Profiling & Operations
A fast happy path is not an operable system. Bounded work, cancellation, profiles and observable failure behavior are the foundations for later training and serving systems.
Section 4 of 5 · 7 units · about 39 hours
By the end you can Run a bounded worker pipeline through overload, cancellation and worker failure, then use profiles, logs and metrics to justify one measured improvement and one rejected optimization.
Units
- Work types, threads, processes, async tasks and the GIL5h
- Synchronization primitives and concurrency hazards6h
- Task ownership, cancellation, timeouts and retries6h
- Backpressure, load shedding and graceful shutdown5h
- Profiling CPU and memory, and benchmarking honestly6h
- Structured logs, metrics and traces5h
- Health checks, containers, supervision and runbooks6h
Build it
Mini project · about 12 to 16 hours
Bounded async worker pipeline with overload, cancellation, telemetry and a container runbook
Give the task runner an execution engine: a bounded async pipeline with deadlines, retries, telemetry and a container runbook. Profile it honestly and make every failure visible through signals another reader can follow.
- 1Compare matched CPU work across serial, thread and process modes on the available Python builds, including a free-threaded one where available, then show an event-loop blocking fault and its move off the loop.
- 2Build the bounded pipeline with
TaskGroupworkers that rejects excess work, drains on shutdown and reports abandoned work instead of success when the drain expires, following the AlgoMaster Bounded Buffer problem. - 3Run each task under a monotonic deadline with classified retries, jittered backoff and cancellation cleanup, tested against a fault-injected dependency that fails then succeeds.
- 4Profile a large batch for the hot spot and retained memory, fix one cause, then benchmark the improvement without the profiler and record one rejected optimization with its numbers.
- 5Instrument runs with spans, queue-wait children, JSON logs and bounded-label counters, exclude secrets from every signal, and locate one seeded failure from the signals alone.
- 6Ship a non-root bounded container with liveness and readiness gates, exercise the dependency outage and
SIGTERMdrain, and write a runbook a second reader follows to recovery.
What you hand in
- The pipeline with overload, shutdown, deadline, retry and cancellation tests plus the executor timings.
- A profiling note with the hot spot, the retention fix, the benchmark and the rejected optimization.
- Telemetry tests, the container definition and the exercised runbook with outage and drain transcripts.
How to check it
- Self-grade with the rubric below
How it's graded
Bounded concurrency
Pass: The overload test rejects excess work, and every accepted run ends recorded or is reported abandoned.
Great: Executor choices cite the matched-work timings, and the blocking fault shows the stalled and advancing heartbeat.
Failure handling
Pass: No retry follows permanent failure, an exhausted budget, an expired deadline or cancellation, and no worker outlives its owner.
Great: Backoff bounds are tested without wall-clock waits, and an idempotency note covers already-applied retries.
Performance evidence
Pass: The hot spot and retention site come from profiler output, and the kept improvement has unprofiled trials.
Great: The note states the workload region where the gain holds and why the rejected optimization lost.
Failure telemetry
Pass: Parent and trace ids agree across the queue, metrics carry only bounded labels, and secrets appear in no signal.
Great: The seeded failure is traced from a metric to a span to a log line without added prints.
Container operations
Pass: The container runs non-root under its limits, survives the outage without a restart, and drains on SIGTERM.
Great: A second reader followed the runbook to recovery, and the steps they found unclear were fixed.
Resources
- AlgoMaster ConcurrencyAlgoMaster
Study Concurrency 101; the Python units on threads, the GIL, multiprocessing, asyncio and concurrent.futures; synchronization primitives; concurrency challenges; locking strategies; and concurrency patterns. Then solve Bounded Buffer, Thread-Safe Blocking Queue, Thread-Safe Rate Limiter and Task Scheduler with Dependencies.
- Python concurrency documentationPython Software Foundation
Use asyncio task groups, cancellation, queues and timeouts; concurrent.futures; multiprocessing; threading; and signal documentation to verify lifecycle and failure behavior.
- High Performance Python, 3rd editionMicha Gorelick and Ian Ozsvald, O'Reilly
Read Chapter 2 Profiling to Find Bottlenecks, Chapter 9 Asynchronous I/O, Chapter 10 The multiprocessing Module and Chapter 12 Using Less RAM, and use the companion code (https://github.com/mynameisfiber/high_performance_python_3e) as experiment starting points. Change the workloads and measure the regimes where concurrency helps, loses or only moves the bottleneck.
- Timeouts, retries, and backoff with jitterAWS Builders' Library, Marc Brooker
Read Timeouts, Retries and backoff, and Jitter for deadline scope, idempotency, one retry owner and bounded retry load. Interpret the five-layer example as three total attempts per layer, not three retries plus the initial attempt.
- Site Reliability Engineering - overload and troubleshootingGoogle
Read Chapter 21, The Pitfalls of Queries per Second, Client-Side Throttling, Utilization Signals, Handling Overload Errors and Conclusions. Read Chapter 12, Triage, Examine, Diagnose, Test and Treat, and Making Troubleshooting Easier for a service runbook. Skip datacenter load-balancing implementation details.
- Liveness, Readiness, and Startup ProbesKubernetes
Read Liveness probe, Readiness probe and Startup probe for the different health decisions and failure actions. Apply the contracts to a local service; do not install Kubernetes or configure a cluster.
- Operating Systems: Three Easy Pieces - concurrencyRemzi and Andrea Arpaci-Dusseau
Read Chapters 26-33, from threads through event-based concurrency, for locks, condition variables, semaphores, concurrency bugs and event loops. Do selected homework only for a weak mechanism.
- Docker guidesDocker
Use the Python container guide and build best practices to run as non-root, pin dependencies, handle signals and test CPU and memory limits.
- OpenTelemetry Python instrumentationOpenTelemetry
Manually instrument one request so its structured logs, one low-cardinality metric and one trace share a correlation identity. Use the telemetry to locate one seeded fault.
- Algorithmica - Getting Accurate ResultsSergey Slotin
Use Measuring the Right Thing and Reducing Noise for benchmark design: input distribution, latency versus throughput, warm-up, frequency scaling and pinning. The examples are C++; the checks transfer to Python.