How do you size a per-function concurrency cap that keeps a function from flooding a rate-limited downstream API, and what does the cap fail to guarantee?
answer
- in flight, not per second
- rate times duration again
- a faster function lets more through
- the excess goes somewhere
basics
~20 sSet the cap near the downstream's sustainable rate times the function's average duration. A cap bounds requests in flight, not per second, so a faster function can still breach the rate limit, and the excess lands on callers or a backlog.
solid answer
~50 sA **per-function concurrency cap** bounds how many invocations run at once, so it bounds throughput at roughly cap ÷ average duration. To protect a downstream that accepts a known rate, size the cap as that rate × the function's average duration, measured under load, with headroom for retries. Two gaps remain. First, the cap limits concurrency, not rate: if the function gets faster (a cache, smaller payloads, a quicker downstream), the same cap lets more calls per second through, so a hard rate limit still needs a rate limiter in the code path or a metered consumer. Second, the excess does not disappear: synchronous callers are throttled, asynchronous events are held and retried until their maximum age, and queue messages wait, so the overload moves to callers or the backlog, which you must watch. On some platforms setting the cap also sets that capacity aside from the shared regional pool.
go deeper
Recall that a function can be given its own concurrency cap and that requests above it are throttled rather than run.
Explain the sizing arithmetic, cap equals rate times duration, and what happens to excess synchronous, asynchronous and queue-driven work.
Show the production judgment: the cap bounds concurrency not rate, it drifts when duration changes, fast failures defeat it, and you verify it held against the downstream's own metrics.
Weigh where downstream protection belongs, a cap per function, a shared rate limiter or a metered queue, and who owns re-sizing it as functions and dependencies change.
## What a cap actually bounds A **per-function concurrency cap** is a setting that limits how many invocations of one function may run at the same moment, regardless of how much room the regional quota has. Operators use it as **blast-radius control**: to stop one function from taking the whole regional pool, and to stop a function from sending a downstream more work than it can take. The cap is a limit on **work in flight**. It is not a limit on requests per second. The two are linked by Little's law: ```text throughput (calls per second) = concurrency / average duration (seconds) ``` So a cap bounds throughput only *through* duration. That one fact explains both how to size the cap and where it fails. ## Sizing it 1. **Find the downstream's real limit.** Use its published rate limit or its measured sustainable rate, whichever is lower, and decide how much of it this function may use if others share it. 2. **Measure the function's duration under load,** not at idle: the time a call takes when the downstream is busy is the one that matters. 3. **Multiply.** For illustration only: a downstream that sustains 100 calls per second and a function that averages half a second per call gives a cap of about 50. 4. **Leave headroom.** Retries from the function's own error handling, and other callers of the same downstream, use part of its budget. 5. **Watch it and adjust.** Re-check after any change in the function or the downstream. ## The concurrency-is-not-rate gap Because throughput = cap ÷ duration, the cap behaves differently depending on which way duration moves: - **The downstream slows down.** Each call takes longer, so the same cap lets *fewer* calls per second through. This is a welcome effect: the cap gives natural **back-pressure**. - **The function gets faster.** A new cache, smaller payloads or a downstream that answers more quickly shortens duration, so the same cap lets *more* calls per second through. A cap sized last quarter can quietly exceed the downstream's rate limit after a performance improvement. - **Errors return fast.** If the downstream starts rejecting calls immediately, failed invocations finish quickly and free their slots, and the function can hammer the downstream with retries faster than before. When the downstream's limit is a strict rate, the cap is the coarse guard and a **rate limiter** in the call path, or a consumer that pulls from a queue at a metered pace, is the precise one. The cap still earns its place by bounding in-flight work and cost. ## Where the excess goes A cap does not make extra demand disappear; it decides who absorbs it: | Invocation type | Above the cap | What to watch | |---|---|---| | Synchronous | The caller gets a throttling error and must retry or fail | Throttle count, caller-side error rate | | Asynchronous | The platform holds the event and retries it until its maximum age, then drops it or sends it to a failure destination | Event age, failure-destination volume | | Queue or stream | Messages wait in the source | Oldest message age, queue depth | For a user-facing synchronous function, a tight cap turns downstream protection into user-visible errors, so it should come with a plan for those callers. For queue-driven work it turns into lag, which is often the right trade. ## Side effects of the cap itself - **Reservation on some platforms.** Some platforms implement the cap by also setting aside that much of the regional pool for the function, so a large cap shrinks the pool for every other function. - **Queue interaction.** On some platforms a queue-driven function that is throttled by its cap returns messages to the queue, and each return counts as a receive attempt; a tight cap with a low retry count can push healthy messages to a dead-letter queue. - **Burst latency.** A cap sized well under normal peak keeps the fleet small but makes every burst wait for slots. ## Checking it held After the change, confirm with numbers: the function's concurrent executions should flatten at the cap during a burst, the downstream's request rate and rejection count should stay under its limit, and the throttle or backlog metrics should show where the overflow went. If the downstream still rejects calls while the cap holds, the gap is rate, not concurrency.
- Why can a per-function cap give natural back-pressure when the downstream slows down?When the downstream slows, each invocation takes longer and holds its slot longer. With a fixed number of slots, fewer calls complete per second, so the function automatically sends less work to the struggling service instead of piling more on.
- What would you add if the downstream enforces a strict calls-per-second limit?A rate limiter in the call path, shared across instances through a fast store, or move the calls behind a queue whose consumer is metered to the allowed rate. Keep the concurrency cap as the coarse bound on in-flight work and cost.
- Why can a tight cap on a user-facing synchronous function be a poor trade?Every request above the cap becomes a throttling error the user's client must handle. Protecting the downstream that way moves the overload to users, so it needs client retries with backoff, a degraded response, or an asynchronous path for work that need not finish inline.
A cap is the number of checkout lanes, not the speed of the cashiers. Fewer lanes limit how many shoppers are served at once, but if the cashiers get twice as fast, twice as many shoppers leave per minute through the same lanes.
- A concurrency cap of N guarantees at most N calls per second to the downstream.
- Throttled asynchronous events are discarded the moment the cap is reached.
- Size the cap from the function's own peak traffic rather than the downstream's capacity.
- Once a cap is set, the overflow needs no monitoring because nothing fails.
- A cap is free: it never affects the function's own callers or backlog.