---
name: clickhouse-rate-limits
description: |-
  Configure ClickHouse query concurrency, memory quotas, and connection limits.
  Use when hitting "too many simultaneous queries", managing concurrent users,
  or tuning server-side resource limits so an app never starves the cluster.
  Trigger with "clickhouse rate limit", "clickhouse concurrency", "clickhouse quota",
  "too many simultaneous queries", "clickhouse connection limit".
allowed-tools: Read, Write, Edit
version: 1.7.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
tags:
  - saas
  - database
  - analytics
  - clickhouse
  - olap
compatibility: Designed for Claude Code
title: clickhouse-rate-limits
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits
last_updated: 2026-10-07T06:06:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/examples.md), [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/implementation.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/clickhouse-rate-limits`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/clickhouse-rate-limits`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/clickhouse-rate-limits --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/clickhouse-rate-limits --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# ClickHouse Rate Limits & Concurrency

## Overview

ClickHouse has no REST API rate limits like a SaaS product. Instead it enforces
server-side concurrency limits, memory quotas, and per-user settings that
control resource usage. This skill configures those server-side limits and pairs
them with client-side controls so an application stays within them under load.

## Prerequisites

- ClickHouse admin access (or Cloud console) to create quotas and settings profiles.
- The `@clickhouse/client` Node package for the client-side patterns.
- A rough target for peak concurrent queries and per-query memory.

## Instructions

Work top-down: cap resources at the server, then make the client respect the cap.

### Step 1: Know the server-side limits

The defaults you tune most often:

| Setting | Default | Controls |
|---------|---------|----------|
| `max_concurrent_queries` | 100 | Queries running simultaneously |
| `max_connections` | 4096 | Max TCP/HTTP connections |
| `max_memory_usage` | ~10GB | Per-query memory |
| `max_execution_time` | 0 (unlimited) | Per-query timeout (seconds) |

ClickHouse Cloud's management API (not the query interface) is separately limited
to 10 requests per 10 seconds. Full table in
[references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/implementation.md).

### Step 2: Cap resources per user (essential skeleton)

Bind a quota and a settings profile to each application user:

```sql
CREATE SETTINGS PROFILE IF NOT EXISTS app_profile
    SETTINGS
        max_memory_usage = 5000000000,       -- 5GB per query
        max_execution_time = 30,             -- 30s timeout
        max_concurrent_queries_for_user = 10 -- 10 parallel queries
    TO app_user;
```

The full quota (`CREATE QUOTA … FOR INTERVAL 1 HOUR MAX …`) plus verification
queries are in [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/implementation.md).

### Step 3: Make the client respect the cap

Four client-side patterns keep the app inside the server limits — connection
pooling, an app-level concurrency queue (`p-queue`), retry-with-backoff on
`TOO_MANY_SIMULTANEOUS_QUERIES`, and insert buffering to avoid `TOO_MANY_PARTS`.
Each is a drop-in TypeScript snippet in
[references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/implementation.md), with the
concurrency queue as the smallest starting point:

```typescript
import PQueue from 'p-queue';
const queryQueue = new PQueue({ concurrency: 5, timeout: 30_000, throwOnTimeout: true });
const rateLimitedQuery = <T>(sql: string) =>
  queryQueue.add(async () => (await client.query({ query: sql, format: 'JSONEachRow' })).json<T>());
```

### Step 4: Monitor and verify

Watch live concurrency and confirm limits bind with the queries in
[references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/examples.md) (`system.processes`,
`system.metrics`, `system.query_log`, `SHOW QUOTAS`).

## Output

Applying this skill produces:

- A ClickHouse **settings profile** and **quota** bound to the app user, capping
  per-query memory, timeout, and per-user concurrency.
- **Client-side guardrails** — a bounded connection pool, a concurrency queue, a
  retry wrapper, and an insert buffer — so the app cannot exceed the server cap.
- **Monitoring queries** that report current running queries, queue depth, and
  historical peak concurrency, plus a check that the quota is applied.

## Error Handling

| Error | Code | Solution |
|-------|------|----------|
| `TOO_MANY_SIMULTANEOUS_QUERIES` | 202 | Reduce client concurrency or raise `max_concurrent_queries`; retry with backoff |
| `MEMORY_LIMIT_EXCEEDED` | 241 | Lower `max_threads`, add query filters, reduce `max_memory_usage` scope |
| `TIMEOUT_EXCEEDED` | 159 | Increase `max_execution_time` or optimize the query |
| `TOO_MANY_PARTS` | 252 | Batch inserts via the insert buffer, wait for merges |

The retry wrapper in [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/implementation.md)
treats codes 202, 159, and network errors as retryable.

## Examples

Three worked end-to-end scenarios live in
[references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/examples.md):

1. **Cap a reporting service at 5 concurrent queries** — wrap every dashboard
   tile's query in the `p-queue` limiter.
2. **Survive a concurrency spike** — `queryWithRetry` absorbs code-202 bursts
   with exponential backoff + jitter instead of returning 500s.
3. **High-throughput ingest without `TOO_MANY_PARTS`** — `InsertBuffer` batches
   a firehose into a few large inserts.

## Resources

- [Server Settings](https://clickhouse.com/docs/operations/server-configuration-parameters/settings)
- [Query Complexity Limits](https://clickhouse.com/docs/operations/settings/query-complexity)
- [Quotas](https://clickhouse.com/docs/operations/quotas)
- [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/implementation.md) — full server + client code
- [references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-rate-limits/references/examples.md) — monitoring queries + worked scenarios

## Next Steps

For security hardening (users, roles, TLS), see the `clickhouse-security-basics`
skill. For query-level performance work, see `clickhouse-performance-tuning`.
