Cloud

Quota Management

Per-tenant resource usage tracking and limit enforcement.

2 min readDocumentationEdit this page

Overview

The quota manager tracks per-tenant resource consumption and enforces configurable limits with automatic period rollover. It emits warning and exceeded events on the global event bus.

Configuration

typescript
import { quotas } from '@axiljs/cloud'
 
quotas.configure({
  tenantId: 'acme-corp',
  limits: [
    { metric: 'api_calls',     max: 100_000,          period: 'monthly' },
    { metric: 'storage_bytes', max: 5_000_000_000,    period: 'lifetime' },
    { metric: 'users',         max: 500,              period: 'lifetime' },
  ],
})

Tracking Usage

typescript
const allowed = quotas.increment('acme-corp', 'api_calls')
 
if (!allowed) {
  res.status(429).json({ error: 'API quota exceeded' })
  return
}

Checking Without Incrementing

typescript
if (!quotas.check('acme-corp', 'api_calls')) {
  // Quota exhausted — block the request
}

Usage Reports

typescript
const usage = quotas.getUsage('acme-corp')
// [
//   { metric: 'api_calls', current: 45200, limit: 100000, percentUsed: 45, period: 'monthly' },
//   { metric: 'users', current: 120, limit: 500, percentUsed: 24, period: 'lifetime' },
// ]

Event Notifications

typescript
import { getGlobalEventBus } from '@axiljs/events'
 
// Warning at 90% usage
getGlobalEventBus().on('quota.warning', ({ tenantId, metric, percentUsed }) => {
  notifyAdmin(`${tenantId} is at ${percentUsed}% of ${metric}`)
})
 
// Hard limit exceeded
getGlobalEventBus().on('quota.exceeded', ({ tenantId, metric, current, limit }) => {
  alertTeam(`${tenantId} exceeded ${metric}: ${current}/${limit}`)
})

Supported Metrics

MetricDescription
api_callsNumber of API requests
storage_bytesStorage consumption
usersNumber of user accounts
projectsNumber of projects
customAny custom metric

Periods

PeriodRollover
dailyEvery 24 hours
monthlyEvery 30 days
lifetimeNever resets

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY