Database, hourglass and a broken arrow to a disk, representing SQL Server delayed durability and log flush

SQL Server Delayed Durability: Faster Commits, Real Data Loss Risk

How SQL Server delayed durability returns commits before the log is hardened, how to enable it, when the data loss risk is acceptable and what to fix first for WRITELOG waits.

December 22, 2021

When a transaction commits in SQL Server, the client only gets the “done” response after the transaction log record has been written to disk. That wait guarantees the data survives even if the server crashes; but if the log disk is slow or the application sends lots of small transactions, every commit waiting for that write becomes the bottleneck, and WRITELOG waits go through the roof.

Delayed durability, introduced in SQL Server 2014, is an option that skips that wait: the commit returns successfully before the log is written to disk. The gain can be real, and so is the price: if the server crashes, the most recently committed transactions can be lost. This article explains how it works, when it’s acceptable, and the alternatives to try first.

SQL Server Performance Consulting

Seeing WRITELOG waits?

Aryasoft finds the source of log write bottlenecks at disk, transaction design and configuration level, and fixes it without compromising data safety.

Get Support

How It Works

When a delayed durable transaction commits, its log records stay in the in-memory log buffer first. The buffer is written to disk when:

  • The log buffer fills up.
  • A fully durable transaction commits in the same database.
  • sys.sp_flush_log is called.
  • SQL Server shuts down cleanly.

If the server goes down unexpectedly in the meantime (a power failure, an operating system crash, the process being killed), the log records in the buffer are lost, and transactions the application believes were committed don’t come back.

Critical

With delayed durability on, the most recently committed transactions can be lost if the server shuts down unexpectedly. The loss is limited to what was in the log buffer, but it isn’t zero. Don’t use it for data you can’t afford to lose, such as financial transactions, orders, payments and stock.

How to Enable It

The database-level setting takes three values:

-- DISABLED: the default, every commit is fully durable
-- ALLOWED : each transaction can choose
-- FORCED  : every commit is delayed durable
ALTER DATABASE StagingDB SET DELAYED_DURABILITY = ALLOWED;

-- Choosing at transaction level when ALLOWED
BEGIN TRAN;
    INSERT INTO dbo.LoadLog (Source, RowCnt) VALUES (N'ERP', 1500);
COMMIT TRAN WITH (DELAYED_DURABILITY = ON);

-- Flush pending log records to disk at a critical point
EXEC sys.sp_flush_log;

To check the current state:

SELECT name, delayed_durability_desc
FROM sys.databases
WHERE delayed_durability_desc <> N'DISABLED';

When Is It Acceptable?

  • Reproducible data: staging and ETL work tables that can be reloaded from the source.
  • Records where loss is tolerable: detailed application logs, telemetry or click data.
  • Bulk loads: load jobs that can simply be rerun if interrupted.

Limits: delayed durability doesn’t apply to cross-database or distributed transactions; those are always fully durable. In Always On, if the primary goes down unexpectedly, delayed durable transactions not yet sent to the secondary can be lost.

Try These First

Before switching to delayed durability, find the cause of the log write bottleneck:

  • Log disk latency: log write latency above a few milliseconds goes straight into commit time. Moving the log file to a fast, separate disk often fixes the problem at the root.
  • Very small transactions: inserting row by row in a loop with a commit per row means thousands of separate log writes. Grouping the work into reasonably sized batches with one commit each gives the same gain with no risk of data loss.
  • Unnecessary log volume: updating unchanged columns in large updates, or unnecessary indexes, increase log volume.
-- Log write latency (ms) and WRITELOG waits
SELECT DB_NAME(vfs.database_id) AS database_name,
       vfs.io_stall_write_ms / NULLIF(vfs.num_of_writes, 0) AS avg_write_ms
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS vfs
JOIN sys.master_files AS mf ON mf.database_id = vfs.database_id AND mf.file_id = vfs.file_id
WHERE mf.type_desc = N'LOG'
ORDER BY avg_write_ms DESC;

SELECT wait_type, waiting_tasks_count, wait_time_ms
FROM sys.dm_os_wait_stats
WHERE wait_type = N'WRITELOG';

Tip

Splitting a load job that commits row by row into transactions of 1,000 rows delivers most of the gain you’d get from delayed durability, with no risk of data loss. If you still need more after fixing the disk and the transaction design, use delayed durability only on databases where loss is acceptable, in ALLOWED mode, for targeted transactions.

Checklist

  • WRITELOG waits and log disk latency have been measured.
  • The log file is on a fast, separate disk.
  • Small transactions are grouped wherever possible.
  • Delayed durability is used only for data where loss is acceptable, with the business’s approval.
  • FORCED mode isn’t used on critical databases.
A database option that lets a commit return successfully before its log record is written to disk. It reduces log write waits, but the most recent transactions can be lost if the server shuts down unexpectedly.
Yes. If the server shuts down unexpectedly, commits still in the log buffer and not yet written to disk can be lost.
For reproducible staging and ETL data, and for log and telemetry records where loss is tolerable. Not for financial or critical business data.
First lower log disk latency and group small transactions. Consider delayed durability only after that, and only for suitable data.

SQL Server Performance Consulting

Gain speed without giving up data safety

Aryasoft’s senior DBA team finds the source of the log write bottleneck at disk, transaction design and configuration level, applies the safe improvements first, and introduces delayed durability only where data allows it, in a controlled way.

4.7/5