Back to Blog
Lesson 33 of the Cloudflare: Cloudflare for Developers: DNS to CDN course
Cloud NativeAugust 11, 20264 min read

Advanced Routing Patterns: Managing Traffic with Cloudflare Workers

Master advanced routing in Cloudflare by splitting traffic between multiple Workers, using wildcards, and managing route priority to build scalable architectures.

CloudflareWorkersRoutingArchitectureDevOps
A city street cleaner in orange uniform at a busy intersection with cars and buildings.

Previously in this course, we covered Workers Routes and Custom Domains: A Developer’s Guide, where you learned how to bind a single Worker to a specific route. In this lesson, we level up that concept by implementing Advanced Routing Patterns, allowing you to orchestrate multiple Workers to handle different parts of your application architecture independently.

When your application grows, a single monolithic Worker becomes difficult to maintain. By utilizing Cloudflare’s routing engine, you can delegate specific paths (like /api/* or /assets/*) to specialized Workers, keeping your codebase clean and your deployment cycles decoupled.

The Routing Engine: Principles of Traffic Distribution

At the edge, Cloudflare evaluates incoming requests against your defined routes to determine which Worker—if any—should execute. This evaluation follows a specific order: it looks for the most specific match first.

If you have two routes, example.com/api/* and example.com/*, a request to example.com/api/users will hit the first route because it is more specific. Understanding this precedence is the key to building complex, multi-service architectures.

Wildcard Routing Patterns

Wildcards (*) act as catch-alls for path segments. They are indispensable for routing patterns that follow a predictable structure.

  • example.com/api/v1/* — Matches any path starting with /api/v1/.
  • */images/* — Matches any subdomain followed by /images/.

Worked Example: Splitting Traffic

High-angle shot of cars on a Danish highway surrounded by lush greenery on a sunny day.

Let’s say you are building a project that handles a standard website and a separate, high-traffic API. You want the main site served by one Worker and the API logic handled by another.

In your wrangler.toml (or via the dashboard), you define the routes as follows:

TOML
# Main site worker
name = "frontend-worker"
routes = [
  { pattern = "myapp.com/*", zone_name = "myapp.com" }
]

# API worker
name = "api-worker"
routes = [
  { pattern = "myapp.com/api/*", zone_name = "myapp.com" }
]

Managing Priority

Cloudflare processes routes based on specificity. However, if you have overlapping patterns, the system follows a hierarchical logic. The most "specific" route wins.

If you find that your frontend-worker is accidentally catching API requests, you must ensure the api-worker route is explicitly defined. If two routes have the same specificity, the order of definition in the dashboard or via your deployment configuration ensures the intended behavior.

Hands-on Exercise: Implementing a Multi-Worker Setup

Your goal is to isolate your "status" endpoint from your main application logic.

  1. Create a second Worker: Initialize a new directory using wrangler init status-worker.
  2. Configure Routes: Update your wrangler.toml for status-worker to listen on yourdomain.com/status/*.
  3. Deployment: Deploy both the main application Worker and the status-worker.
  4. Verification: Send a request to yourdomain.com/status/check. Observe that the response comes strictly from your new status-worker, while requests to yourdomain.com/ are still handled by your primary project code.

Common Pitfalls

Close-up of a triangular warning sign indicating a slippery surface, fixed to a wooden post.

  • Over-lapping Wildcards: Defining * as a route will catch every single request to your domain. If you add more specific routes later, you might accidentally mask them if you don't account for the precedence rules.
  • Missing /*: Remember that example.com/api is NOT the same as example.com/api/*. Without the wildcard, you are only routing the exact root of that directory, not the sub-paths.
  • Route Bloat: While you can have dozens of routes, it becomes difficult to audit. Use descriptive names for your Workers and group related paths logically.

FAQ

Q: Does routing have a performance impact? A: No, the routing decision happens at the Cloudflare edge in microseconds. It is extremely efficient.

Q: What happens if no route matches? A: The request falls through to your origin server (the IP address or hostname defined in your DNS settings).

Q: Can I share data between these Workers? A: Workers are isolated by default. To share data, you should use KV or D1 as a shared state layer.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

You’ve now moved beyond simple single-Worker deployments. By using wildcard patterns and managing route specificity, you can modularize your application, effectively delegating responsibilities to different edge functions. This is the foundation of a true microservices architecture at the edge.

Up next: We will dive into Observability and Logging, where you'll learn how to trace requests across these multiple Workers and debug production issues effectively.

Similar Posts