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.

Watch lesson5:03

Where it shows up

The same pattern appears across everyday software.

  • 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.

  • PgBouncer

    Puts many clients on few PostgreSQL connections. query_wait_timeout disconnects a client that waited too long for a server, and reserve_pool_size adds connections when clients wait.

  • PostgreSQL

    max_connections is the limit, and superuser_reserved_connections keeps some slots for administrators: a reserved share.

  • Java Semaphore

    Permits without objects and without ownership. The constructor chooses fairness, and the documentation says that without it barging is permitted.

  • Kubernetes ResourceQuota

    Limits the sum of resources per namespace. A request that would go over it is rejected with status 403 and a message that names the limit.

  • nginx limit_conn

    Limits connections per key, such as an address. Those above the limit are refused, with status 503 unless configured otherwise.

  • Resilience4j Bulkhead

    Two versions: permits, or a fixed thread pool with a bounded queue. By default 25 calls may run at once and nobody waits.

  • Envoy circuit breakers

    Limits per upstream cluster, among them the number of pending requests that wait for a connection: a bounded line.

  • TCP flow control

    The receiver announces how much it can take. If the window shrinks to zero the sender must stop, and probe from time to time.

  • Netty

    High and low water marks on the write buffer: above the high one a channel is no longer writable, until the queue falls below the low one.

  • Linux control groups

    pids.max is a hard limit on processes. memory.high slows a group down before memory.max is enforced: backpressure first.