Spinlock Contention for DBAs: Measure a Delta, Not a Total
CPU on the instance sits near 100%. Wait stats don’t show much beyond some SOS_SCHEDULER_YIELD. Someone runs SELECT * FROM sys.dm_os_spinlock_stats ORDER BY spins DESC;, sees a spinlock with a few billion spins at the top, and reports it as the cause.

That number may be real, but it covers everything since the service started. On its own it can’t tell you whether that spinlock was busy five minutes ago or five days ago. I wanted to see what a useful spinlock measurement looks like, so I built a workload that’s meant to cause contention and measured it on two local instances.
Spinlocks, Latches and Locks
Three kinds of synchronization in SQL Server get mixed up a lot.
Locks protect logical, transactional consistency. They’re held for the duration of the logical transaction. Latches are internal to the engine and protect in-memory structures, such as pages in the buffer pool, for the duration of the physical operation.[1]
Spinlocks also protect internal data structures, but for very short periods. The difference from a latch is what a thread does when the resource is taken. A thread that can’t get a latch yields the CPU right away, which means a context switch. A thread that can’t get a spinlock runs a loop and keeps checking whether the structure is free. After a while it gives up and yields, and Microsoft calls that a backoff.[2] Spinning avoids the cost of a context switch when the hold time is short. The trade-off is that a spinning thread uses CPU while it waits, so heavy contention can show up as high CPU use.[3]
What the DMV Columns Mean
sys.dm_os_spinlock_stats returns one row per spinlock type.[3] My SQL Server 2019 instance returned 398 rows and my SQL Server 2025 instance returned 530.
collisions: how many times a thread tried to acquire the spinlock and found another thread holding it.spins: how many times a thread ran the loop while waiting.spins_per_collision: spins divided by collisions.sleep_time: milliseconds threads spent sleeping after a backoff.backoffs: how many times a spinning thread failed to get the spinlock and yielded the scheduler.
The values are cumulative since the instance started or since someone last cleared them with DBCC SQLPERF('sys.dm_os_spinlock_stats', CLEAR).[3] Clearing them resets the counters for everyone, including any monitoring tool that reads them, so I’d rather take a delta.
The Microsoft whitepaper says billions or trillions of spins are common on a busy system that has been running for a long time. It also says a high spin count for one type isn’t enough to show that the workload is affected.[2]
When It’s Worth Looking
The whitepaper lists the conditions that together may point to spinlock contention:[2]
- A high number of spins and backoffs for one spinlock type.
- Heavy CPU use or CPU spikes, with high signal waits on
SOS_SCHEDULER_YIELD. - High concurrency.
- CPU use and spins rising out of proportion to throughput.
It adds that most increases in CPU have other causes, such as queries reading more pages as data grows, or plan changes, and that those should be ruled out first. It says the problem has generally been seen on systems with 24 or more cores, most often more than 32.[2]
Capturing a Delta
A delta is the same idea I use for wait stats in Wait Stats over Time: take a snapshot, let the workload run, take another, and subtract. The whitepaper has a longer version of this script in its appendix.[2] My measurements came from a PowerShell script that does the same thing from a controller session. This is the T-SQL version, which I checked on both instances. It reads DMVs and writes only to a temp table:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
USE [master]; CREATE TABLE #spin_before ( [name] nvarchar(256) NOT NULL , [collisions] bigint NULL , [spins] bigint NULL , [sleep_time] bigint NULL , [backoffs] bigint NULL , [captured] datetime2(3) NOT NULL ); INSERT INTO #spin_before ( [name] , [collisions] , [spins] , [sleep_time] , [backoffs] , [captured] ) SELECT [s].[name] , [s].[collisions] , [s].[spins] , [s].[sleep_time] , [s].[backoffs] , SYSDATETIME() FROM [sys].[dm_os_spinlock_stats] AS [s]; /* Run the workload now, or wait for a fixed window. */ WAITFOR DELAY '00:01:00'; SELECT TOP (10) [s].[name] , [s].[collisions] - [b].[collisions] AS [collisions] , [s].[spins] - [b].[spins] AS [spins] , CONVERT(decimal(18, 1), ([s].[spins] - [b].[spins]) * 1.0 / NULLIF([s].[collisions] - [b].[collisions], 0)) AS [spins_per_collision] , [s].[sleep_time] - [b].[sleep_time] AS [sleep_time_ms] , [s].[backoffs] - [b].[backoffs] AS [backoffs] , DATEDIFF(millisecond, [b].[captured], SYSDATETIME()) AS [window_ms] FROM [sys].[dm_os_spinlock_stats] AS [s] INNER JOIN #spin_before AS [b] ON [b].[name] = [s].[name] ORDER BY [s].[spins] - [b].[spins] DESC , [s].[collisions] - [b].[collisions] DESC; DROP TABLE #spin_before; |
Take the same delta while the system is quiet, so you know what normal background activity looks like. If the busy delta doesn’t stand well clear of the quiet one, I’d stop looking at spinlocks.
The Test
The whitepaper describes LOCK_HASH contention when many threads read the same row at once, for example a shared lock on a frequently read row.[2] The DMV page says LOCK_HASH protects the lock manager’s hash table.[3] So I built a one-row table and pointed many sessions at it.
The table is in a scratch database and has a clustered primary key. The database uses the default READ COMMITTED isolation level, without read committed snapshot. Each worker session ran this loop for 60 seconds:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
USE [sat_spinlock]; SET NOCOUNT ON; DECLARE @v int; DECLARE @i int; DECLARE @n bigint = 0; DECLARE @end datetime2(3) = DATEADD(second, 60, SYSDATETIME()); WHILE SYSDATETIME() < @end BEGIN SET @i = 0; WHILE @i < 500 BEGIN SELECT @v = [h].[val] FROM [dbo].[hot_row] AS [h] WHERE [h].[id] = 1; SET @i += 1; END; SET @n += 500; END; SELECT @n AS [iterations] , [r].[cpu_time] FROM [sys].[dm_exec_requests] AS [r] WHERE [r].[session_id] = @@SPID; |
A PowerShell script took the delta around three 60-second windows on each instance: idle, one worker, and 14 workers (I ran the 14-worker window twice). The instances were SQL Server 2019 (15.0.4500.2, Developer) and SQL Server 2025 (17.0.5015.4, Enterprise Developer), on one laptop with an Intel Core Ultra 7 165U (12 cores, 14 logical processors). I tested one instance at a time. The script also checked for other user requests every five seconds. There were none on SQL Server 2025. On SQL Server 2019 there was always one, a Database Mail session waiting on BROKER_RECEIVE_WAITFOR. A second request showed up at some point in each 2019 window, including the idle one, and I didn’t capture what it was.
Results
The top spinlocks by spins in each window:
| Instance | Window | Top spinlock | Collisions | Spins | Spins per collision | Backoffs |
|---|---|---|---|---|---|---|
| SQL Server 2019 | Idle | LOCK_HASH | 159 | 2,954 | 18.6 | 24 |
| SQL Server 2019 | 1 worker | SOS_OBJECT_STORE | 12,793 | 99,469 | 7.8 | 0 |
| SQL Server 2019 | 14 workers, run 1 | LOCK_HASH | 50,634,621 | 1,428,524,454 | 28.2 | 16,930,340 |
| SQL Server 2019 | 14 workers, run 2 | LOCK_HASH | 53,469,881 | 1,557,351,978 | 29.1 | 18,674,966 |
| SQL Server 2025 | Idle | LOCK_HASH | 205 | 967 | 4.7 | 21 |
| SQL Server 2025 | 1 worker | LOCK_HASH | 247 | 1,170 | 4.7 | 20 |
| SQL Server 2025 | 14 workers, run 1 | LOCK_HASH | 42,091,916 | 373,271,825 | 8.9 | 14,161,496 |
| SQL Server 2025 | 14 workers, run 2 | LOCK_HASH | 40,775,584 | 395,286,977 | 9.7 | 13,119,075 |
In all four 14-worker windows, the second spinlock was OPT_IDX_STATS, with 8.5 to 8.8 million spins on SQL Server 2025 and 56.1 to 58.5 million on SQL Server 2019. Nothing else reached 100,000 spins in those windows. Signal wait time on SOS_SCHEDULER_YIELD also rose, from 0 ms idle to 121,412 and 181,840 ms on SQL Server 2025, and from 120 ms to 28,237 and 44,652 ms on SQL Server 2019.
One worker reading the same row about 9 million times (SQL Server 2025) or 12.6 million times (SQL Server 2019) barely moved LOCK_HASH. On SQL Server 2025 the one-worker window was within 250 spins of the idle window. Fourteen sessions reading the same row moved it by hundreds of millions of spins.
Throughput and CPU, from the worker sessions:
| Instance | Workers | Rows read | Worker CPU (ms) | Rows read per CPU second |
|---|---|---|---|---|
| SQL Server 2019 | 1 | 12,609,500 | 59,983 | 210,218 |
| SQL Server 2019 | 14 | 50,654,500 | 819,128 | 61,840 |
| SQL Server 2019 | 14 | 51,461,000 | 805,473 | 63,889 |
| SQL Server 2025 | 1 | 8,980,500 | 59,985 | 149,712 |
| SQL Server 2025 | 14 | 45,291,500 | 718,795 | 63,010 |
| SQL Server 2025 | 14 | 45,286,500 | 658,109 | 68,813 |
With 14 workers, the sessions used 11 to 14 times the CPU of one worker and read 4 to 5 times as many rows. That’s the kind of gap between CPU and throughput the whitepaper describes. I can’t attribute all of it to spinning, though. This laptop CPU has a mix of faster and slower cores, so 14 busy threads can’t each run as fast as a single thread on the fastest core.
What the Raw Totals Would Have Shown
After the tests, I read the cumulative totals on both instances. On SQL Server 2019, LOCK_HASH had 3,000,966,125 spins since startup. My two 14-worker windows account for 2,985,876,432 of them, so most of that number came from two minutes of synthetic test that had finished just before I looked.
On SQL Server 2025 the raw top three were LOCK_HASH, OPT_IDX_MISS_KEY and OPT_IDX_STATS. OPT_IDX_MISS_KEY had 117,145,779 spins, but it wasn’t in the top ten of any of my windows. Something outside my measured windows caused those spins, and the raw total doesn’t say when.
What I Can and Can’t Tell
LOCK_HASH matches the whitepaper’s hot-row case. I didn’t check which lock resource was hot. The row lock, the page lock and the table-level intent lock all go through the lock manager, and I can’t tell them apart from this DMV. OPT_IDX_STATS is listed as “Internal use only” on the DMV page.[3] I can see that it rose with this workload, but I can’t say what structure it protects.
Spins per collision for LOCK_HASH were about 28 to 29 on SQL Server 2019 and about 9 to 10 on SQL Server 2025. Microsoft says internal changes in SQL Server 2022 make spinlocks more efficient.[3] That may explain part of the difference, but these are two builds with different editions on one machine, and I can’t separate those factors.
The DMV doesn’t tell you which code path is spinning. For that, the whitepaper uses an Extended Events session on sqlos.spinlock_backoff, filtered on the spinlock type, with the package0.callstack action and a bucketizer target. To turn those call stacks into function names you need the debug symbols (sqlservr.pdb) for your exact build, and the sample script turns on trace flag 3656 for symbol resolution.[2] I didn’t install symbols or create that session, so I have no stack data.
If you adapt the sample, check the type numbers first. Its comments look up a map named spinlock_types, and the session filters LOCK_HASH as type 61. On my instances that map name returned no rows from sys.dm_xe_map_values. The map was sqlservr_spinlock_types, with LOCK_HASH as 157 on SQL Server 2019 and 199 on SQL Server 2025. I haven’t confirmed that the event’s type filter uses those values.
SQL Server 2025 also has a SPINLOCK_EXT wait type, which needs trace flag 8134.[2] I didn’t turn on any trace flags, so I didn’t test it.
For LOCK_HASH on hot rows, the whitepaper suggests spreading the hot reference data across more rows, or using optimistic concurrency or optimized locking where possible.[2] I didn’t test any of those here.
What this test doesn’t tell you:
- I tested one build of each version, on one laptop with 14 logical processors. That’s below the core counts where Microsoft says the problem usually appears.
- The workload is synthetic. Real application traffic may be spread over many rows, rather than 14 sessions reading one row in a tight loop.
- Each window was 60 seconds, with two runs at 14 workers. That shows the order of magnitude, but it’s too few runs to compare the two versions closely.
- The DMV is instance-wide. I checked for other user requests, but background tasks still count, which is why the idle windows aren’t zero.
Related: THREADPOOL Waits: Reproducing Worker Thread Exhaustion in SQL Server, Finding the Queries That Use the Most CPU, and What Each Source Misses and Tempdb Latch Contention on SQL Server 2019 and 2025.
If you’ve chased spinlock contention on a production system, I’d like to hear what the delta showed and what you found behind it. Find me on Bluesky or LinkedIn.
References
- Diagnose and resolve latch contention on SQL Server - Microsoft Learn. Compares latches and locks: latches give memory consistency for the duration of a physical operation, and locks give logical transactional consistency for the duration of the transaction. ↩
- Whitepaper: Diagnose and resolve spinlock contention on SQL Server - Microsoft Learn, originally published by the SQLCAT team. Defines spinlocks and backoffs, lists the symptoms and core counts, describes LOCK_HASH contention on frequently read rows, and gives the delta and Extended Events scripts. ↩
- sys.dm_os_spinlock_stats (Transact-SQL) - Microsoft Learn. Column definitions, the note that values are cumulative since startup or the last clear, the SQL Server 2022 efficiency note, and descriptions of LOCK_HASH and OPT_IDX_STATS. ↩