random

The 'random' directive specifies that the load balancing method used in an upstream block will randomly select a server.

Syntaxrandom;
Defaultnone
Contextupstream
Argumentsnone

Description

The 'random' directive, used within an upstream context, enables NGINX to implement load balancing by randomly selecting which server to route requests to from the available pool of upstream servers. This directive enhances the distribution of incoming requests, ensuring that no single server is overwhelmed by consecutive requests. When activated, the 'random' directive works alongside the existing upstream servers defined in the configuration, randomly choosing one of them for each request.

This selection process is facilitated through an internal algorithm that utilizes a random number generator to pick a server from the pool each time a request hits the upstream block. This directive does not take any arguments, and therefore operates based solely on its presence in the upstream definition. Another important aspect is that this method does not consider the server load or health status, which may make it less ideal in scenarios where a more intelligent load balancing method, like 'least_conn' or 'ip_hash', could be preferred.

Consequently, while using the 'random' directive can help in distributing requests evenly for light workloads, administrators must be mindful of its limitations in high-demand environments where server performance varies significantly.

Config Example

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
    random;
}

Using 'random' may lead to uneven server load in high-traffic scenarios if servers have different performance capabilities.

Make sure to combine 'random' with health checks for a more resilient configuration.

← Back to all directives