> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Controlling behavior on server CPU overload.

# Server overload

<h2 id="overview">
  Overview
</h2>

Sometimes server can become overloaded due to different reasons. In order to determine the current CPU overload,
ClickHouse server calculates the ratio of CPU wait time (`OSCPUWaitMicroseconds` metric) to busy time
(`OSCPUVirtualTimeMicroseconds` metric). When the server is overloaded above certain ratio,
it makes sense to discard some queries or even drop connection requests to not increase the load even more.

There's a server setting `os_cpu_busy_time_threshold` which controls the minimum busy time to consider CPU
doing some useful work. If the current value of `OSCPUVirtualTimeMicroseconds` metric is below this value,
CPU overload is assumed to be 0.

<h2 id="rejecting-queries">
  Rejecting queries
</h2>

The behavior of rejecting queries is controlled by query-level settings `min_os_cpu_wait_time_ratio_to_throw` and
`max_os_cpu_wait_time_ratio_to_throw`. If those settings are set and `min_os_cpu_wait_time_ratio_to_throw` is less
than `max_os_cpu_wait_time_ratio_to_throw`, then the query is rejected and `SERVER_OVERLOADED` error is thrown
with some probability is the overload ratio is at least `min_os_cpu_wait_time_ratio_to_throw`. The probability
is determined as a linear interpolation between min and max ratios. For example, if `min_os_cpu_wait_time_ratio_to_throw = 2`,
`max_os_cpu_wait_time_ratio_to_throw = 6`, and `cpu_overload = 4`, then the query will be rejected with a probability of `0.5`.

<h2 id="dropping-connections">
  Dropping connections
</h2>

Dropping connections is controlled by server-level settings `min_os_cpu_wait_time_ratio_to_drop_connection` and
`max_os_cpu_wait_time_ratio_to_drop_connection`. Those settings can be changed without server restart. The idea behind
those settings is similar to the one with rejecting queries. The only difference in this case is if the server is overloaded,
the connection attempt will be rejected from the server side.

<h2 id="resource-overload-warnings">
  Resource overload warnings
</h2>

ClickHouse also logs CPU and Memory overload warnings to `system.warnings` table when the server is overloaded. You can
customize these thresholds through server configuration.

**Example**

```xml theme={null}

<resource_overload_warnings>
    <cpu_overload_warn_ratio>0.9</cpu_overload_warn_ratio>
    <cpu_overload_clear_ratio>0.8</cpu_overload_clear_ratio>
    <cpu_overload_duration_seconds>600</cpu_overload_duration_seconds>
    <memory_overload_warn_ratio>0.9</memory_overload_warn_ratio>
    <memory_overload_clear_ratio>0.8</memory_overload_clear_ratio>
    <memory_overload_duration_seconds>600</memory_overload_duration_seconds>
</resource_overload_warnings>
```
