🗄️ 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
CACHEsize (orNO CACHEfor 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.



