Entitlement cache: fast checks and a chosen behaviour when the platform is down
EntitlementCache in TenancyEngine.Sdk (2.7.0 or later, namespace TenancyEngine.Sdk.Entitlements) answers "may this tenant use this feature?" from memory in most requests, never makes your request wait on a slow platform, and does what you decided for each feature when the platform cannot be reached.
Read Tenant entitlements first for the contract it builds on.
Use it
var cache = new EntitlementCache(client, new EntitlementCacheOptions
{
Ttl = TimeSpan.FromSeconds(30),
StaleWhileRevalidate = TimeSpan.FromSeconds(30),
DefaultPolicy = EntitlementFailurePolicy.FailClosed,
FeaturePolicies = new Dictionary<string, EntitlementFailurePolicy>
{
["basic.view"] = EntitlementFailurePolicy.FailOpen,
["reports.export"] = EntitlementFailurePolicy.UseLastKnown(TimeSpan.FromMinutes(5)),
},
});
var decision = await cache.CheckAsync("bankweave", "Production", tenantId, "reports.export");
if (!decision.Allowed) return Results.StatusCode(402);decision.Source tells you where the answer came from (Fresh, Cache, Stale, LastKnown, PolicyFailOpen, PolicyFailClosed, TenantNotFound) and decision.Age how old it is, so you can log or show it.
What happens in each situation
| Situation | Result |
|---|---|
Answer younger than Ttl | Returned from the cache. No network call. |
Older than Ttl, inside StaleWhileRevalidate | Returned at once; one background refresh per tenant. |
| Older than that, platform answers | Fresh answer, cached. |
| Platform down (network error, timeout, 5xx, 408, 429) | The feature's policy decides. |
| Platform says the tenant does not exist | Denied. A fail-open policy does not apply. |
| Platform says 401, 403 or another 4xx | The call throws. A wrong key is never treated as an outage. |
Choosing a policy
- Fail closed (default): deny. Use for anything the customer pays for.
- Fail open: allow. Use only where a wrong "yes" costs you nothing, such as read-only views.
- Use last known: allow or deny as the last answer said, but only while that answer is younger than the age you give, and never older than
AbsoluteMaxStale(default 1 hour). The entitlement contract asks you to deny billing-controlled features once a snapshot is past its validity, so choosing this policy is an explicit decision to accept a bounded delay for that feature.
Share answers across servers
The default store is per process. To share answers across a fleet, and keep them through a platform outage, implement IEntitlementCacheStore (three methods: get, set with retention, remove) over Redis or your database and pass it in. Entries hold only the application slug, environment, tenant id and the list of enabled feature keys.
Tenant isolation
Entries are keyed by application, environment and tenant, and a tenant id is required. An entry read from a shared store is discarded unless the application, environment and tenant stored inside it match the request.
Drop an answer when billing changes
Call cache.Invalidate(app, environment, tenantId) from your entitlement.changed webhook handler. The next check goes to the platform.
Metrics
Meter TenancyEngine.Sdk.Entitlements: te.entitlements.cache.hit, .miss, .stale_serve, .outage, .policy_decision. Hit rate is hit / (hit + miss).