> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Admission Control

CockroachDB supports an admission control system to maintain cluster performance and availability when some nodes experience high load. When admission control is enabled, CockroachDB sorts request and response operations into work queues by priority, giving preference to higher priority operations. Internal operations critical to node health, like [node liveness heartbeats](https://docs.cockroachlabs.com/docs/v24.2/cluster-setup-troubleshooting#node-liveness-issues), are high priority. The admission control system also prioritizes transactions that hold [locks](https://docs.cockroachlabs.com/docs/v24.2/crdb-internal#cluster_locks), to reduce [contention](https://docs.cockroachlabs.com/docs/v24.2/performance-best-practices-overview#transaction-contention) and release locks earlier.

## How admission control works

At a high level, the admission control system works by queueing requests to use the following system resources:

* CPU
* Storage IO (writes to disk)

For CPU, different types of usage are queued differently based on priority to allow important work to make progress even under [high CPU utilization](https://docs.cockroachlabs.com/docs/v24.2/common-issues-to-monitor#cpu-usage).

For storage IO, the goal is to prevent the [storage layer's log-structured merge tree](https://docs.cockroachlabs.com/docs/v24.2/architecture/storage-layer#log-structured-merge-trees) (LSM) from experiencing high [read amplification](https://docs.cockroachlabs.com/docs/v24.2/architecture/storage-layer), which slows down reads, while also maintaining the ability to absorb bursts of writes.

Admission control works on a per-[node](https://docs.cockroachlabs.com/docs/v24.2/architecture/overview#node) basis, since even though a large CockroachDB cluster may be well-provisioned as a whole, individual nodes are stateful and may experience performance [hot spots](https://docs.cockroachlabs.com/docs/v24.2/performance-best-practices-overview#hot-spots).

For more details about how the admission control system works, see:

* The blog post [Here's how CockroachDB keeps your database from collapsing under load](https://www.cockroachlabs.com/blog/admission-control-in-cockroachdb/).

## Use cases for admission control

A well-provisioned CockroachDB cluster may still encounter performance bottlenecks at the node level, as stateful nodes can develop [hot spots](https://docs.cockroachlabs.com/docs/v24.2/performance-best-practices-overview#hot-spots) that last until the cluster rebalances itself. When hot spots occur, they should not cause failures or degraded performance for important work.

This is particularly important for CockroachDB Standard and CockroachDB Basic, where one user tenant cluster experiencing high load should not degrade the performance or availability of a different, isolated tenant cluster running on the same host.

Admission control can help if your cluster has degraded performance due to the following types of node overload scenarios:

* The node has more than 32 runnable goroutines per CPU, visible in the **Runnable goroutines per CPU** graph in the [**Overload** dashboard](https://docs.cockroachlabs.com/docs/v24.2/ui-overload-dashboard#runnable-goroutines-per-cpu).
* The node has a high amount of overload in the Pebble LSM tree, visible in the **IO Overload** graph in the [**Overload** dashboard](https://docs.cockroachlabs.com/docs/v24.2/ui-overload-dashboard#io-overload).
* The node has high CPU usage, visible in the **CPU Utilization** graph in the [**Overload** dashboard](https://docs.cockroachlabs.com/docs/v24.2/ui-overload-dashboard#cpu-utilization).
* The node is experiencing out-of-memory errors, visible in the **Memory Usage** graph in the [**Hardware** dashboard](https://docs.cockroachlabs.com/docs/v24.2/ui-hardware-dashboard#memory-usage). Even though admission control does not explicitly target controlling memory usage, it can reduce memory usage as a side effect of delaying the start of operation execution when the CPU is overloaded.

## Operations subject to admission control

Almost all database operations that use CPU or perform storage IO are controlled by the admission control system. From a user's perspective, specific operations that are affected by admission control include:

* [General SQL queries](https://docs.cockroachlabs.com/docs/v24.2/selection-queries) have their CPU usage subject to admission control, as well as storage IO for writes to [leaseholder replicas](https://docs.cockroachlabs.com/docs/v24.2/architecture/replication-layer#leases).
* [Bulk data imports](https://docs.cockroachlabs.com/docs/v24.2/import-into).
* [Backups](https://docs.cockroachlabs.com/docs/v24.2/backup-and-restore-overview).
* [Schema changes](https://docs.cockroachlabs.com/docs/v24.2/online-schema-changes), including index and column backfills (on both the [leaseholder replica](https://docs.cockroachlabs.com/docs/v24.2/architecture/replication-layer#leases) and [follower replicas](https://docs.cockroachlabs.com/docs/v24.2/architecture/replication-layer#raft)).
* [`COPY`](https://docs.cockroachlabs.com/docs/v24.2/copy) statements.
* [Deletes](https://docs.cockroachlabs.com/docs/v24.2/delete-data) (including deletes initiated by [row-level TTL jobs](https://docs.cockroachlabs.com/docs/v24.2/row-level-ttl); the [selection queries](https://docs.cockroachlabs.com/docs/v24.2/selection-queries) performed by TTL jobs are also subject to CPU admission control).
* [Follower replication work](https://docs.cockroachlabs.com/docs/v24.2/architecture/replication-layer#raft).
* [Raft log entries being written to disk](https://docs.cockroachlabs.com/docs/v24.2/architecture/replication-layer#raft).
* [Changefeeds](https://docs.cockroachlabs.com/docs/v24.2/create-and-configure-changefeeds).
* [Intent resolution](https://docs.cockroachlabs.com/docs/v24.2/architecture/transaction-layer#write-intents).

The following operations are not subject to admission control:

* SQL writes are not subject to admission control on [follower replicas](https://docs.cockroachlabs.com/docs/v24.2/architecture/replication-layer#raft) by default, unless those writes occur in transactions that are subject to a Quality of Service (QoS) level as described in [Set quality of service level for a session](#set-quality-of-service-level-for-a-session). In order for writes on follower replicas to be subject to admission control, the setting `default_transaction_quality_of_service=background` must be used.

> [!NOTE]
>
> Admission control is beneficial when overall cluster health is good but some nodes are experiencing overload. If you see these overload scenarios on many nodes in the cluster, that typically means the cluster needs more resources.

## Enable and disable admission control

Admission control is enabled by default. To enable or disable admission control, use the following [cluster settings](https://docs.cockroachlabs.com/docs/v24.2/cluster-settings):

* `admission.kv.enabled` for work performed by the [KV layer](https://docs.cockroachlabs.com/docs/v24.2/architecture/distribution-layer).
* `admission.sql_kv_response.enabled` for work performed in the SQL layer when receiving [KV responses](https://docs.cockroachlabs.com/docs/v24.2/architecture/distribution-layer).
* `admission.sql_sql_response.enabled` for work performed in the SQL layer when receiving [DistSQL responses](https://docs.cockroachlabs.com/docs/v24.2/architecture/sql-layer#distsql).

When you enable or disable admission control settings for one layer, Cockroach Labs recommends that you enable or disable them for **all layers**.

## Work queues and ordering

When admission control is enabled, request and response operations are sorted into work queues where the operations are organized by priority and transaction start time.

Higher priority operations are processed first. The criteria for determining higher and lower priority operations is different at each processing layer, and is determined by the CPU and storage I/O of the operation. Write operations in the [KV storage layer](https://docs.cockroachlabs.com/docs/v24.2/architecture/storage-layer) in particular are often the cause of performance bottlenecks, and admission control prevents [the Pebble storage engine](https://docs.cockroachlabs.com/docs/v24.2/architecture/storage-layer#pebble) from experiencing high [read amplification](https://docs.cockroachlabs.com/docs/v24.2/architecture/storage-layer). Critical cluster operations like node heartbeats are processed as high priority, as are transactions that hold [locks](https://docs.cockroachlabs.com/docs/v24.2/crdb-internal#cluster_locks) in order to avoid [contention](https://docs.cockroachlabs.com/docs/v24.2/performance-recipes#transaction-contention) and release locks earlier.

The transaction start time is used within the priority queue and gives preference to operations with earlier transaction start times. For example, within the high priority queue operations with an earlier transaction start time are processed first.

### Set quality of service level for a session

In an overload scenario where CockroachDB cannot service all requests, you can identify which requests should be prioritized. This is often referred to as *quality of service* (QoS). Admission control queues work throughout the system. To set the quality of service level on the admission control queues on behalf of SQL requests submitted in a session, use the [`default_transaction_quality_of_service`](https://docs.cockroachlabs.com/docs/v24.2/set-vars) session variable. The valid values are `critical`, `background`, and `regular`. Admission control must be enabled for this setting to have an effect.

To increase the priority of subsequent SQL requests, run:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET default_transaction_quality_of_service=critical;
```

To decrease the priority of subsequent SQL requests, run:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET default_transaction_quality_of_service=background;
```

To reset the priority to the default session setting (in between background and critical), run:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET default_transaction_quality_of_service=regular;
```

The quality of service for [`COPY`](https://docs.cockroachlabs.com/docs/v24.2/copy) statements is configured separately with the [`copy_transaction_quality_of_service`](https://docs.cockroachlabs.com/docs/v24.2/set-vars) session variable, which defaults to `background`.

To increase the priority of subsequent `COPY` statements, run:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET copy_transaction_quality_of_service=critical;
```

### Set quality of service level for a transaction

To set the quality of service level for a single [transaction](https://docs.cockroachlabs.com/docs/v24.2/transactions), set the [`default_transaction_quality_of_service`](https://docs.cockroachlabs.com/docs/v24.2/set-vars) and/or [`copy_transaction_quality_of_service`](https://docs.cockroachlabs.com/docs/v24.2/set-vars) session variable for just that transaction using the [`SET LOCAL`](https://docs.cockroachlabs.com/docs/v24.2/set-vars#set-a-variable-for-the-duration-of-a-single-transaction) statement inside a [`BEGIN`](https://docs.cockroachlabs.com/docs/v24.2/begin-transaction) ... [`COMMIT`](https://docs.cockroachlabs.com/docs/v24.2/commit-transaction) block, as shown in the following example. The valid values are `critical`, `background`, and `regular`.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
BEGIN;
SET LOCAL default_transaction_quality_of_service = 'regular'; -- Edit transaction QoS to desired level
SET LOCAL copy_transaction_quality_of_service = 'regular'; -- Edit COPY QoS to desired level
-- Statements to run in this transaction go here
COMMIT;
```

## Known limitations

Admission control works on the level of each node, not at the cluster level. The admission control system queues requests until the operations are processed or the request exceeds the timeout value (for example by using [`SET statement_timeout`](https://docs.cockroachlabs.com/docs/v24.2/set-vars#supported-variables)). If you specify aggressive timeout values, the system may operate correctly but have low throughput as the operations exceed the timeout value while only completing part of the work. There is no mechanism for preemptively rejecting requests when the work queues are long.

Organizing operations by priority can mean that higher priority operations consume all the available resources while lower priority operations remain in the queue until the operation times out.

## Considerations

[Client connections](https://docs.cockroachlabs.com/docs/v24.2/connection-parameters) are not managed by the admission control subsystem. Too many connections per [gateway node](https://docs.cockroachlabs.com/docs/v24.2/architecture/sql-layer) can also lead to cluster overload.

To control the maximum number of non-superuser ([`root`](https://docs.cockroachlabs.com/docs/v24.2/security-reference/authorization) user or other [`admin` role](https://docs.cockroachlabs.com/docs/v24.2/security-reference/authorization#admin-role)) connections a [gateway node](https://docs.cockroachlabs.com/docs/v24.2/architecture/sql-layer) can have open at one time, use the `server.max_connections_per_gateway` [cluster setting](https://docs.cockroachlabs.com/docs/v24.2/cluster-settings).  If a new non-superuser connection would exceed this limit, the error message `"sorry, too many clients already"` is returned, along with error code `53300`.

## Observe admission control performance

The [DB Console Overload dashboard](https://docs.cockroachlabs.com/docs/v24.2/ui-overload-dashboard) shows metrics related to the performance of the admission control system.

## See also

* The [Overload Dashboard](https://docs.cockroachlabs.com/docs/v24.2/ui-overload-dashboard) in the [DB Console](https://docs.cockroachlabs.com/docs/v24.2/ui-overview).
* The blog post [Here's how CockroachDB keeps your database from collapsing under load](https://www.cockroachlabs.com/blog/admission-control-in-cockroachdb/).
* The blog post [Rubbing Control Theory on the Go scheduler](https://www.cockroachlabs.com/blog/rubbing-control-theory/).
