Under the Hood: The Research Behind Microsoft SQL, part 4
This is a really interesting type of engineering paper. No new architecture, no new theory. Just a team looking hard at a bottleneck everyone had accepted for twenty years, and removing it.
The bottleneck was a single byte. Yes, really.
Concurrent Updates to Pages with Fixed-Size Rows Using Lock-Free Algorithms (VLDB 2020) is about how SQL Server keeps track of free space, and how fixing one small piece of that produced up to 5x throughput on some workloads.
The setup
Start with something you’d actually write in an app. Each request to your app runs a stored procedure that creates a temp table, fills it with a few thousand rows, does some work, and drops it. Or you have an ingestion service where a few hundred sessions insert rows at the same time. Nothing too crazy, but likely pretty common.
SQL Server stores everything in 8 KB pages. Permanent tables, temp tables, indexes, all of it. When the page you’re writing to fills up, the engine needs a fresh one and has to find one that isn’t already in use. When you drop that temp table, its pages go back into circulation. So every insert-heavy operation is quietly also a stream of “give me a free page” and “here’s one back” requests. One call from your application can trigger hundreds of them.
To answer those quickly, SQL Server keeps a sign-out sheet. It’s called a Page Free Space page (now I know what PFS stands for!), and it has one line for every page it tracks: taken or free, how full, a few other details. One Page Free Space page (aka a sheet) covers about 64 MB worth of pages. I’m not sure if I’m laughing or frustrated at that choice of naming “Page Free Space page,” perhaps I can revisit later.
Grabbing a page means marking its line. Handing one back means clearing it. You never see this. It’s bookkeeping. Another one of those things I never really thought about a database engine, before reading this paper.
The problem
Here’s the rule that caused twenty years of trouble: only one person could touch the sheet at a time.
Not one person per line. One person, period. Two sessions creating two completely unrelated temp tables, needing two different pages, took turns. Not because they wanted the same thing, but because their bookkeeping happened to live on the same sheet.
“The exclusive latch on the page prevents other threads from modifying the page at the same time, reducing the concurrency, and negatively impacting the throughput of the system.”
The paper is refreshingly blunt about how long this had been going on:
“Customers of SQL Server have faced similar contention on space management pages like PFS for decades.”
If you’ve ever watched a server crawl while the CPU sat mostly idle, this is one of the classic causes. In the wait statistics it shows up as PAGELATCH_UP. I should note for all the Microsoft SQL veterans, you know this much better than I do. You experienced it and suffered with it, and tried to find ways around it. Please forgive me for just learning about it without truly experiencing it!
Then Constant Time Recovery, the subject of part 2 of this blog series, made it worse. That feature needs to update the sheet more often (because it made the row history store durable). A change that removed hour-long outages pushed harder on a twenty-year-old traffic jam.
The fix
The team noticed something that had been true the whole time: two sessions never actually need to write on the same line. Before you can touch a line on the sheet, you already have exclusive control of the page that line describes. So the conflict the rule was preventing couldn’t happen in the first place. Everyone was queuing to avoid a collision that was already impossible. So they changed the rule! Now many sessions can stand at the sheet at once, and each writes only its own line. Each write uses a processor instruction that completes as one indivisible step, so a line can never be caught half-updated.
That’s the whole idea! The rest of the paper is the unglamorous work of making it safe that I can’t admit to grokking fully, but to summarize: keeping a shared counter on the sheet readable while people are writing to it, making sure the background process that saves changes to disk doesn’t take its snapshot mid-update, and a few similar details.
The part I find most interesting is how they handled old code. Plenty of existing code assumed the old rule and read the entire sheet expecting it to hold still. Rather than track down and rewrite every one of those callers, they made any request that asks for the old behavior automatically get it. Old code keeps its guarantee. New code gets the speed. That’s the difference between a clever idea and a shippable one.
What it bought
A transaction processing benchmark on a 40-core machine:
|
Throughput |
CPU used |
| Before |
475,423 |
45.3% |
| After |
787,100 |
98.5% |
About 65% more work. But look at the CPU column. The machine wasn’t slow. It was waiting.
A workload doing nothing but creating temp tables, 400 sessions at once:
| Machine |
Before |
After |
| 2-socket, 256 GB |
10 min, 10% CPU |
under 2 min, 32% CPU |
| 8-socket, 6 TB |
11.5 min, 1% CPU |
5 min, 83% CPU |
Roughly 5x and 2x. That 1% CPU on a large server is the number I’d put on a slide.
A real customer workload:
|
Requests/sec |
CPU |
Top wait |
| Before |
350 |
35% |
PAGELATCH_UP at 38.5% |
| After |
1,035 |
63% |
gone from the top waits |
Three times the throughput. Same hardware, same application, no code changes. The overhead was about 156 KB of memory for a 100 GB database. At publication the feature was running on more than five million databases in Azure SQL Database.
Where you see this today
You don’t configure this. It’s how the engine works now, on by default in SQL Server 2019 and later and in Azure SQL/Fabric SQL. Pretty neat!
What’s changed since the paper
The same technique spread to the other maps. PFS pages aren’t the only bookkeeping SQL Server keeps. SQL Server 2022 added similar concurrency improvements for GAM and SGAM pages, which track space at a coarser level and had the same style of bottleneck. Don’t ask me about these in detail, I don’t know much about them (yet).
A lot of old tuning advice is now obsolete. For years the standard workaround for allocation contention was trace flags 1117 and 1118 plus a specific number of tempdb data files (so I’m told). Those two trace flags stopped being necessary in SQL Server 2016, when the behavior became built in. Combined with this paper’s work and memory-optimized tempdb metadata, much of the folklore (lol sorry) you’ll find in older blog posts is solving problems the engine no longer has.
If you’re carrying tuning steps from a 2016 runbook, that’s worth a fresh look. Hopefully you aren’t, because it’s out of support, but I’ve heard of earlier versions running in production…
Why this matters if you think the engine has stopped changing
The perception that Microsoft SQL is finished software is understandable. SQL Server has been around a long time and the surface looks stable. The reality is that someone questioned a rule from the 1990s, proved the thing it was protecting against couldn’t happen, and removed it. A customer workload went from 350 to 1,035 requests per second without touching the application or the hardware.
The authors’ own conclusion is a decent engineering principle:
“The work showed that taking a closer look at a particular subsystem that is based on lock-based algorithms and changing them to be lock-free can help in significantly boosting the performance of the entire system.”
Most systems have a byte like this somewhere, can you find another??
Credits
Concurrent Updates to Pages with Fixed-Size Rows Using Lock-Free Algorithms was written by Raghavendra Thallam Kodandaramaih, Hanuma Kodavalla, and Girish Mittur Venkataramanappa (Microsoft).
Next steps
The post Your server wasn’t slow, it was waiting. We fixed it with this paper appeared first on Azure SQL Dev Corner.