# Quota Service

# Quota Service (Port 6380) {#quota-service-port-6380}


The **quota-service** is a high-performance service responsible for real-time tracking of resource usage and enforcing plan-based limits across the MQUAD platform.


## Infrastructure {#infrastructure}


- **Port**: 6380 (Internal & External)
- **Primary Cache**: Redis (Shared with core services)
- **Mode**: Real-time counter-based enforcement


## Core Responsibility {#core-responsibility}


The service tracks consumption for key resources:
- **Surveys**: Total count of surveys per tenant.
- **Responses**: Monthly submission counts.
- **Storage**: Total binary storage used (MB).
- **Webhooks**: Count of active webhook subscriptions.
- **Team Members**: Count of active users per tenant.


## Integration Flow {#integration-flow}


1. A domain service (e.g., `data-service`) receives a request (e.g., `POST /responses`).
2. The domain service calls `GET /check-quota/{tenant_id}/{feature_key}` on the quota-service.
3. The quota-service retrieves the tenant's plan tier from `feature-flags-service`.
4. The quota-service checks the current usage counter in Redis.
5. If under the limit, a success code is returned; otherwise, a `403 Forbidden` (Quota Exceeded) is issued.
6. After the action is completed, the domain service calls `POST /increment/{tenant_id}/{feature_key}` to update the count.


## Redis Counter Keys {#redis-counter-keys}


Usage counters are stored in Redis using the following pattern:
- `quota:{tenant_id}:{feature_key}:{period_suffix}`

Example: `quota:550e8400-e29b-41d4-a716-446655440000:responses:2025-05`


## Key Endpoints {#key-endpoints}


| Method | Path | Purpose |
|--------|------|---------|
| GET | `/check/{tenant_id}/{key}` | Check if tenant has remaining quota |
| POST | `/increment/{tenant_id}/{key}` | Increment usage for a specific feature |
| POST | `/decrement/{tenant_id}/{key}` | Decrement usage (e.g., on survey deletion) |
| GET | `/usage/{tenant_id}` | Get summary of all usage for a tenant |
| POST | `/reset/{tenant_id}` | Manual reset of tenant quotas (e.g., on plan upgrade) |


## Configuration {#configuration}


Standard plan limits are managed via the `feature-flags-service`, but the quota-service handles the atomic increment/decrement logic to ensure consistency across distributed microservice instances.
