Scarcity: limited resources 00:00:00 Make a prediction You will turn a finite-resource limit into an admission policy and test that permits return even on failure. Three database connections are busy. A fourth request starts work without reserving capacity. Has starting another thread created another connection? No. A fourth thread only competes for the same three connections. It needs a defined policy: wait within a deadline, reject, or use a separately provisioned resource. 00:00:31 Three telescopes on the roof A mountain hut has three sleeping bags for four walkers. A longer sign-in sheet records more names but creates no new bag. A returned bag can be handed to one waiting walker. Concurrency limits represent real capacity. More workers can conceal scarcity as a longer queue, so track waiting time as well as utilization. A scarce resource is something that fewer threads can use at once than want to: database connections, licences, memory, slots of a slow service. Scarcity control decides who uses it and what happens to the rest. Without a limit the thing itself decides, usually by failing for everybody. With a limit but without a lease the limit shrinks until nothing is left. With a lease but without a line limit, everybody waits longer than it is worth. 00:01:24 Limit borrowers of a connection pool A service has three database connections and ten simultaneous requests. Starting ten operations just moves contention into the database. Each request claims one permit before borrowing a connection. The fourth request waits, times out, or is rejected according to an explicit policy. When one connection is returned, release its permit exactly once. Test a query that throws so a forgotten return cannot quietly shrink the usable pool. Hold all three connections with long queries, then start seven more callers. They must wait, fail, or be queued within an explicit bound. Opening a new worker thread does not create storage capacity. Check that waiting requests can time out without permanently consuming a slot. 00:02:13 Two islands of capacity Image rendering and ordinary search share a process. If slow image requests can occupy every worker, search stops too. Separate capacity budgets isolate the workloads. Measure queue time and rejection counts to see whether either budget is too small. Run eight batch jobs until they hold their allowed slots. Two short user requests can still enter the protected budget. If every workload shares one unbounded admission queue, long jobs can occupy every seat and protect throughput numbers while interactive latency becomes unacceptable. A permit leaked on every exception gradually reduces useful capacity even while physical connections sit idle. Test a failed operation followed by another successful borrow. Reserve separate budgets when one slow workload must not starve a more critical workload. HikariCP. A connection pool for Java. maximumPoolSize limits it, connectionTimeout (30 seconds by default) ends the wait with an exception, and leakDetectionThreshold logs connections that stay out too long. Go database/sql. SetMaxOpenConns limits open connections. DB.Stats reports how many are in use, how often callers waited, and for how long in all. 00:03:34 Try an interview follow-up Ten threads, a pool of ten, and everything stands still. No thread uses processor time. Why? Probably each holds one connection and waits for a second. In this example, multiply the thread count by one less than the maximum number of connections a thread can hold, then add one. Ten threads that need two connections each give a bound of eleven. That rules out this particular standstill. Better: pass the connection down, so no request borrows twice. A thread dump shows it: all in borrow, with a borrow further up the same stack. Eight workers protect a database from too many active requests. New work still arrives twice as fast as they finish. Where does it go? Bound the waiting queue as well, decide what to do when it fills, and measure waiting time. A concurrency limit on its own leaves the waiting room unbounded. 00:04:36 What to take with you Lend leases. Count what exists or is about to exist, make and close outside the lock, give back on every way out, once. Closing refuses new borrowers, wakes the sleeping ones and waits for the rest. With different sizes the line needs a rule: blocked by your own quota you step aside, blocked by the common budget you hold the line. The granter takes the amount for the waiter, so nobody steals it.