Function concurrency and billing
Concurrency is not request rate
Concurrency is the number of execution attempts active at the same time. Throughput is the number completed per unit of time. Under stable arrival and completion rates, average in-flight work is approximately arrival rate multiplied by mean time in the system. For execution concurrency alone, use mean active execution duration and the rate of attempts actually starting, excluding queue wait. The estimate is not a peak, a quota or a scaling guarantee.
For 120 attempts per second with mean active duration of 250 ms, estimated average concurrency is 120 × 0.25 = 30. A toy platform allowing four simultaneous invocations per instance needs at least ceil(30/4) = 8 instances for that average load if evenly distributed. Real provisioning delays, tail latency, CPU contention and bursts require measurement and headroom.
Protect downstream capacity
A function can scale faster than a shared database. If each active invocation occupies one database connection and the database permits 24 connections for this workload, then a limit of 24 active invocations is the simplified upper bound; reserve headroom for administrative and other work. At 0.25 s per attempt, 24 active slots can complete approximately 96 attempts/s under ideal saturation. With 120 arrivals/s and no loss or retries, backlog grows by 24/s, or 1,440 in a minute.
Use bounded concurrency, admission control and a queue where the business permits waiting. A limit can lead to throttling, delayed events or failed requests depending on the trigger. Monitor queue age as well as depth; make a plan for excess work. Some provider settings reserve capacity, others cap instances, others limit requests per instance. Confirm actual semantics and downstream connection pooling. Increasing memory can improve performance but may raise unit cost; benchmark with safe representative inputs.
Retries and idempotent business effects
A transient error is a failure that may clear, such as a temporary dependency outage. Retry only suitable operations, with a bounded attempt count, backoff and jitter; direct permanent errors to a reviewed failure path rather than retrying endlessly. The trigger or caller may already retry, so account for combined attempts. With asynchronous Lambda invocation, duplicate delivery is possible even if a previous attempt did not report an error. Azure retry rules depend on trigger and extension; Cloud Run event retry behavior depends on the delivery configuration. HTTP retries can be initiated by clients or intermediaries.
Idempotence means repeating an operation with the same identity does not multiply its intended business effect. The receipt key can be an order ID if the business permits exactly one receipt per order; if receipts have versions, use order ID plus version. The identity must be stable across retries and scoped to the operation and tenant where applicable. A repeated key with different data is a conflict, not a new order.
Illustrative serial ledger (not an atomic database implementation):
| Attempt | Key | Amount | Decision |
|---|---|---|---|
| 1 | B17 | 2500 | store |
| 2 | B17 | 2500 | duplicate: no write |
| 3 | B17 | 2600 | conflict: investigate |
| 4 | B18 | 900 | store |
The ledger ends with two receipts totaling 3400 cents. In a real concurrent system, an atomic conditional insert or unique constraint plus transaction prevents two workers from both passing a separate existence check. Acknowledge only after the durable action succeeds. For nontransactional side effects such as external email, use a supported downstream idempotency key or a durable outbox and reconciliation; an outbox alone does not ensure exactly-once delivery. Choose a deduplication retention period covering the possible replay window. Never promise universal exactly-once execution.
Estimate consumption charges using fictional rates
For a simplified single-invocation metering model, estimate billed attempts = original events + additional attempts. Resource usage in GB-seconds = attempts × billed duration in seconds × memory in GB. The units are a teaching convention here: use the actual billing-unit definition, rounding, free allowance and pricing tiers for the selected provider and plan.
Example: 100,000 original events, 5,000 additional attempts, each billed for 0.2 s at 0.5 GB. At invented rates of $0.30 per million attempts and $0.00002 per GB-second:
- Attempts: 105,000; usage: 10,500 GB-s.
- Request cost: 105,000 / 1,000,000 × $0.30 = $0.0315.
- Execution cost: 10,500 × $0.00002 = $0.21.
- Add fictional logs/storage of $0.50: total $0.7415 before tax or credits.
These rates are not provider quotes. In production, AWS Lambda pricing varies with function mode, architecture and options; Azure Functions varies by hosting plan and may charge for always-ready capacity; Cloud Run-based functions can include CPU, memory, requests and billing modes, with concurrency sharing an instance. A per-invocation GB-second estimate cannot reproduce a shared-instance invoice. Include gateway, queue, database, network, logs, idle capacity, storage and applicable taxes. Serverless can be economical for intermittent work but steady high utilization may favor another hosting model. Budget alerts do not universally cap spending.
Worked selection: latency versus cost
Two hypothetical function configurations process 1,000,000 attempts. Option A uses 0.5 GB for 0.4 s; option B uses 1 GB for 0.15 s. Both use the same fictional request rate of $0.30/million and resource rate of $0.00002/GB-s. A consumes 200,000 GB-s and costs $4.30; B consumes 150,000 GB-s and costs $3.30 before other charges. More memory can cost less when duration falls enough, but these durations are assumptions, not benchmark results. Compare latency percentiles, errors, concurrency and real billed usage, not just memory size. For latency-sensitive traffic, evaluate warm capacity options alongside their idle charges.
Practice and troubleshooting
Start with a trace that includes arrival time, event identity, attempt number, outcome and duration; redact payloads and secrets. If queue age rises, check start rate, execution duration, concurrency caps and downstream throttling. If bills rise unexpectedly, inspect retry counts, recursive triggers, always-ready capacity and log volume before changing limits. For the manual exercise, estimate costs and backlog, classify duplicate and conflict events, and propose a safe retry policy. Next chapter: API gateways and cloud application integration.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Function concurrency and billing — knowledge check
Choose the best answer for each offline scenario.
At 80 attempts/s and 0.5 s mean active execution, what is estimated average concurrency?
Two attempts use the same receipt key and identical data. What should the second durable operation do?
A database permits 20 connections, each active attempt needs one, and mean duration is 0.25 s. What is ideal maximum completion rate with 20 active attempts?
1000 billed attempts at 0.5 GB for 0.2 s each consume how many GB-s in the toy model?
Why is the toy GB-second formula insufficient for every serverless platform?