Skip to content

Possible Azure SQL #14

Open
Open

Description

@adamfur

Hi,

We have built a system that is using the PollingClient to feed around 25 commit observing projection builders.
Occupationally in production we have simultaneously received crashes in several of our projection builders at the same time, at the very same commit checkpoint.
When we reset the checkpoint, and feed the projection builders with the entire event stream again, it passes perfectly.
So it appears it sometimes skips a few events!
It only seem to happen during highly intense read+write periods during imports of data.

SQL Azure's default transaction level is different from vanilla MSSQL.

If I write event+0 and event+1 to the database, is it possible that event+1 is available before event+0 in SQL Azure?

In MSSQL we haven't been able to reproduce this result.

Any clues?

Activity

  1. andreabalducci commented on Aug 27, 2015

    @andreabalducci
    Member

    Haven't worker yet with SqlAzure but is possibile to have on a distributed system this kind of issue. You should handle this "glitch" in your client:
    A) polling client -> sequencer -> projection
    B) modify the polling client to read with few milliseconds delay

    I will go for A

  2. adamfur commented on Aug 27, 2015

    @adamfur
    Author

    I've messed around a bit, received transaction exceptions in the polling client while using the EnlistInAmbientTransaction() call during the Wireup(). Not sure if it actually solves anything, but gonna try it out for a few days.

    Regarding opt A and B.
    I think creating a sequencer is difficult, we except a lot of holes in CheckpointNumber identity column, as we are using several buckets, also SqlAzure sometimes "randomly" bumps the identity by +10'000.

    Implemented a version of B were we changed ObserveFrom*() to pass UtcNow - 300ms, and ignore all commits newer than that. Giving some time for the infrastructure to catch up.

    If we settle for opt B, I will eventually send a pull request with a SqlAzureDialect.

  3. adamfur commented on Sep 11, 2015

    @adamfur
    Author

    Our issues:

    • Azure SQL sometimes bump up the identity (CheckpointNumber) value by 10'000.
    • Fetching a range of recently written commits, is sometimes missing a portion of the data.
    • There are unexplained gaps in CheckpointNumber, usually ranging from 1-2.

    The workaround:

    • Added a predicate so we can filter on the bucketId, whatever we should use the OnNext()-method on the current commit.
    • Always fetches from all-the-buckets (Only way to know if we have a gap).
    • Always consume commits older than 5 seconds.
    • Throw if the next commit is not the last checkpointnumber + 1.

    Notes:

    1. If case we stumble upon a gap, the clients will have to tolerate a lag of 5 seconds before their projections are updated (happens like two times a day).
    2. Invalid sequence retrieval is treated like a transient error, will retry until the next commit has aged to 5 seconds or more, or if we receive the correct sequence.
    3. In our logs we can see that it has taken almost 0.5s before we ultimately receive our expected sequence number.
  4. fschmied commented on Mar 31, 2016

    @fschmied

    I believe this to be caused by READ COMMITTED SNAPSHOT, which seems to be on in Azure SQL by default (https://blogs.msdn.microsoft.com/sqlcat/2013/12/26/be-aware-of-the-difference-in-isolation-levels-if-porting-an-application-from-windows-azure-sql-db-to-sql-server-in-windows-azure-virtual-machine/) and is incompatible with NES.

    I wonder if creating an AzureSqlDialect using READCOMMITTEDLOCK would have resolved the issue (if that works on Azure SQL).

  5. fschmied commented on Apr 15, 2018

    @fschmied

    I just did a bit of experimentation that showed adding the WITH (READCOMMITTEDLOCK) table hint to NEventStore's queries would probably solve the problem observed by @adamfur as it reintroduces the blocking behavior of the polling client normally seen under SQL Server, but lost under Azure SQL.

  6. fschmied commented on Aug 9, 2019

    @fschmied

    We've created a subclass of MsSqlDialect that adds the READCOMITTEDLOCK table hint for Azure SQL, and it seems to fix the main problem.

    What remains is the very low likelihood of #21 occurring, but I think noone has actually ever seen this in production.

  7. AGiorgetti commented on Dec 3, 2026

    @AGiorgetti
    Member

    Azure SQL diagnosis and follow-up

    The report here fits row-versioned READ COMMITTED: writer A can hold an earlier inserted checkpoint open, writer B commits a higher one, and the poll sees only the higher commit. Restarting a projection from an earlier checkpoint after both commit can find both, which explains how a rebuild can succeed without fixing the original poll.

    SQL #31 / PR #39 proposes READCOMMITTEDLOCK for SQL Server and Azure SQL. It can wait on an uncommitted row the query actually encounters. It is not a guarantee: if the lower checkpoint is allocated but its row or conflicting lock is not yet in the reader's path, the reader can still advance. See SQL #21 and the report of misses with the hint. A 300 ms or 5 s delay may reduce the odds, but cannot distinguish a delayed insert from an intentional checkpoint gap.

    The durable fix is tracked in SQL #72 under core #425; the proof needs independent writers/readers under both lock-based and row-versioned READ COMMITTED, including commit, rollback and restart. SQL #63 covers a different way a poll can miss data: shifting offsets after rows are deleted. This Azure SQL issue remains useful as a symptom report, not as evidence that the same hint applies to other databases.

    Co-authored by Alessandro Giorgetti and Codex (OpenAI).

  8. AGiorgetti commented on Dec 3, 2026

    @AGiorgetti
    Member

    SQL Server and Azure SQL: opt-in mitigation for legacy checkpoint polling

    We’re implementing #31 as a targeted mitigation for legacy polls under READ_COMMITTED_SNAPSHOT (RCSI). If transaction A has inserted checkpoint 101 but hasn’t committed, and transaction B commits 102, an ordinary versioned read can return 102 without waiting. A projection that saves 102 will never ask for 101 again. With READCOMMITTEDLOCK, a poll that encounters A’s conflicting lock waits for A to finish. If A commits, the poll can read 101 before advancing; if A rolls back, the poll can move past that gap. This is the Azure SQL scenario described in #14.

    Implementation. Reuse the default-off dialect opt-in from PR #39, but not its broad query rewrite. When enabled on the SQL Server dialect, apply the hint only to the two queries behind GetFrom(checkpoint) and GetFrom(bucketId, checkpoint), shared by the sync and async legacy pollers. Leave bounded GetFromTo reads, stream loading, date-range reads, writes, and other dialects unchanged. The provider cannot tell a legacy poller from another caller of the same GetFrom API, so those callers receive the hint too when they opt in. No schema or database-wide isolation change is required. Polls can block longer or hit command timeouts, which is why the default remains off.

    Verification. In an isolated RCSI database, hold the lower insert open on one connection and commit a higher row independently. Verify that the default read can see the higher row while the lower one is still uncommitted. With the opt-in enabled, verify that the poll actually waits on the lower transaction and resumes correctly after commit or rollback. Cover sync and async, global and bucket-scoped polls, and assert that the other query shapes stay unchanged.

    What this does not fix. The hint cannot wait on a conflicting row lock that does not exist yet. If 101 has been allocated but the reader can already pass its position, it can still save 102 and miss 101 later. That publication-order race is tracked in #21; #72 remains the SQL Server implementation of the stronger core #425 contract. #63 remains separate: it addresses offset paging, not publication order. This change is intended to resolve #31, mitigate the RCSI symptom in #14 and #21, and leave the broader issues open.

    Co-authored by Alessandro Giorgetti and Codex (OpenAI).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions