Session 6 · A real-world case

Cloudflare's outage of 2 July 2019

On Tuesday, 2 July 2019, at 13:42 UTC, a large part of the web stopped answering. Sites of every kind returned the same error page, 502 Bad Gateway.

What they had in common was Cloudflare, a company that sits between millions of websites and their visitors: requests go to Cloudflare's servers first, which filter them, cache what they can, and pass the rest on. On every one of those servers, in every city where Cloudflare has them, the processors that handle web traffic were nearly 100% busy. During the incident a leader of Cloudflare's Solutions Engineering group reported that the company had “lost 80% of our traffic”.

There was no attack, and nothing had crashed. The cause was one regular expression and a property of the event loop that we study in session 6: running code keeps the thread until it returns.

The firewall rule

A regular expression is a pattern that describes a set of strings; a program called a regex engine tests whether a given string contains a part that the pattern matches. Before passing a request on, Cloudflare's servers check it against a web application firewall, the WAF: a list of rules, each a regular expression that recognizes some kind of attack. At 13:31 an engineer merged a new rule against cross-site scripting (XSS), an attack that places a script of the attacker's in a web page, where the browsers of the page's visitors run it. The rule was tested, built, and at 13:42 deployed.

WAF rules are deployed differently from the rest of Cloudflare's software. An ordinary change goes out in stages, over hours or days: first to a data center whose traffic comes only from Cloudflare's employees, then to one that carries a small share of customer traffic, then to three canary data centers, a small group in which a fault shows before it spreads, and only then to the world. A rule goes everywhere at once, within seconds, through a system called Quicksilver, because when a new attack appears, a rule that arrives tomorrow is useless.

The new rule reached every server in the world, and on every server the processors that handle web traffic went to full load.

Backtracking

The rule was this regular expression:

(?:(?:\"|'|\]|\}|\\|\d|(?:nan|infinity|true|false|null|undefined|symbol|math)|\`|\-|\+)+[)]*;?((?:\s|-|~|!|{}|\|\||\+)*.*(?:.*=.*)))

The trouble is at its end, which amounts to .*.*=.*: any characters, then any characters, then =, then any characters (. matches one character, and * repeats it any number of times, zero included). The engine that ran it, the library PCRE (Perl Compatible Regular Expressions), works by backtracking: it tries one way of matching, and when the rest of the pattern fails, it goes back and tries another. With two .* in a row, the number of ways to split a string between them grows with the length of the string, and the engine tries each way in full before it gives up. The pattern is also not anchored to the start of the string: when every way fails from the first character, the engine starts again from the second, then from the third, and so on to the end. Cloudflare's report counts the steps: 23 for the input x=x, 555 for x= followed by twenty x.

JavaScript's regex engine also backtracks, so the growth can be measured in Node. The program cloudflare/backtracking.mjs tests the same tail, .*.*=.*, followed by a ; that the input never contains, so that the engine must try every way before it reports that there is no match:

for (const re of [/.*.*=.*;/, /^.*.*=.*;/]) {
  for (const n of [1000, 2000, 4000]) {
    const s = "x=" + "x".repeat(n);
    const t = performance.now();
    re.test(s);
    console.log(`${re} on ${n} characters: ${(performance.now() - t).toFixed(1)} ms`);
  }
}
/.*.*=.*;/ on 1000 characters: 161.3 ms
/.*.*=.*;/ on 2000 characters: 1244.6 ms
/.*.*=.*;/ on 4000 characters: 9922.1 ms
/^.*.*=.*;/ on 1000 characters: 0.8 ms
/^.*.*=.*;/ on 2000 characters: 3.3 ms
/^.*.*=.*;/ on 4000 characters: 9.2 ms

Each time the input doubles, the time is multiplied by about eight. Both causes are needed for that: the splits between the two .* make one attempt long, and the attempts from every starting position multiply that by the length of the string once more. Anchored with ^, the pattern /^.*.*=.*;/ is tried from the first character only, and the program measures it too: about 9 ms on the same 4,000 characters, against 10 seconds. These measurements are consistent with growth as the cube of the length, for this pattern and these inputs; the exact times and ratios depend on the engine and the machine. An input of a few thousand characters keeps a processor busy for seconds.

The event loop

A slow test does not by itself stop a server from serving its other requests. In a server that gives each request its own thread, the operating system shares the processors among the threads: a request stuck in the regular expression slows the others down but does not stop them, as long as processors and threads remain; enough such requests exhaust those too. On an event loop, one such request stops every other request that shares its loop. Cloudflare's servers run nginx, a web server built around event loops: on each machine it runs several worker processes, and each worker runs one loop on one thread, with hundreds of requests in flight at once. While a request waits for the network, the loop serves the others; that is how a few processes serve so many requests. This is the model of session 6.

Property 3 of the session, run-to-completion, states that the loop takes the next function from the queue only when the stack is empty: the running code has returned, and so has everything it called. Until then no other computation runs on that thread; the functions the running code calls run inside it. The operating system may pause the thread to run another program, but when the thread runs again it goes on with the same computation. A regular expression under test is running code. While one request's test ran, every other request on that worker waited, whatever it asked for, however small. The program cloudflare/one_loop.mjs reproduces this on one loop: a timer every 10 ms stands for a cheap request, and at 100 ms one request runs the regular expression on 2,000 characters. At 3,000 ms the program prints the count once more and stops the timer.

// One loop serves a cheap request every 10 ms. At 100 ms, one request runs the regular expression.
const t0 = performance.now();
const at = () => Math.round(performance.now() - t0);
let served = 0;
const cheap = setInterval(() => { served++; }, 10);
setTimeout(() => {
  console.log(`regex starts at ${at()} ms; cheap requests served: ${served}`);
  /.*.*=.*;/.test("x=" + "x".repeat(2000));
  console.log(`regex ends at ${at()} ms; cheap requests served: ${served}`);
}, 100);
setTimeout(() => {
  console.log(`at ${at()} ms; cheap requests served: ${served}`);
  clearInterval(cheap);
}, 3000);
regex starts at 101 ms; cheap requests served: 9
regex ends at 1440 ms; cheap requests served: 9
at 3002 ms; cheap requests served: 152
The first 1,600 ms of the run above. The test runs from 101 to 1440 ms; the timer's ticks are schematic: drawn every 10 ms, as scheduled, before and after it.

For 1.3 seconds the loop served nothing else. (The count at 3,000 ms is about 150, not 300, because setInterval does not make up the ticks it missed. The timer shows only that no callback runs during the test; it is not a model of a server's queue, where requests may wait, time out, or be refused under overload.) On 2 July, according to Cloudflare's report, the processors that serve HTTP and HTTPS traffic were exhausted on every server worldwide; the front-line servers still had processor cores free, but could not reach the processes that serve that traffic, and answered with the 502 error.

Writing the handler in the program above as an async function would not have helped. An async function runs on the same thread, and between two awaits it runs to completion like any other code; the test of the regular expression contains no await. On one loop, other requests cannot run during an uninterrupted synchronous computation. A long computation can be split into pieces that hand the loop back between them, or run on another thread. An await alone is not enough: in JavaScript, await Promise.resolve() lets queued promise continuations run, but timers and network events can still wait behind them; handing the loop back means letting the host's queue run, for instance with a timer of 0 ms between pieces.

Failures and recovery

Cloudflare's report lists eleven failures that came together. Two of them are described above: the rule's regular expression, and an engine without guarantees on running time. Three others bear on this story.

At 14:07 the team used a global switch that turned the WAF off everywhere. At 14:09 traffic and processor load were back to normal, twenty-seven minutes after the deploy. The WAF was turned back on, with the rule's change rolled back, at 14:52.

2 July 2019, from 13:30 to 15:00 UTC, times from Cloudflare's report. The accent marks the twenty-seven minutes from the deploy to the return to normal load.

Repairs

According to the report, the protection against runaway processor use was put back, and each of the 3,868 WAF rules was inspected by hand for backtracking. Rules were to go through performance tests and, outside emergencies, staged rollouts. The WAF was also to move to a regex engine with guarantees on running time, RE2 or Rust's: these engines do not backtrack, and their time grows linearly with the input.

In March 2020 Cloudflare described a change to its nginx servers that addresses the same weakness, although the article is about the time requests take and does not mention the outage. The WAF's work, the most notable place where a request may need more processor time, was moved off the event loop to a pool of threads that nginx already had. While a rule runs on a thread of the pool, the loop goes on serving the other connections. Cloudflare measured, for each request, the longest time it blocked the loop; for requests that go through the WAF, the 99th percentile of that time (the value that 99 requests in 100 stay under) fell by 30 to 40%. Session 6 gives two remedies for its own example, a timer due at 100 ms whose function runs only at 500 ms, after a loop that computes for 500 ms: split the computation, or move it to another thread. This is the second.

Lessons

Sources

Draft, 7 October 2026.