content/shared/influxdb3-query-guides/query-timeout-best-practices.md
Learn how to set appropriate query timeouts for InfluxDB 3 to balance performance and resource protection.
Query timeouts prevent resource monopolization while allowing legitimate queries to complete successfully. The key is finding the "goldilocks zone"—timeouts that are not too short (causing legitimate queries to fail) and not too long (allowing runaway queries to monopolize resources).
Query timeouts define the maximum duration a query can run before being canceled. In {{% product-name %}}, timeouts serve multiple purposes:
Query execution includes network latency, query planning, data retrieval, processing, and result serialization.
Optimal timeouts are:
InfluxDB 3 uses round-robin query routing to balance load across multiple queriers. This creates a "checkout line" effect that influences timeout strategy.
[!Note]
Concurrent query execution
InfluxDB 3 supports concurrent query execution, which helps minimize the impact of intensive or inefficient queries. However, you should still use appropriate timeouts and optimize your queries for best performance.
Consider a grocery store with multiple checkout lines:
If one querier is unhealthy or has been hijacked by a "noisy neighbor" query (excessively resource hungry), giving up sooner may save time--it's like jumping to a cashier with no customers in line. However, if all queriers are overloaded, then short retries may exacerbate the problem--you wouldn't jump to the end of another line if the cashier is already starting to scan your items.
In distributed systems:
Configure timeouts that can be modified without service restarts using environment variables, configuration files, runtime APIs, or per-query overrides. Design your client applications to easily adjust timeouts on the fly, allowing you to respond quickly to performance changes and test different timeout strategies without code changes.
See the InfluxDB 3 client library examples for how to configure timeouts in Python.
Implement different timeout classes based on query characteristics.
{{% hide-in "cloud-serverless" %}}
| Query Type | Recommended Timeout | Use Case | Rationale |
|---|---|---|---|
| UI and dashboard | 10 seconds | Interactive dashboards, real-time monitoring | Users expect immediate feedback |
| Generic default | 60 seconds | Application queries, APIs | Balances performance and reliability |
| Mixed workload | 2 minutes | Development, testing environments | Accommodates various query types |
| Analytical and background | 5 minutes | Reports, batch processing, ETL operations | Complex queries need more time |
| {{% /hide-in %}} |
{{% show-in "cloud-serverless" %}}
| Query Type | Recommended Timeout | Use Case | Rationale |
|---|---|---|---|
| UI and dashboard | 10 seconds | Interactive dashboards, real-time monitoring | Users expect immediate feedback |
| Generic default | 30 seconds | Application queries, APIs | Serverless optimized for shorter queries |
| Mixed workload | 60 seconds | Development, testing environments | Limited by serverless execution model |
| Analytical and background | 2 minutes | Reports, batch processing | Complex queries within serverless limits |
| {{% /show-in %}} |
{{% show-in "enterprise, core" %}}
[!Tip]
Use caching
Where immediate feedback is crucial, consider using Last Value Cache to speed up queries for recent values and Distinct Value Cache to speed up queries for distinct values. {{% /show-in %}}
Consider using more sophisticated retry strategies rather than simple fixed retries:
Consider these indicators that timeouts may need adjustment:
When introducing a new query to your application or when issuing ad-hoc queries to a database with many users, your query might be the "noisy neighbor" (the shopping cart overloaded with groceries). By setting a tighter timeout on experimental queries you can reduce the impact on other users.
Configure timeouts in the InfluxDB 3 Python client:
import influxdb_client_3 as InfluxDBClient3
# Configure different timeout classes (in seconds)
ui_timeout = 10 # For dashboard queries
api_timeout = 60 # For application queries
batch_timeout = 300 # For analytical queries
# Create client with default timeout
client = InfluxDBClient3.InfluxDBClient3(
host="https://{{< influxdb/host >}}",
database="DATABASE_NAME",
token="AUTH_TOKEN",
timeout=api_timeout # Python client uses seconds
)
# Quick query with short timeout
def query_latest_data():
try:
result = client.query(
query="SELECT * FROM sensors WHERE time >= now() - INTERVAL '5 minutes' ORDER BY time DESC LIMIT 10",
timeout=ui_timeout
)
return result.to_pandas()
except Exception as e:
print(f"Quick query failed: {e}")
return None
# Analytical query with longer timeout
def query_daily_averages():
query = """
SELECT
DATE_TRUNC('day', time) as day,
room,
AVG(temperature) as avg_temp,
COUNT(*) as readings
FROM sensors
WHERE time >= now() - INTERVAL '30 days'
GROUP BY DATE_TRUNC('day', time), room
ORDER BY day DESC, room
"""
try:
result = client.query(
query=query,
timeout=batch_timeout
)
return result.to_pandas()
except Exception as e:
print(f"Analytical query failed: {e}")
return None
Replace the following:
{{% hide-in "cloud-serverless" %}}
DATABASE_NAME{{% /code-placeholder-key %}}: the name of the database to query{{% /hide-in %}}
{{% show-in "cloud-serverless" %}}DATABASE_NAME{{% /code-placeholder-key %}}: the name of the bucket to query{{% /show-in %}}
{{% show-in "clustered,cloud-dedicated" %}}AUTH_TOKEN{{% /code-placeholder-key %}}: a database token with read access to the specified database.{{% /show-in %}}
{{% show-in "cloud-serverless" %}}AUTH_TOKEN{{% /code-placeholder-key %}}: an API token with read access to the specified bucket.{{% /show-in %}}
{{% show-in "enterprise,core" %}}AUTH_TOKEN{{% /code-placeholder-key %}}: your {{% token-link "database" %}}with read permissions on the specified database{{% /show-in %}}Implement simple retry strategies with progressive timeouts:
import time
import influxdb_client_3 as InfluxDBClient3
def query_with_retry(client, query: str, initial_timeout: int = 60, max_retries: int = 2):
"""Execute query with basic retry and progressive timeout increase"""
for attempt in range(max_retries + 1):
# Progressive timeout: increase timeout on each retry
timeout_seconds = initial_timeout + attempt * 30
try:
result = client.query(
query=query,
timeout=timeout_seconds
)
return result
except Exception as e:
if attempt == max_retries:
print(f"Query failed after {max_retries + 1} attempts: {e}")
raise
# Simple backoff delay
delay = 2 * (attempt + 1)
print(f"Query attempt {attempt + 1} failed: {e}")
print(f"Retrying in {delay} seconds with timeout {timeout_seconds}s...")
time.sleep(delay)
return None
# Usage example
result = query_with_retry(
client=client,
query="SELECT * FROM large_table WHERE time >= now() - INTERVAL '1 day'",
initial_timeout=60,
max_retries=2
)
Track these essential timeout-related metrics:
Symptoms: Many queries exceeding timeout limits
Common causes:
Solutions:
Symptoms: Same queries sometimes fast, sometimes timeout
Common causes:
Solutions:
[!Note] Regular analysis of timeout patterns helps identify optimization opportunities and system scaling needs.