0
0
Nginxdevops~10 mins

A/B testing with split_clients in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - A/B testing with split_clients
Client Request Arrives
split_clients Directive
Hash Client ID or IP
Assign Client to Variant A or B
Serve Variant Content
Log or Track Variant Served
The client request is processed by split_clients which hashes client info to assign them to variant groups for A/B testing.
Execution Sample
Nginx
split_clients "$remote_addr" $variant {
  50%     "A";
  *       "B";
}

if ($variant = "A") {
  return 200 "Variant A served";
}
return 200 "Variant B served";
This config splits clients by IP address into two groups: 50% get variant A, others get variant B, and returns a message accordingly.
Process Table
StepClient IPHash ResultAssigned VariantActionOutput
1192.168.1.10hash=0.45AServe Variant AVariant A served
2192.168.1.11hash=0.75BServe Variant BVariant B served
310.0.0.5hash=0.10AServe Variant AVariant A served
410.0.0.6hash=0.90BServe Variant BVariant B served
5172.16.0.1hash=0.50AServe Variant AVariant A served
6172.16.0.2hash=0.99BServe Variant BVariant B served
7End of requests---No more clients
💡 All client requests processed and assigned variants based on hash thresholds.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
$remote_addr-192.168.1.10192.168.1.1110.0.0.510.0.0.6172.16.0.1172.16.0.2-
$variant-ABABAB-
Key Moments - 3 Insights
Why does a client with hash exactly 0.50 get assigned to variant A?
Because the split_clients directive assigns 50% to variant A using '50%', which includes hash values up to but not including 0.50. However, in nginx split_clients, the percentage is inclusive of the boundary, so hash=0.50 maps to A as shown in execution_table row 5.
What happens if a client's hash is greater than 0.50?
They are assigned to variant B as the '*' catch-all rule covers all remaining hash values above 0.50, as seen in rows 2,4,6.
Is the variant assignment consistent for the same client IP?
Yes, because the hash is based on the client IP ($remote_addr), so the same IP always hashes to the same variant, ensuring consistent experience.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what variant is assigned to client IP 10.0.0.5?
AVariant B
BNo variant assigned
CVariant A
DDepends on time
💡 Hint
Check row 3 in execution_table where client IP 10.0.0.5 is assigned variant A.
At which step does the hash value exceed 0.50, causing assignment to variant B?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at execution_table row 2 where hash=0.75 > 0.50, assigned to B.
If the split_clients directive changed to 30% for variant A, how would the assignment change for hash=0.45?
AAssigned to variant B
BAssigned to variant A
CNo assignment
DRandom assignment
💡 Hint
Since 0.45 > 0.30, it falls outside the 30% threshold, so assigned to variant B.
Concept Snapshot
split_clients "$variable" $target {
  percentage% "variant1";
  * "variant2";
}

- Splits clients by hashing $variable (e.g., IP)
- Assigns variants based on percentage thresholds
- Ensures consistent variant per client
- Used for A/B testing in nginx
Full Transcript
This visual execution shows how nginx's split_clients directive assigns clients to A/B test variants. Each client IP is hashed to a number between 0 and 1. If the hash falls within the first percentage (e.g., 50%), the client is assigned variant A; otherwise, variant B. The execution table traces multiple client IPs, their hash values, assigned variants, and the response served. Variables $remote_addr and $variant track client IP and assigned variant respectively. Key moments clarify how boundary hash values are assigned and consistency of assignment. The quiz tests understanding of variant assignment based on hash thresholds. The snapshot summarizes the syntax and behavior of split_clients for quick reference.