Report metered usage so a retry never counts twice
Networks fail, workers restart, queues redeliver. If every retry adds to a customer’s usage, someone is charged for work they did once. An idempotency key makes the second attempt a no-op.
6 min read · Last checked
In short
Key points
- Derive the key from the business event, such as a job id. A random value generated on each attempt defeats the purpose.
- A repeated call with the same key is recognised as an idempotent replay, and does not raise a second threshold alert.
- A call that would exceed the limit is refused with HTTP 400 and an error message. Decide what your user sees.
- If the work is rolled back, release the usage with its own key.
What the key protects you from
Consider a report-generation job that consumes one unit of usage. Your worker calls the platform, the platform counts it, and the response is lost on the way back. The worker retries. Without a key the customer is counted twice. With the same key the platform recognises the repeat.
The key must be stable across retries of the same business event and different for different events. A job id, an order id, or a request id your client sends all work. A new GUID generated inside the retry loop does not.
Consume with a stable key
The result carries Success, the current value, what remains, and whether the call was a replay. Check the application and tenant you pass are the ones the work belongs to, because a bug there is a billing bug.
using TenancyEngine.Sdk;
static async Task<bool> TryConsumeAsync(
TenancyEngineClient te, string tenantSlug, string jobId, CancellationToken ct)
{
try
{
var result = await te.ConsumeUsageAsync(
"my-app", tenantSlug, "Production", "reports",
amount: 1,
idempotencyKey: $"report-{jobId}", // same job, same key, however many retries
ct: ct);
// result.IdempotentReplay is true when this exact key was already counted.
return result.Success;
}
catch (TenancyEngineApiException ex) when (ex.StatusCode == 400)
{
// The platform refused the call, for example because the limit is reached.
// ex.Message carries the reason. Show the customer an upgrade path, not a stack trace.
return false;
}
}Gauges and counters are different
A counter meter, such as exports this month, only goes up until its period resets. A gauge meter, such as seats or projects, can fall again. Release usage when a seat is removed or a project is deleted. Threshold alerts follow the same split: counters alert once per threshold per period, gauges alert every time usage rises back across a threshold.
Roll back with release
If you consume before the work and the work then fails, release the same amount. Give the release its own key, for example one that includes the word release and the job id, so the two operations are not confused with each other.
Batching
When one request touches several features, the batch call takes a list of items, each with a feature key, an amount, and an optional key. The platform can answer 207, meaning some items succeeded and some did not, so read each item’s result instead of assuming all or nothing.
Check first, but do not rely on the check
Reading the runtime context before an expensive operation gives you a friendly early answer. The consume call is still the authority, because two requests can pass the check at the same moment. Treat a refused consume as the real answer.
Honest limits
What to know before you rely on this
- You report the usage. TenancyEngine counts what your server sends.
- The article does not state how long the platform remembers a key. Do not reuse a key for a different event.
- Metered features and threshold alerts are described in the docs; billing the usage to a customer needs Growth or above.
Questions people ask
What should I use as the idempotency key?
An identifier of the business event that is the same on every retry: a job id, an order id, or a client-supplied request id.
Does a replay trigger a second alert email?
No. Replaying a consume with the same key never alerts twice for counter meters.
Sources
Keep reading
Try it on the free plan
One app and five customers, no credit card required.