On 2 July 2019, websites behind Cloudflare around the world returned 502 errors for 27 minutes. The cause was not an attack or a hardware fault. It was one regular expression inside a new firewall rule, which made the regex engine do so much repeated work that every CPU core handling web traffic was pinned at 100%. The incident is a compact lesson in two things interviewers care about: how an innocent-looking pattern can take far more time than its input size suggests, and why even small configuration changes need a staged rollout.
What users saw
Cloudflare sits in front of its customers' websites as a reverse proxy. A visitor's request reaches a Cloudflare edge server first, is inspected, and is then forwarded to the customer's origin server. When the edge servers ran out of CPU they could not process requests, so visitors to sites proxied through Cloudflare saw a 502 Bad Gateway page.
Cloudflare said traffic fell by 82% at the worst point. Customers also could not log in to the Cloudflare dashboard or use its API, because those are served through the same edge. The company noted that it had not had a global outage in six years.

Timeline
All times are UTC, as given in Cloudflare's post-mortem. Indian Standard Time is alongside.
| Time (UTC) | Time (IST) | Event |
|---|---|---|
| 13:31 | 19:01 | The pull request containing the new rule is merged after approval |
| 13:37 | 19:07 | The build system builds the rules and runs the tests, which pass |
| 13:42 | 19:12 | The rule change is deployed, and goes to the whole network |
| About 13:45 | 19:15 | The first alert pages engineers, followed by many more: failing tests, 502s, CPU exhaustion |
| 14:00 | 19:30 | The WAF is identified as the cause and an attack is ruled out |
| 14:02 | 19:32 | The team proposes a global terminate of the WAF managed rules |
| 14:07 | 19:37 | The global terminate is executed |
| 14:09 | 19:39 | Traffic and CPU return to normal |
| 14:52 | 20:22 | With the faulty rule removed and the fix verified, the WAF is switched back on |
What the rule was for
A web application firewall, or WAF, inspects the content of HTTP requests and blocks those that look like attacks. Cloudflare maintains a set of managed rules that it updates frequently as new attack techniques appear. In the 60 days before the incident it had handled 476 change requests for these rules, about one every three hours.
The change on 2 July improved detection of cross-site scripting, where an attacker tries to smuggle JavaScript into a page. The new rule was deployed in simulate mode. In that mode a rule runs against real traffic and logs what it would have done, but blocks nothing, so the team can measure false positives. The important detail is that a simulated rule still executes. Its regular expression is still evaluated on every request, so it still costs CPU.
Backtracking from first principles
The rule's regular expression was long, but Cloudflare identified the damaging part as the fragment .*(?:.*=.*). Ignoring the non-capturing group, which does not change matching here, that is:
.*.*=.*
Read it piece by piece. . matches any single character. * means zero or more of the previous thing. So the pattern says: any run of characters, then another run of characters, then an equals sign, then any run of characters.
How a backtracking engine works
Many regex engines, including the PCRE library Cloudflare's WAF used at the time, match by trial and error. The engine walks through the pattern from left to right. Whenever it has a choice, it picks one option, remembers the others, and carries on. If the rest of the pattern later fails, it goes back to the most recent choice and tries the next option. That going back is backtracking.
.* is greedy: it first grabs as many characters as it can, and gives them back one at a time only if it must.
A small example
Take the input xxxx, four characters and no equals sign, and try .*.*=.* from the start of the string.
- The first
.*grabs all four characters. The second.*has nothing left and matches the empty string. The engine now needs=, but it is at the end of the input. Fail. - Backtrack. The first
.*gives back one character and holdsxxx. The second.*grabs the remainingx. Need=: fail. The second.*gives back its character and holds nothing. Need=, but the next character isx: fail. - Backtrack again. The first
.*holdsxx. The second.*tries holdingxx, thenx, then nothing. Each attempt fails at=. - This continues until the first
.*holds nothing at all and the second has tried every length from four down to zero.
The engine cannot know that there is no = anywhere. It has to try every way of dividing the text between the two stars before it can give up. The number of ways is easy to count. If the first star takes k characters, the second can take anything from 0 to n − k, where n is the input length. Adding those up gives (n + 1)(n + 2) / 2 combinations.
| Input length n | Ways to split between the two .* |
|---|---|
| 5 | 21 |
| 10 | 66 |
| 20 | 231 |
| 40 | 861 |
| 1,000 | 501,501 |
Doubling the input roughly quadruples the work. And this counts only attempts anchored at the first character. An engine searching for a match anywhere in the string repeats the process from each later starting position too, and every extra .* in the pattern multiplies the combinations again.
Cloudflare measured this with a regex debugger and published the numbers. Matching .*.*=.* against x=x took 23 steps. Against x= followed by 20 x characters it took 555 steps. When the input was 20 x characters with no = at all, the engine needed 4,067 steps just to conclude that there was no match. With a semicolon added to the end of the pattern, so that x= followed by 20 x characters no longer matched, the count rose to 5,353 steps.
Those are tiny inputs. Real HTTP requests carry bodies and query strings thousands of characters long, and the WAF was running the pattern on live traffic at every edge location.
Why this is avoidable
Backtracking is a property of the engine, not of regular expressions. An alternative family of engines simulates all possible positions in the pattern at once, moving through the input one character at a time and never going back. Their running time grows linearly with the input length. The idea dates back to a 1968 paper by Ken Thompson, as Cloudflare's post points out. RE2 and Rust's regex library work this way. The price is that they give up a few features, such as backreferences, that cannot be matched in linear time.
Why the safeguards did not catch it
Cloudflare listed the contributing factors itself. Several independent protections each had a gap.
- The CPU guard had been removed. The WAF once had a protection that stopped a regular expression from using too much CPU. It was removed by mistake during a refactoring a few weeks earlier, a refactoring whose purpose was to make the WAF use less CPU.
- The engine offered no guarantee. PCRE backtracks and puts no bound on how long a match may take.
- The tests checked correctness, not cost. The test suite verified that rules blocked what they should and passed what they should. It had no way to detect a rule that consumed excessive CPU.
- WAF rules skipped the staged rollout. Cloudflare's software goes out in stages: first to a location used only by employees, then to one carrying a small share of customer traffic, then to canary locations, and only then to everything. WAF rule updates were exempt so that new threats could be blocked quickly. They were distributed through Quicksilver, Cloudflare's key-value store, which pushes changes to the whole network in seconds. This change was not an emergency, but it took the fast path anyway.
- Rollback was too slow. The planned way to undo the change needed the full WAF build to run twice.
- The tools were behind the broken system. Engineers had trouble reaching internal systems because those were protected by Cloudflare's own products, which were down. Some had also lost access because credentials that had not been used recently were disabled as a security measure.
In the end the team used a global terminate, a kill switch that disables one component across the whole network. Turning off the WAF managed rules brought CPU back to normal within two minutes.
What Cloudflare changed
The post-mortem listed concrete actions, with their status at the time of writing:
- The protection against excessive CPU use by a rule was put back.
- All 3,868 managed WAF rules were inspected by hand for other patterns that could backtrack excessively.
- Performance profiling of every rule was being added to the test suite.
- The WAF was to move to a regex engine with run-time guarantees, either RE2 or Rust's.
- The procedure for rule changes was altered to use the same staged rollout as other software, while keeping an emergency path for active attacks.
- Plans were made for a way to take the dashboard and API off the Cloudflare edge in an emergency, and for automatic status page updates.
The computer science underneath
Layer 7 inspection costs CPU per request. A device that forwards packets by IP address and port does a small, fixed amount of work for each one. A WAF works at the application layer: it must reassemble the HTTP request and run pattern matching over its contents. That is far more work, and how much depends on the content and on the rules. The lessons on layer 4 versus layer 7 load balancers and on firewalls explain what each layer can see and what that visibility costs.
Algorithmic complexity is an operational concern. The same pattern can be matched in time proportional to n by one algorithm and proportional to n squared, or worse, by another. On test inputs of ten characters the difference is invisible. On production inputs it is the difference between a healthy server and a dead one.
A CPU-bound task with no preemption point starves everything else. When every worker is stuck inside a long regex match, there is no core left to serve other requests. The operating system can share cores fairly between processes, but it cannot make a busy worker finish sooner. This is why per-task CPU budgets and timeouts matter, and it is the same reasoning behind time slices in CPU scheduling.
Configuration is code. A rule update is as capable of breaking production as a binary release. Staged rollouts limit the blast radius by exposing a change to a small slice of traffic first and watching the metrics. See rolling, blue-green and canary deployments.
What an interviewer could ask from this
1. What is catastrophic backtracking? It is when a backtracking regex engine has to try a very large number of ways to divide the input among the quantifiers in a pattern before it can report a match or a failure. The work grows polynomially or exponentially with the input length.
2. Why is .*.*=.* slow on a string with no equals sign?
Each .* can match any length, so the engine tries every way of splitting the text between them, about n squared over two combinations, before it can conclude there is no match.
3. How do you guarantee linear-time matching? Use an engine based on automata, such as RE2, which tracks all possible states at once and never rereads input. You lose backreferences and some lookaround features.
4. The rule was in simulate mode. Why did it still cause an outage? Simulate mode skips the blocking action, not the evaluation. The expensive part, running the regex, happened on every request.
5. How would you roll out a rule change safely? Test it for CPU cost as well as correctness, deploy to a small canary first, watch CPU and error rates, then widen in stages. Keep a fast, well-practised kill switch.
Takeaways
- A regular expression is a program. Its worst-case running time depends on the engine, and backtracking engines have no upper bound that is safe for untrusted input.
- Test performance, not just correctness, and test with inputs that fail to match as well as inputs that succeed.
- Every change that reaches production deserves a staged rollout, including rules and configuration.
- Defence in depth only works if each layer is verified. Here a CPU guard, a test suite and a rollout process all had gaps that lined up.
- Keep a kill switch, and make sure you can reach it when your own product is down.












