Design patterns and optimization · Intermediate

Admission control

Protect a constrained resource before work begins by defining concurrency limits, queueing, rejection and deadlines.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Protect the resource before expensive work
  2. Distinguish concurrency from rate
  3. Release capacity on every exit path
  4. Consider fairness and retry feedback
  5. Ask Logeia about this articleCheck your understanding

Protect the resource before expensive work

Admission control decides whether new work should enter a constrained part of the system. If a database has twenty connections, starting one thousand simultaneous operations does not create more database capacity. A semaphore can bound active operations, but an unlimited queue of waiting requests still consumes memory and time. Define active slots, waiting slots and waiting deadlines separately. Place the admission decision before expensive parsing, allocation or downstream calls whenever those resources also need protection.

Distinguish concurrency from rate

With twenty active slots and forty waiting slots, sixty unfinished requests fill the configured capacity. The sixty-first request needs a defined rejection or deferral path. This is not a limit of twenty requests per second: if each active request takes ten seconds, twenty slots sustain only about two completions per second in the simplified steady state. Service-time changes alter throughput without changing the concurrency cap. Rate limits and concurrency limits can therefore serve complementary purposes.

Release capacity on every exit path

An admitted operation must return its permit after success, exception or cancellation. Use a finally block or a structured context manager rather than relying on every branch to release manually. Waiting requests can also expire before they start; executing them afterward wastes capacity for clients that may already have abandoned the result. Remove expired work or recheck its deadline before execution. Observe rejected requests and expired waits as well as the number of currently occupied permits.

Consider fairness and retry feedback

A first-come queue can let long jobs delay short requests, while always favoring short jobs can starve long ones. Separate budgets or an explicit scheduling policy may be appropriate for different workloads. Rejected clients can worsen overload if they immediately retry, so combine admission with bounded retries, backoff and jitter where callers control retries. Admission control does not speed up the protected database; it limits how much collateral work accumulates when demand exceeds capacity.

Distinguish protection of the shared resource from fairness among callers. A global limit can keep the database healthy while one tenant consumes every slot. Per-tenant budgets or a fair queue may be needed in addition, but they add scheduling policy. Duplicate requests after uncertain outcomes can also occupy capacity unnecessarily, so connect admission decisions with the operation’s idempotency and cancellation rules.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 18

Understand the systems behind the web · STEP 15

Choose a learning path