Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
SQL

SQL Server: Fix a Sequence Object Resetting After a Failover

- 30.11.26 - ErcanOPAK

🗄️ Fix a Sequence Object Resetting After a Failover

A SEQUENCE object’s current value is tracked in memory and only checkpointed to disk periodically (or on a clean shutdown) rather than being durably persisted on every single NEXT VALUE FOR call, which is a deliberate performance trade-off — an unexpected failover or crash between checkpoints can roll the sequence’s visible value back to its last saved point, handing out numbers that were already issued (and possibly already stored) before the failover happened.

🔎 The Problem

CREATE SEQUENCE dbo.OrderSeq AS INT START WITH 1 INCREMENT BY 1;
-- Sequence reaches 48213 in memory, last checkpoint saved at 48000
-- Unplanned failover occurs
-- After failover: SELECT NEXT VALUE FOR dbo.OrderSeq returns 48001
-- -- values 48001-48213 were already issued before the crash

✅ Fix: Use the Cache Setting Deliberately, Not by Default

  • Set an explicit, small CACHE size (or NO CACHE for sequences where gaps are unacceptable and the extra I/O cost is acceptable) instead of relying on the engine’s default caching behavior.
  • For identifiers that must never repeat even across a failover, don’t use a bare sequence value as the final key — pair it with a uniqueness constraint or a secondary check at the point the value gets persisted.
  • If occasional gaps are fine but duplicates are not, add a unique constraint on the column the sequence feeds, so a post-failover duplicate fails loudly on insert instead of silently overwriting or conflicting with existing data.

🚨 Why This Is Dangerous

  • A duplicate order number handed out after a failover can collide with one already written to disk before the crash, and whether that collision is caught depends entirely on whether a uniqueness constraint exists downstream.

A sequence promises each call gets a higher number than the last — it never promised that number survives an unplanned restart intact.

— Database Engineer

Related posts:

SQL Server: Why a View Gets Slower After Adding a Column to Its Base Table

SQL Server: Why a View Gets Slower After Adding a Column to Its Base Table

SQL Deadlocks Only Happen at Night

SQL Deadlocks Only Happen at Night

How to list all tables referencing a table by Foreign Key in MS SQL

How to list all tables referencing a table by Foreign Key in MS SQL

Views: 2
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

WordPress: Fix Gutenberg Reusing Blocks Across Unrelated Posts
Docker: Fix Compose Health Checks Never Reaching Healthy

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com