Project: ClickHouse (App Definition and Development / Database)
File: workload-scheduling.md

slug: /en/operations/workload-scheduling sidebar_position: 69 sidebar_label: "Workload scheduling" title: "Workload scheduling" When ClickHouse execute multiple queries simultaneously, they may be using shared resources (e.g. disks). Scheduling constraints and policies can be applied to regulate how resources are utilized and shared between different workloads. For every resource a scheduling hierarchy can be configured. Hierarchy root represents a resource, while leafs are queues, holding requests that exceed resource capacity. :::note Currently only remote disk IO can be scheduled using described method. For CPU scheduling see settings about thread pools and . For flexible memory limits see ::: To enable IO scheduling for a specific disk, you have to specify `readresource` and/or `writeresource` in storage configuration. It says ClickHouse what resource should be used for every read and write requests with given disk. Read and write resource can refer to the same resource name, which is useful for local SSDs or HDDs. Multiple different disks also can refer to the same resource, which is useful for remote disks: if you want to be able to allow fair division of network bandwidth between e.g. "production" and "development" workloads. Example: ```xml <clickhouse> <storage_configuration> ... <disks> <s3> <type>s3</type> <endpoint>https://clickhouse-public-datasets.s3.amazonaws.com/my-bucket/root-path/</endpoint> <accesskeyid>youraccesskeyid</accesskey_id> <secretaccesskey>yoursecretaccesskey</secretaccess_key> <readresource>networkread</read_resource> <writeresource>networkwrite</write_resource> </s3> </disks> <policies> <s3_main> <volumes> <main> <disk>s3</disk> </main> </volumes> </s3_main> </policies> </storage_configuration> </clickhouse> ``` Queries can be marked with setting `workload` to distinguish different workloads. If `workload` is not set, than value "default" is used. Note that you are able to specify the other value using settings profiles. Setting constraints can be used to make `workload` constant if you want all queries from the user to be marked with fixed value of `workload` setting. Let's consider an example of a system with two different workloads: "production" and "development". ```sql SELECT count() FROM my_table WHERE value = 42 SETTINGS workload = 'production' SELECT count() FROM my_table WHERE value = 13 SETTINGS workload = 'development' ``` From the standpoint of scheduling subsystem a resource represents a hierarchy of scheduling

```mermaid graph TD subgraph network_read nr_root(("/")) -->|100 concurrent requests| nr_fair("fair") -->|75% bandwidth| nr_prod["prod"] nr_fair -->|25% bandwidth| nr_dev["dev"] end subgraph network_write nw_root(("/")) -->|100 concurrent requests| nw_fair("fair") -->|75% bandwidth| nw_prod["prod"] nw_fair -->|25% bandwidth| nw_dev["dev"] end ``` Possible node types: `inflightlimit` (constraint) - blocks if either number of concurrent in-flight requests exceeds `maxrequests`, or their total cost exceeds `max_cost`; must have a single child. `bandwidthlimit` (constraint) - blocks if current bandwidth exceeds `maxspeed` (0 means unlimited) or burst exceeds `maxburst` (by default equals `maxspeed`); must have a single child. `fair` (policy) - selects the next request to serve from one of its children nodes according to max-min fairness; children nodes can specify `weight` (default is 1). `priority` (policy) - selects the next request to serve from one of its children nodes according to static priorities (lower value means higher priority); children nodes can specify `priority` (default is 0). `fifo` (queue) - leaf of the hierarchy capable of holding requests that exceed resource capacity. To be able to use the full capacity of the underlying resource, you should use `inflightlimit`. Note that a low number of `maxrequests` or `maxcost` could lead to not full resource utilization, while too high numbers could lead to empty queues inside the scheduler, which in turn will result in policies being ignored (unfairness or ignoring of priorities) in the subtree. On the other hand, if you want to protect resources from too high utilization, you should use `bandwidthlimit`. It throttles when the amount of resource consumed in `duration` seconds exceeds `maxburst + maxspeed * duration` bytes. Two `bandwidth_limit` nodes on the same resource could be used to limit peak bandwidth during short intervals and average bandwidth for longer ones. The following example shows how to define IO scheduling hierarchies shown in the picture: ```xml <clickhouse> <resources> <network_read> <node path="/"> <type>inflight_limit</type> <maxrequests>100</maxrequests> </node> <node path="/fair"> <type>fair</type> </node> <node path="/fair/prod"> <type>fifo</type> <weight>3</weight> </node> <node path="/fair/dev"> <type>fifo</type> </node> </network_read> <network_write> <node path="/"> <type>inflight_limit</type> <maxrequests>100</maxrequests> </node> <node path="/fair"> <type>fair</type> </node> <node path="/fair/prod"> <type>fifo</type> <weight>3</weight> </node> <node path="/fair/dev"> <type>fifo</type> </node> </network_write> </resources> </clickhouse> ``` Workload classifiers are used to define mapping from `workload` specified by a query into leaf-queues that should be used for specific resources. At the moment, workload classification is simple: only static mapping is available. Example: ```xml <clickhouse> <workload_classifiers> <production> <networkread>/fair/prod</networkread> <networkwrite>/fair/prod</networkwrite> </production> <development> <networkread>/fair/dev</networkread> <networkwrite>/fair/dev</networkwrite> </development> <default> <networkread>/fair/dev</networkread> <networkwrite>/fair/dev</networkwrite> </default> </workload_classifiers> </clickhouse> ```