In-Memory OLTP as a shock absorber: how a buffer in RAM absorbs an insert storm
Many clients write every message as a single row into a table. Day to day, that works fine. When a burst arrives, the writers pile up and the application waits along with them. We recreated such a burst in the lab and measured, step by step, what helps: from the index option through delayed durability to a small table in memory that absorbs the burst and passes it on at leisure.
In short
- Single-row inserts have two brakes. Every commit waits until its log record is written (
WRITELOG). And all new rows want the same last page of the index (PAGELATCH_EX). - The usual levers each release one brake.
OPTIMIZE_FOR_SEQUENTIAL_KEYorders access to the last page, and delayed durability removes the wait for the log flush. In the lab, the accepted rate during the burst rose from 2,546 to 6,255 inserts per second with them, out of 64,000 requested. - A buffer in RAM releases both. A memory-optimized table has no pages and no latches. With
DURABILITY = SCHEMA_ONLY, it doesn't write any log either. It accepted all 64,000 requested messages per second, in 0.08 ms per insert on average. A drain job moved them into the actual table in batches. - The price is durability. Whatever is still in the buffer is gone after a restart or failover. In the lab, that was 55,000 to 564,000 rows at the peak, depending on the run. Whether that is acceptable is for the people responsible for the application to decide, not the database.
- The drain is the new bottleneck. A second drain into the same target table made things worse in the lab: both fought over its last page.
From practice: when every message is a commit
We come across this pattern again and again. An application logs what its clients do, and every client writes each message as its own row, in autocommit. Hundreds of connections, each with a few messages per second: the table carries that without trouble for a long time.
Then a burst arrives: the start of a shift, a batch run, an error that triggers messages in all clients at once. For a few seconds, twenty times as many rows want into the table. The table can't handle it. But the burst doesn't disappear, it lands somewhere else: in the clients waiting for their insert. The logging slows down the very application it is only meant to record in passing.
The obvious levers are well known and quickly set. In-Memory OLTP isn't among them, because it is considered a tool for the core of an OLTP application: rebuild tables, compile procedures natively, handle conflicts differently. Here, we use it in a small way. A single table in RAM in front of the actual log table, a job that empties it, and a different target for the insert. Microsoft described the pattern as early as 2013 and gave it a fitting name: Shock Absorber.
The test
Test setup: SQL Server 2022 CU27 (16.0.4295.3) in a container with eight schedulers and 7 GB
max server memory, log file on its own SSD. 64 connections each write one row per insert. In the base load, each sends 15 messages per second, 960 in total. From second 10 to 25, each requests 1,000 per second, 64,000 in total, then back to base load until second 60. Two runs per step in alternating order, the median is reported; the three buffer steps ran two more times in a follow-up, and the ranges cover all four runs. Synthetic data, Linux container. Details under Method.
The log table is built the typical way: a clustered index on bigint IDENTITY, a timestamp, a level, a client identifier and a text of about 110 characters.
CREATE TABLE dbo.LogEntry ( id bigint IDENTITY CONSTRAINT PK_LogEntry PRIMARY KEY CLUSTERED, ts datetime2(3) NOT NULL, lvl tinyint NOT NULL, client varchar(64) NOT NULL, msg nvarchar(200) NOT NULL); -- every connection, every message: one insert, one commitINSERT dbo.LogEntry (ts, lvl, client, msg)VALUES (SYSUTCDATETIME(), 2, @client, @msg);Each second, we measured how many inserts were accepted and how long they took. Every 250 ms, we also recorded the fill level of the buffer and the arrival in the target table. After each run, we checked that every row written had either arrived or was still in the buffer. In none of the 22 runs was anything lost.
Step by step
Step
accepted inserts per second during the burst (seconds 10 to 25)
Inserts per second accepted from 64 connections during the burst, median of the runs per step, linear scale from zero. Top wait: the largest wait in the first run of each step; PREEMPTIVE_OS_FLUSHFILEBUFFERS and WRITELOG are combined as log flush, SOS_SCHEDULER_YIELD is shown as CPU, WAIT_XTP_HOST_WAIT is left out. SQL Server 2022 CU27 in a Linux container, eight schedulers.
During the burst, 64,000 messages per second were requested. The chart shows how many of them each step actually accepted. All steps handled the base load of 960 per second without effort.
Step 0, straight into the table: 2,546 per second. During the burst, each insert took 25 ms on average, in the base load 22 to 24 ms. For almost the whole time, the commit was waiting for the log flush (WRITELOG). That is the most important finding of this step: the individual insert hardly gets slower during the burst. Fewer of them simply get through. Over the 15 seconds, about 922,000 messages were left unaccepted, stuck in the clients.
Step 1, OPTIMIZE_FOR_SEQUENTIAL_KEY: 2,724 per second. The option orders access to the last index page. Here, though, the page wasn't in the way, the log was. As shown in the OFSK article: when the log flush is the brake, the option brings little.
Step 2, delayed durability: 3,648 per second. With COMMIT … WITH (DELAYED_DURABILITY = ON), the commit returns before its log is written. The wait for the flush disappears. The second brake immediately steps forward: now the writers waited at the last index page (PAGELATCH_EX).
Step 3, both together: 6,255 per second. Only now does the index option help as well. The wait moves into its flow control (BTREE_INSERT_FLOW_CONTROL). That is two and a half times the starting point and still only a tenth of what was requested. A single table with an ascending key can't deliver more here.
Step 4, a buffer in RAM, durable: 2,964 per second. Here is the surprise you should know about. A memory-optimized table with DURABILITY = SCHEMA_AND_DATA has no pages and no latches. But it writes log, and every commit still waits for the flush. That puts it almost back at step 0. So In-Memory OLTP alone is no turbo. It only takes the page out of the way, not the log.
Step 5, buffer with delayed durability: 60,765 per second. Now both brakes are released: no page that everyone waits for, and no wait for the flush. The buffer accepted almost the entire burst, in 0.5 ms per insert on average. A drain job moved the rows into the actual table in batches of up to 50,000.
Step 6, buffer with DURABILITY = SCHEMA_ONLY: 63,993 per second. Without a log on insert, the last bit of overhead disappears as well. The buffer accepted everything that came in, in 0.08 ms per insert on average. Only the drain still writes log, when it inserts the rows into the target table: about 390 bytes per row instead of about 2,000 on the direct path.
The burst over time
Compare with 0 Direct:
Backlog in the application
requested but not yet accepted, summed over the burst
0 Direct
921,943
messages in the backlog
6 SCHEMA_ONLY
436
messages in the backlog
The lab clients don’t catch up after the burst; a real application would keep waiting.
Buffer fill level
rows in RAM not yet in the table
6 SCHEMA_ONLY
peak 564,431
empty 5.4 s after the burst ends
all runs: peak 55,000–564,000, empty after 0.5–5.4 s
Measured time series per step from one run, seconds 0 to 60; the tanks and peaks belong to that run, while the legend shows the median of all runs. The buffer fill level varies widely from run to run, so the range is shown below the tank. Requested: 64 connections with 15 messages per second each, 1,000 during the burst. Accepted: completed inserts per second. Arrival in the table: growth of the target table per second, the same as “accepted” for the direct steps. Axis up to 70,000; values above it are clipped and labeled. The tanks have their own scales, starting at zero.
The staircase shows averages. More interesting is what happens over the 60 seconds. The chart plays back the measured curves: at the top, what was requested and what was accepted, below that two reservoirs for the burst.
The burst doesn't disappear. The question is where it lands.
Straight into the table, it lands in the application. During the burst, a steady 2,500 or so messages per second were accepted, and the rest waited. At the end of the 15 seconds, about 922,000 of the 960,000 requested messages had not been accepted. In the lab, the writers simply stop afterwards; a real application would have kept waiting: threads blocked, user interfaces frozen, timeouts. The logging then holds up the work it was only supposed to log.
With the buffer, it lands in the database's memory. The clients got rid of their messages in fractions of a millisecond. The fill level rose during the burst by the difference between intake and drain, up to 55,000 to 564,000 rows depending on the run. After the burst, the drain got the CPU back and emptied the buffer within 0.5 to 5.4 seconds. For the application, the burst was over after 15 seconds, for the database a few seconds later.
The drain is the new bottleneck
With the buffer, the bottleneck hasn't gone away, it has moved. Now what decides is how fast a single drain gets rows into the target table. During the burst, it competed with 64 writers for eight cores and managed 28,000 to 62,000 rows per second, depending on the run. Afterwards it was 84,000 to 103,000. How much CPU it got during the burst varied from run to run, and with it the peak fill level: 55,000 to 564,000 rows.
With the durable buffer (step 5), the drain is slower because deleting from the buffer goes into the log as well. During the burst, it managed only 11,000 to 22,000 rows per second, and the fill level rose to 641,000 to 781,000 rows. The buffer was empty about 6 to 7 seconds after the burst.
For planning, this means: the buffer absorbs bursts as long as, on average, no more arrives than the drain can handle. If the continuous load is above that, it grows steadily until memory runs out. Then no buffer helps, only a faster target table or fewer messages.
The obvious idea would be to simply start a second drain. We tried it: two buffer tables, each for half of the connections, each with its own drain, both into the same target table. It got worse. Together, the two drains managed only 7,000 to 31,000 rows per second during the burst. The buffer rose to as much as 941,000 rows and was empty only 8 to 51 seconds after the burst. The two drains fought over the last page of the target table (PAGELATCH_EX), exactly the end of the index the buffer was supposed to protect the clients from. Anyone who really wants to distribute the drain needs separate targets or a different target, such as a columnstore index, as Microsoft suggests for this pattern. We haven't measured that.
Rows in the buffer tables, sampled every 250 ms, axis starting at zero; with two drains, the sum of both buffers. Curves: one run each, including the time the drain keeps working after the burst. Legend: range across all runs, since the peaks vary widely.
What the buffer costs
Step
Accepted during the burst 0–70,000/s
What a restart or failover can losesafevolatile
0Direct
1+ OPTIMIZE_
2Delayed durability
3DD + OFSK
4Durable buffer
5Buffer + DD
6Buffer SCHEMA_ONLY
Confirmed means the commit has returned to the application. Delayed durability confirms before the log record is written; a SCHEMA_ONLY buffer writes no log at all. Accepted inserts during the burst as in the step chart, linear scale from zero.
The speed has a price, and it belongs before the decision, not after.
Durability. Each step changes what can be lost in a crash, restart or failover:
- Direct, also with OFSK: nothing that was confirmed. Every commit returns only once its log record is written.
- Delayed durability: the most recently confirmed commits whose log wasn't on disk yet. Microsoft explicitly doesn't exclude the planned restart here either. In an availability group, there is no guarantee that the secondary knows them (Control transaction durability).
- Buffer
SCHEMA_ONLY: everything still in the buffer at the moment of the restart or failover. In the lab, that was 55,000 to 564,000 rows at the peak, depending on the run, and almost nothing in quiet operation. The table itself remains, just empty.
For log messages, that can be acceptable; for financial postings, never. The decision belongs to the people responsible for the application. Anyone who can't lose anything takes the durable buffer and gives up speed, or stays on the direct path and works on the log path.
Memory. The buffer lives in the instance's memory, next to the buffer pool. In the lab, 564,000 rows took up about 160 MB. Deleted rows are only freed a little later by garbage collection. In Standard Edition, memory-optimized data may take up at most 32 GB per database, in Express 352 MB. If the drain doesn't run, the buffer keeps growing until the inserts fail with an out-of-memory error, and then the application is stuck again. With Resource Governor, the database can be bound to a pool of its own that limits the memory (instructions). Up to SQL Server 2022, that works only in Enterprise Edition; from 2025, in Standard as well.
Operations. Up to SQL Server 2022, the filegroup for memory-optimized data can only be removed together with the database; from 2025, separately. Readers of the log table see new entries only after the next drain. If a drain fails, for example on a row the target table rejects, the whole batch stays in the buffer. The fill level and the age of the oldest entry therefore belong in monitoring (query 1).
Application. The change is easiest when the clients write through a procedure. Then only its target changes. If they write directly with INSERT into the table, the application has to change too.
Build plan
The whole change consists of four parts. The names are examples. We tested the code on SQL Server 2019, 2022 and 2025.
1. Filegroup and buffer. The database needs a filegroup for memory-optimized data, once. The buffer has the same columns as the log table and its own key. A nonclustered index is enough, because it is only read in order.
-- Once: filegroup for memory-optimized data.-- Up to SQL Server 2022 it can only be removed together with the database; from SQL Server 2025 on, separately.ALTER DATABASE AppDb ADD FILEGROUP AppDb_mod CONTAINS MEMORY_OPTIMIZED_DATA;ALTER DATABASE AppDb ADD FILE (NAME = N'AppDb_mod', FILENAME = N'D:\SQLData\AppDb_mod') TO FILEGROUP AppDb_mod;-- Allows access to memory-optimized tables in explicit transactions without a table hint (drain)ALTER DATABASE AppDb SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = ON;GO -- The buffer. SCHEMA_ONLY writes no transaction log.-- Whatever is still in the buffer is gone after a restart or failover.CREATE TABLE dbo.LogBuffer ( id bigint IDENTITY CONSTRAINT PK_LogBuffer PRIMARY KEY NONCLUSTERED, ts datetime2(3) NOT NULL, lvl tinyint NOT NULL, client varchar(64) NOT NULL, msg nvarchar(200) NOT NULL) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_ONLY);2. Writing. The clients call a procedure instead of writing directly into the log table.
CREATE OR ALTER PROCEDURE dbo.WriteLog @lvl tinyint, @client varchar(64), @msg nvarchar(200)ASBEGIN SET NOCOUNT ON; INSERT dbo.LogBuffer (ts, lvl, client, msg) VALUES (SYSUTCDATETIME(), @lvl, @client, @msg);END;If the buffer contents are to survive a restart, the buffer is created as durable: the same table, here dbo.LogBufferDurable, with DURABILITY = SCHEMA_AND_DATA. It only becomes fast with delayed durability (step 5). In a crash, that costs the last few commits, which is considerably less than an entire buffer. It is set deliberately for this commit only. The database allows it for that purpose but doesn't force it:
ALTER DATABASE AppDb SET DELAYED_DURABILITY = ALLOWED;GOCREATE OR ALTER PROCEDURE dbo.WriteLogDelayed @lvl tinyint, @client varchar(64), @msg nvarchar(200)ASBEGIN SET NOCOUNT ON; BEGIN TRAN; INSERT dbo.LogBufferDurable (ts, lvl, client, msg) VALUES (SYSUTCDATETIME(), @lvl, @client, @msg); COMMIT TRAN WITH (DELAYED_DURABILITY = ON);END;3. Draining. For each batch, the drain fixes the upper bound once and then copies and deletes exactly these rows. A TOP without a fixed bound in both statements could otherwise hit different rows. A SQL Agent job starts the procedure every minute. It works for 55 seconds and waits 200 ms when the buffer is empty. Exactly one drain per target table; the section on the drain above shows why. With the durable buffer, the same drain reads from dbo.LogBufferDurable.
CREATE OR ALTER PROCEDURE dbo.DrainLogBuffer @batch int = 50000, @seconds int = 55ASBEGIN SET NOCOUNT ON; SET XACT_ABORT ON; DECLARE @until datetime2(3) = DATEADD(second, @seconds, SYSUTCDATETIME()), @max bigint, @n int; WHILE SYSUTCDATETIME() < @until BEGIN BEGIN TRAN; -- fix the upper bound once, then copy and delete exactly these rows SELECT @max = MAX(id) FROM (SELECT TOP (@batch) id FROM dbo.LogBuffer ORDER BY id) AS t; IF @max IS NOT NULL BEGIN INSERT dbo.LogEntry (ts, lvl, client, msg) SELECT ts, lvl, client, msg FROM dbo.LogBuffer WHERE id <= @max ORDER BY id; SET @n = @@ROWCOUNT; DELETE dbo.LogBuffer WHERE id <= @max; IF @@ROWCOUNT <> @n THROW 50001, N'Drain: row count mismatch', 1; END; COMMIT; IF @max IS NULL WAITFOR DELAY '00:00:00.200'; END;END;In the target table, the entries get new keys; order and timestamps are preserved. A row that the target table rejects makes the whole batch fail. Anyone who has to expect such rows checks them in the drain beforehand and puts them in a table of their own.
4. Monitoring. Query 1 shows how full the buffer is, how far the drain is lagging behind and how much memory the table uses.
/* PSG · Shock absorber · Query 1: How full is the buffer? Run in the database that holds the buffer. Shows the rows in the buffer, how old the oldest entry is (that is, how far the drain is lagging behind) and how much memory the table uses. Used memory also includes deleted rows that garbage collection hasn't released yet. Caution: the query counts the whole buffer (COUNT_BIG, MIN). With several million rows that takes a moment and costs CPU. It doesn't change anything, but it shouldn't run every second; for ongoing monitoring, once a minute is enough. Permissions: SELECT on dbo.LogBuffer. In tests on SQL Server 2019, 2022 and 2025 this was also enough for the memory columns. Note: SSMS reports "Warning: The join order has been enforced because a local join hint is used." This comes from the system view and is harmless. OPTION (MAXDOP 1): the diagnostic query shouldn't tie up parallel workers. */SELECT b.row_count, DATEDIFF(second, b.oldest, SYSUTCDATETIME()) AS lag_s, CAST(m.memory_used_by_table_kb / 1024.0 AS decimal(12, 1)) AS used_mb, CAST(m.memory_allocated_for_table_kb / 1024.0 AS decimal(12, 1)) AS allocated_mbFROM (SELECT COUNT_BIG(*) AS row_count, MIN(ts) AS oldest FROM dbo.LogBuffer) AS bCROSS JOIN sys.dm_db_xtp_table_memory_stats AS mWHERE m.object_id = OBJECT_ID(N'dbo.LogBuffer')OPTION (MAXDOP 1);In the query test with 10,000 rows before the drain:
row_count lag_s used_mb allocated_mb
10000 1 1.2 1.3If lag_s stays large for minutes or row_count grows steadily, the drain can't keep up or has stopped. That belongs in alerting, before memory gets tight.
What the test shows and what it doesn’t
In-Memory OLTP doesn't have to be the big rebuild. A single table in memory, a job and a new insert target absorbed a burst in the lab that the table alone couldn't accept. But first comes the question of what may be lost in the worst case. Only then comes the choice of step.
Sources
- Microsoft SQL Server Team: In-Memory OLTP Common Design Pattern – High Data Input Rate/Shock Absorber (2013)
- SQL Server Storage Engine Team: Ingesting 1.4 million sustained rows per second with In-Memory OLTP & Columnstore (2017)
- Microsoft Learn: In-Memory OLTP overview and usage scenarios, Durability for memory-optimized tables, Control transaction durability, The memory-optimized filegroup, Bind a database to a resource pool, Transactions with memory-optimized tables
Read on: Optimize for sequential key · Slow under load · SQL Server transaction log full