# Resource Limiting

> The Things Stack supports setting maximum limits for active resources. Access to each resource is limited by a unique identifier key, and it is possible to …

Source: https://www.thethingsindustries.com/docs/enterprise/management/resource-limiting/


The Things Stack supports setting maximum limits for active resources. Access to each resource is limited by a unique identifier key, and it is possible to define resource limiting classes for fine-grained control.

## Example configuration

The resource limiting configuration is split into multiple profiles. For each profile, we provide a name and the maximum number of concurrent resources allowed rate. The profile is associated with a number of resource limiting classes. Refer to the [Limited Entities](#limited-entities) section below for a list of all available resource limiting classes and what they mean.

Enable resource limiting by adding the following configuration to your `ttn-lw-stack.yml`.

Resource limiting can be applied per application, as well as for all applications in a tenant. The values shown below are only meant as an example. Make sure to adjust them accordingly, depending on the actual traffic of your deployment.

```yaml
resource-limiting:
  profiles:
    - name: Limit application data plane connections (MQTT and gRPC)
      max-concurrent: 50
      associations:
        - as:conn
    - name: Override limit for application `test-app-1` of tenant `test-tenant-1`
      max-concurrent: 100
      associations:
        - as:conn:app:test-app-1@test-tenant-1
    - name: Override limit for all applications of tenants `test-tenant-2` and `test-tenant-3`
      max-concurrent: 200
      associations:
        - as:conn:tenant:test-tenant-2
        - as:conn:tenant:test-tenant-3
```

## Limited Entities


<div class="has-vertical-space">
  <a href="#application-server-data-plane-connections">
    <h3 id="application-server-data-plane-connections">Application Server Data Plane Connections</h3>
  </a>
  <table class="fixed">
    <tbody>
      <tr>
        <td><b>Description</b></td><td>Limit number of concurrent Application Server data plane connections by application and frontend.</td>
      </tr>
      <tr>
        <td><b>Classes</b></td><td><code>as:conn:app:{{ AppUID }}&lt;/code&gt; &lt;code&gt;as:conn:tenant:{{ TenantID }}&lt;/code&gt; &lt;code&gt;as:conn:frontend:{{ Frontend }}&lt;/code&gt; &lt;code&gt;as:conn</code></td>
      </tr>
      <tr>
        <td><b>Key</b></td><td><code>as:conn:frontend:{{ Frontend }}:app:{{ AppUID }}</code></td>
      </tr>
      <tr>
        <td><b>Example</b></td><td><code>as:conn:frontend:mqtt:app:app-1@tenant-1</code></td>
      </tr>
    </tbody>
  </table>
</div>


For resources where multiple classes are defined, the first matching profile will be used. For example, if profile `A` is associated with class `as:conn` and profile `B` is associated with `as:conn:app:my-application@my-tenant`, then connections for `my-application@my-tenant` will use the limits from profile `B`.

## Resource Limiting Actions

The following table describes how The Things Stack reacts when the maximum resource limits are exceeded.

| Resource                     | Resource Limit Action                                                         | Expected Client Action                    |
| ---------------------------- | ----------------------------------------------------------------------------- | ----------------------------------------- |
| gRPC Application Connections | Calls to `Subscribe` RPC will fail with an error of type `ResourceExhausted`. | Terminate existing connections and retry. |
| MQTT Application Connections | A `ServerUnavailable` response is returned for new connections.               | Terminate existing connections and retry. |

For example, if an MQTT connection is failing due to resource limits exceeding, you will see an error such as `Resource limit 16 exceeded for resource with key as:conn:frontend:mqtt:app:test-app-1@test-tenant-1`. The resolution would be to terminate existing connections and retry.

## External configuration

It is possible to load extra resource limiting profiles from an external file. Create a file named `/opt/config/resource-limiting.yml` (replace `/opt/config` with any path you like) with the following contents:

```yaml
# If a profile is configured in `ttn-lw-stack.yml` for `as:conn`, then
# it will be overriden
profiles:
  - name: Example profile
    max-concurrent: 5
    associations: [as:conn]
```

Then add the following config to `ttn-lw-stack.yml` (replace `/opt/config` with your path):

```yaml
resource-limiting:
  # This will read profiles from /opt/config/resource-limiting.yml
  config-source: directory
  directory: /opt/config
```

