Skip to main content
Version: 1.x

SLEEP statement

The SLEEP statement is used to introduce a delay or pause in the execution of a query or a batch of queries for a specific amount of time.

Statement syntax

SurrealQL Syntax
SLEEP @duration;

Example usage

The following query shows example usage of this statement.

-- Sleep one second
SLEEP 1s;
-- Sleep 100 milliseconds
SLEEP 100ms;

Use cases

SLEEP can be useful in a small number of situations, such as:

  • Testing and debugging: can be used to understand how concurrent transactions interact, test how systems handle timeouts and delays, simulate behaviour in more distant regions with longer latency
  • Throttling: can be used to throttle the execution of operations to prevent the database from being overwhelmed by too many requests at once
  • Security measures: can be used to slow down the response rate of login attempts to mitigate the risk of brute force attacks
  • Avoiding duplicate timestamps: multiple queries like CREATE type::thing("person", time::now()); may execute so quickly that one attempted record will have the same timestamp (and thus the same record ID) as the previous one, resulting in an error