Using Load Balancer and Cloudflare Worker to redirect

Suppose that you are using a Cloudflare Worker (https://developers.cloudflare.com/workers/examples/redirect/) to redirect from one URL to another.

  1. Is there an open source redirection manager (e.g., closed source redirection.io) that makes it easy to manage these redirects using a Cloudflare Worker(s)?

  2. What is the most performant way to set this worker(s) up with the Cloudflare Load Balancer when you have, for example,

  • 100 redirects
  • 1,000 redirects
  • 10,000 redirects
  • 100,000 redirects

I am interested in when, how, and why you would configure a single or multiple worker(s) to handle this problem.

I am also interested in which data structure you would use (e.g., JavaScript Map, JSON object, Cloudflare KV) to store and retrieve the redirect information for each request to the worker.

I would highly recommend looking into Bulk Redirects (beta) · Cloudflare Rules docs

You don’t need a load balancer in front of a worker.

I would only use a worker when the logic is too complex for Single Redirects · Cloudflare Rules docs

I would only use a worker when the logic is too complex for Single Redirects.

https://developers.cloudflare.com/rules/url-forwarding/#availability

You can only use up to 125 redirects with regex. That’s on the enterprise plan. So, you have not covered the 1,000+ redirect case.

https://developers.cloudflare.com/rules/url-forwarding/bulk-redirects/

Bulk Redirects are essentially static — they do not support string replacement operations or regular expressions.

This is the problem with bulk redirects.

Furthermore, you can only create 1,250 bulk redirects that are limited to 10,000 operations per month. So, you have not covered the 1,000+ redirect case.

You don’t need a load balancer in front of a worker.

You don’t need one. However, in a setup that uses a load balancer, you can use the load balancer to route to specific workers or even “redirect” based on path.

But the question is whether that is a good idea or not?

https://developers.cloudflare.com/rules/url-forwarding/#execution-order

This execution order shows how rules are executed.

So, in theory, a load balancer could route the request first making it more performant. However, that may not be a good idea when you have 10,000 redirects.

So, to add context to the original post.

When does the Cloudflare Worker execute? Is it before or after page (redirect) rules?

Which implementation of Cloudflare Worker redirects is the most performant using the storage options listed above?

Can adding a load balancer help you make that configuration more performant when there are thousands of redirects?

Is there a tool that implements the most performant implementation that can account for 10,000+ redirects?

Workers run before load balancers, so you wouldn’t be routing the request.

Workers run second to last so rules go before them.

Depends on your rules. If you want to change them dramatically, then something like KV is going to be best. But otherwise, I would use a JavaScript object to define the redirects that will be used.

No. Workers can easily handle thousands of requests per second

There are some example workers out that for redirecting. Can look through them https://github.com/search?q=cloudflare+workers+redirects&type=repositories

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.