Print

Query SLEEP

The SLEEP clause tells RioDB to suspend a query after its conditions have hit a match (or after its output action has been triggered).
RioDB often deals with stream of thousands of requests per second. Suppose the output action is to generate a system alert in a remote system. If not careful, the query could result in millions of alerts being generate in a few seconds.
To prevent this catastrophy, the SLEEP clause tells RioDB to suspend the query for an amount of time after each time the query triggers an action.
The query will automatically resume after the SLEEP time has passed.

The sleep clause is optional.

Syntax

The SLEEP value expressed as time counted in an integer followed by a character representing the time unit. ‘s’ for seconds, ‘m’ for minutes, ‘h’ for hours, ‘d’ for days.

SLEEP <integer><time unit>

The SLEEP can optionally be applied to a partitioned window’s key. In other words, the query will stop firing alerts for a giving partition but will continue to fire alerts for other partitions. A typical example to illustrate this: Suppose you are counting requests received by remote_ip_addr during the last 10 seconds. For this, you have a partitioned window counting requests by remote_ip_addr, with range 10s. You want the query to fire an alert when the count exceeds 10,000. If partition “12.12.12.12” (a sample IP address) receives more than 10,000, the query will fire an alert, however, you only want the query to sleep for remote_ip_addr “12.12.12.12”! If a different IP address, say “13.13.13.13” also exceeds 10,000, you want the query to fire again, because it’s a different IP address. You can accomplish this by indicating that the query should SLEEP for a matching partition but continue to run for other partitions.

SLEEP <integer><time unit> ON PARTITION <partitioned window name or alias>

Examples

Seconds

sleep 30 seconds:

SELECT ...
FROM ...
WHEN... 
OUTPUT ...
SLEEP 30s;

Minutes

sleep 5 minutes:

SELECT ...
FROM ...
WHEN... 
OUTPUT ...
SLEEP 5m;

Hours

sleep 2 hours:

SELECT ...
FROM ...
WHEN... 
OUTPUT ...
SLEEP 2h;

Day

sleep 1 day:

SELECT ...
FROM ...
WHEN... 
OUTPUT ...
SLEEP 1d;

On Partition

Assume we have a partitioned window that counts requests received by each remote_ip_addr, IP_REQS_LAST_10S. We want a query to SLEEP when requests from any remote_ip_addr reaches a threshold. When if does, we want the query to sleep only for that remote_ip_addr, but continue to run for other values:

SELECT ...
FROM ip_reqs_last_10s
WHEN... 
OUTPUT ...
SLEEP 90s ON PARTITION ip_reqs_last_10s;

The same, but using alias:

SELECT ...
FROM ip_reqs_last_10s w
WHEN... 
OUTPUT ...
SLEEP 90s ON PARTITION w;

System-wide Configuration for SLEEP:

For RioDB Cloud deployments:

On the cloud, RioDB has a default setting of 1-second sleep per query. You may specify a higher value. However, if the SLEEP is not specified for a query, then RioDB will automatically assume a 1-second sleep period each time the query triggers a match. The reason for this is because if a user accidentally triggers an external OUTPUT request a million times per second for several hours, it could incur astronomical costs from the downstream service. So, this default policy is a protection.

For RioDB On-Prem deployments:

The RioDB configuration file (riodb.conf) has a property ‘min_sleep_secs’ to force a minimum SLEEP time on all queries. This prevents a catastrophic output flood if a developer submits a query forgetting the SLEEP clause.
When ‘min_sleep_secs’ is set, developers can always specify a higher SLEEP interval than the system minimum, but never lower. The ‘min_sleep_secs’ is automatically apply to all queries that lack the SLEEP clause.

Note, RioDB must be restarted in order for the ‘min_sleep_secs’ to become effecited, and it will apply to queries submitted prior to the change.

Table of Contents
Scroll to Top