Regex Engines: Backtracking vs. Finite Automata
A backtracking regex engine tries one path at a time, which can be fast but also exponentially slow. A finite-automata engine (like Go's) checks all paths at once, guaranteeing linear time. The footgun is using a backtracking engine on untrusted user input.
Why it exists
Regular expressions are powerful but can be dangerously slow. A seemingly simple pattern against a crafted input can cause some engines to run for seconds, minutes, or even hours, creating a denial-of-service vulnerability. This happens because the engine's strategy for finding a match can lead to an exponential number of steps.
The mental model
An engine's strategy determines its performance. A backtracking engine (like those in Python, Perl, and PCRE) is an optimist: it tries one possible match path, and if it fails, it "backtracks" to try another. A finite-automata engine (like Google's RE2, used in Go and Rust) is a pessimist: it considers all possible matches simultaneously in a single pass.
How it works
The backtracking engine's optimism can make it fast for simple cases where the first path works. But for complex patterns with many alternatives or nested quantifiers, a malicious input can force it to explore an exponential number of paths. This is called "catastrophic backtracking." A finite-automata engine builds a state machine where each input character transitions it to a new set of possible states. Since it never backtracks, its runtime is guaranteed to be linear in the length of the input string, regardless of the pattern's complexity.
When to use it
Use a finite-automata engine (like Go's default, or Rust's regex crate) whenever you're processing a regular expression from an untrusted source, like a user search query or a web form validation. Its primary guarantee is safety: the match time is predictable and won't crash your server. It is the correct choice for production systems prioritizing stability and security over niche features.
When not to use it
Backtracking engines are not inherently bad; they represent a different trade-off. They often support advanced features like backreferences and lookarounds, which are difficult or impossible to implement in a pure finite-automata engine with linear-time guarantees. If you absolutely need those features and you completely control the regex pattern and its inputs, a backtracking engine may be suitable.
One canonical example
Google's RE2 library was designed explicitly to handle regexes from untrusted users safely. It guarantees linear time performance and has configurable memory limits, preventing Regular Expression Denial of Service (ReDoS) attacks. This philosophy is why languages like Go and Rust adopted this model for their standard regex libraries, prioritizing production safety over feature parity with older engines like PCRE.
Interview question
What is the primary risk associated with using a backtracking regex engine when processing untrusted user input?
- a.It often fails to support advanced regex features like backreferences and lookarounds.
- b.It can be vulnerable to Regular Expression Denial of Service (ReDoS) attacks due to exponential time complexity.Correct
- c.It might consume excessive memory by attempting to store all possible match paths simultaneously.
- d.It guarantees linear time performance, making it unsuitable for complex pattern matching.
Why? this is the answer
The card highlights that backtracking engines can suffer from 'catastrophic backtracking' on crafted inputs, leading to exponential time complexity and a denial-of-service vulnerability. Finite-automata engines, conversely, guarantee linear time and are recommended for untrusted input.
Just read this? Test yourself on what you have been reading.
Read the original → github.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles