Error Budget Remaining for an SLO
Problem Statement
Given an availability SLO and observed request counts, compute how much of the error budget remains.
Background
For a target availability slo (e.g. 0.999) over total requests, the allowed failures (error budget) is (1 - slo) * total. With bad observed failures, the fraction of budget remaining is (budget - bad) / budget, clamped to [−∞, 1] but reported down to negative if exhausted. If the budget is 0 (slo == 1), remaining is 1.0 when bad == 0 else 0.0.
Your Task
def error_budget_remaining(slo, total, bad):
Return the fraction of error budget remaining, rounded to 4 decimals (may be negative).
Input Format
- slo (float in (0,1]), total (int), bad (int).
Output Format
- A float rounded to 4 decimals.
Sample
print(error_budget_remaining(0.99, 1000, 4))
Output:
0.6
Example:
print(error_budget_remaining(0.99, 1000, 4))
0.6
- Calculate the total error budget allowed by the SLO: with a target availability of 0.99 and 1000 total requests, the allowed failures are (1−0.99)×1000=10.
- Determine the remaining budget by subtracting the observed failures from the total budget: 10−4=6.
- Compute the fraction of the budget remaining by dividing the remaining budget by the total budget: 6/10=0.6.
- Round the result to 4 decimal places as required: 0.6 remains 0.6.
- The final output is 0.6
Constraints:
- budget =
(1 - slo) * total. - remaining =
(budget - bad) / budget; if budget == 0, return 1.0 (bad==0) else 0.0. - Round to 4 decimals; may be negative if overspent.
1. Background Knowledge
Service Level Objectives (SLOs) define the target reliability of a system, typically expressed as a fraction of successful requests. For example, an SLO of 0.999 means the system aims to serve 99.9% of requests successfully. The error budget is the complement of this target: it quantifies how many failures are "allowed" before the SLO is violated. Mathematically, for a target availability slo over total requests, the absolute error budget is:
budget=(1−slo)×totalThis budget acts as a safety valve in production systems. Teams can deploy risky changes as long as they do not exhaust the budget. Once the budget is consumed, further changes are typically blocked until the budget resets.
The remaining budget fraction measures how much of this allowance is left after observing bad failures. It is computed as:
remaining=budgetbudget−bad​This value can be negative if failures exceed the budget, indicating the SLO has been breached. It is clamped at 1.0 from above (you cannot have more than 100% of the budget remaining) but is reported as-is if negative.
2. Algorithm Approach
This is a direct computation problem with a special edge case. The core logic involves:
- Calculating the absolute error budget.
- Handling the degenerate case where the budget is zero (perfect availability target).
- Computing the remaining fraction.
- Applying clamping and rounding.
No complex data structures or iterative algorithms are required. The challenge lies in correctly handling floating-point precision and boundary conditions.
3. Step-by-Step Strategy
- Compute the absolute budget: Calculate budget = (1 - slo) * total. This represents the maximum number of failures allowed.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.