AlgoViz

Scarcity: limited resources

hard 50 min

When more threads want something than there is of it, a design must say who gets it, who waits, for how long, and how it comes back.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 5:03

See it run

Who gets it, who waits, for how long, and how it comes back.

Operations

Borrow and give back

One scanner, three people, one of them breaks it.

Step 1 of 8

First borrow

ashaThreadisscanningmaxThreadisidleleeThreadisidlepool of 1ScannerPoolfree0alive1scanner 1Scannerwithashaborrow(5)lease

Nothing is free, but one may be made: alive becomes 1, then the scanner is made.

Step 1: First borrow

def borrow(self, timeout):    with self._changed:        able = lambda: self._free or self._alive < self._size        if not self._changed.wait_for(able, timeout):            raise PoolTimeout()        if self._free:            return Lease(self, self._free.pop())        self._alive += 1    return Lease(self, self._make())def _give_back(self, scanner, broken):    with self._changed:        if not broken:            self._free.append(scanner)            self._changed.notify()            return    scanner.close()    self._forget()      # alive - 1, notify all
Watch
free and alive
Result
One user at a time

alive never goes above size.

In the real world

The same scarcity doing real jobs. Pick one and step through it.

One scanner a day is not given back.

Step 1 of 4

Monday

handlerThreadisfailedpoolScannerPoolsize3can lend2never comes

Three in the pool. A handler throws between take and give back.

Step 1: Monday

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Give back without finally

    critical

    The first error makes the pool smaller for good.

    Wrong
    s = pool.take()s.scan(page)pool.put(s)
    Right
    with pool.borrow(5) as s:    s.scan(page)
  • Counting after making

    critical

    Everybody sees room before anybody counts.

    Wrong
    scanner = make()self._alive += 1
    Right
    self._alive += 1        # under the lockscanner = make()        # outside
  • Making with the lock held

    major

    Nobody can give back while a slow object is made.

    Wrong
    with self._changed:    return Lease(self, self._make())
    Right
    with self._changed:    self._alive += 1return Lease(self, self._make())
  • A line without a limit

    critical

    Under overload everybody waits too long.

    Wrong
    waiting = Queue()
    Right
    waiting = Queue(maxsize=8)# full: refuse with a hint
  • Release in a finally around acquire

    critical

    If acquire fails, somebody else's slot is freed.

    Wrong
    try:    gate.enter(1)    work()finally:    gate.leave(took)
    Right
    gate.enter(1)try:    work()finally:    gate.leave(took)
  • Not looking at the line when someone gives up

    major

    The next one sleeps beside free memory.

    Wrong
    self._line.remove(t)return False
    Right
    self._line.remove(t)self._grant()return False

Interview tips

  • Four questions

    • Who gets it?
    • Who waits, how long?
    • How does it come back?
  • Lend leases

    • On every way out.
    • Exactly once.
    • Broken ones are closed.
  • Short lines

    • Patience x slots / time.
    • Refuse early.
    • Give a time to return.
  • Size by numbers

    • Rate x time held.
    • Plus half.
    • Shorten before you enlarge.