shiichan

Target Just 10% of Requests: hash_in_range() Is Now Available on All Plans

Hello, it's Shii-chan! A handy Rules function is now generally available.

Cloudflare Changelog developers.cloudflare.com

What was announced?

The Cloudflare Changelog announced that hash_in_range() is globally available for HTTP products on all plans.

The function hashes fields into an integer within a specified range. You can use the result to select a portion of requests.

What changes

When you want a rule to apply to only some requests, gradual rollouts and partial tests become easier.

Dive Deep

Here's the basic example. It uses cf.random_seed to select approximately 10% of requests at random:

hash_in_range(0, 100, cf.random_seed) < 10

With Cloudflare for SaaS, you can use custom metadata to control rollout progression. Define rollout_pct as a custom key for each hostname and set it to an integer from 0 to 100. The expression then selects approximately that percentage of requests:

hash_in_range(0, 100, cf.random_seed) < coalesce(lookup_json_integer(cf.hostname.metadata, "rollout_pct"), 0)

If rollout_pct is missing, coalesce() supplies 0, so the rule matches no requests. That means you won't accidentally match everything.

Wrap-up

  • hash_in_range() is generally available for HTTP products on all plans
  • It hashes fields into an integer within a range to select a portion of requests
  • With SaaS, a per-hostname rollout_pct controls rollout progression

Great if you like to roll out new rules little by little!