AlgoViz

Thread pool

medium 70 min

A few threads that live long, one queue with a limit, and a loop: take, run, repeat.

Watch lesson5:06

Overload

Four answers to a full queue, as a strategy

The point: a full queue is not an error of the pool. It is the moment where the design decides who pays: the new task, the old one, or the caller.

Who pays?

PolicyPaysJava nameDanger
RefuseThe new taskAbortPolicy, the defaultThe caller ignores it
Caller runsThe caller's timeCallerRunsPolicyIt runs in an event loop or with a lock held
Drop the oldestThe one who waited longestDiscardOldestPolicyIts future never ends
Drop the new one silentlyThe new task, without knowingDiscardPolicyData is lost and nobody sees it
Wait a momentThe caller's time, limitedwrite it yourselfMany callers wait together

Policies as a strategy

Each answers one question: the queue is full, what now? The pool does not know which one it has. See strategy.

import threadingfrom collections import dequeclass Rejected(Exception):    passclass Abort:    def handle(self, box, job):        raise Rejected("the queue is full")class CallerRuns:    def handle(self, box, job):        if box.closed:            raise Rejected("the pool is closed")        job()                                # in the thread that submitted itclass DropOldest:    def handle(self, box, job):        while not box.offer(job):            if box.closed:                raise Rejected("the pool is closed")            box.drop_oldest()class WaitAMoment:    def __init__(self, seconds):        self.seconds = seconds    def handle(self, box, job):        if not box.offer(job, self.seconds):            raise Rejected(f"no place after {self.seconds} s")

Lines 7 to 9

The honest default. Fast, and the caller can tell its user the truth.

1 of 4

Walk

A slow dependency, minute by minute

40 workers, normally 0.2 s per task, 150 tasks per second

TimeWhat happensWithout a limitWith 100 places and a refusal
0 sBusy: 150 x 0.2 = 30 of 40FineFine
10 sThe payment service now needs 2 s40 / 2 = 20 tasks per second are donethe same
11 s130 too many per second130 waiting100 waiting, the rest hears busy
70 sOne minute of it7,800 waiting, 390 s of work100 waiting, 5 s of work
71 sThe service is fast again: 200 per secondNeeds 7,800 / (200 - 150) = 156 s to catch upNeeds 100 / 50 = 2 s
During those 156 seconds it answers users who left long ago. The refusals looked worse and were kinder. 130 x 60 = 7,800 and 7,800 / 20 = 390.

Requests time out after 3 seconds at the caller. Your queue holds 5 seconds of work. What is wrong, and what do you do?

BreaksNothing, the queue protects us
How
Leave it.
Limit
At full load every task is done for nobody: 100 percent busy, 0 percent useful.
Choose when
Never.
WorksShorten the queue
How
Below 3 seconds of work.
Limit
The speed changes with the dependency, so the number is never right for long.
Choose when
As a first step.
StrongDeadline with the task
How
Each task carries its deadline. A worker that takes an expired one drops it at once, and submit refuses if it cannot make it.
Limit
The deadline must travel with the request.
Choose when
Any chain of services with timeouts.