Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:10:35+00:00 2026-05-27T01:10:35+00:00

In SQL 2008+, the following Queue table and Enqueue, Dequeue operations are intended to

  • 0

In SQL 2008+, the following Queue table and Enqueue, Dequeue operations are intended to allow efficient, job queuing within streams among multiple generators and consumers in arbitrary named queues in an optional partially ordered succession. Trivial support for poison message handling through RetryLater(), FailNow(), Reset() (not shown).

CREATE TABLE [dbo].[Queue](
    [ID] BIGINT IDENTITY(1,1) NOT NULL CONSTRAINT [PK_Queue] PRIMARY KEY,
    [PredecessorID] BIGINT NULL,
    [QueueName] NVARCHAR(255) NOT NULL,
    [DataType] NVARCHAR(255) NOT NULL,
    [Data] NVARCHAR(MAX) NOT NULL,
    [RetriesRemaining] INT CONSTRAINT [CHK_RetryCount] CHECK ([RetriesRemaining] IS NULL OR 0 <= [RetriesRemaining]),
    [IsFailed] AS (CASE WHEN [RetriesRemaining] IS NULL OR 0 < [RetriesRemaining] THEN 0 ELSE 1 END) PERSISTED,
    [QueuedOnUTC] DATETIME NOT NULL DEFAULT (GETUTCDATE()),
    [DelayUntilUTC] DATETIME NULL,
    [LastFailedOnUTC] DATETIME NULL,
    [LastFailure] NVARCHAR(MAX)
)

-- Enqueue
INSERT INTO [dbo].[Queue]
(
    [PredecessorID],
    [QueueName],
    [DataType],
    [Data],
    [RetriesRemaining]
)
VALUES
(
    @PredecessorID,
    @QueueName,
    @DataType,
    @Data,
    @RetriesRemaining
)

-- Dequeue
SELECT
    [Dequeued].[ID],
    [Dequeued].[QueueName],
    [Dequeued].[DataType],
    [Dequeued].[Data]
FROM
    [dbo].[Queue] AS [Dequeued] WITH (ROWLOCK, UPDLOCK, READPAST)
LEFT JOIN
    [dbo].[Queue] AS [Predecessor] ON
    [Dequeued].[PredecessorID] = [Predecessor].[ID]
WHERE
    [Dequeued].[IsFailed] = 0
    AND [Dequeued].[QueueName] = @QueueName
    AND ([Dequeued].[DelayUntilUTC] IS NULL OR [Dequeued].[DelayUntilUTC] < GETUTCDATE())
    AND [Predecessor].[ID] IS NULL

Consumer transactions are held open from before Dequeue() through processing application-side and until DELETE, RetryLater(), or FailNow().

  • Is this design sound?
  • Is this setup deadlock free?
  • What negative impact might IDX_Queue_IsFailed_QueueName_DelayUntilUTC or other indexes have on deadlock-freeness?
  • What other indexes are beneficial?
  • How might table partitioning (by queueName) or other features improve the scalability?
  • As conceived, Generators signal to Consumers through an external mechanism that data has been enqueued to avoid tight polling. Instead, (and without SQL ServiceBroker) is there a way to use SQL Locks to efficiently lock a Consumer for which there are no available rows until a generator has written to the named queue?

    CREATE INDEX [IDX_Queue_IsFailed_QueueName_DelayUntilUTC] ON [dbo].[Queue] ([IsFailed], [QueueName], [DelayUntilUTC])

Also, I guess that OrderBy doesn’t matter, as long as we always get one with no predecessor.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:10:36+00:00Added an answer on May 27, 2026 at 1:10 am

    My 2c on this:

    • first and foremost the dequeue is a read. It must be a write. Use UPDATE WITH OTUPUT.
    • don’t use NULL for DelayUntilUTC, use current UTC at insert time. OR expressions may trigger scan, resulting in deadlocks, besides the perf degradation
    • the [Queue] clustered index must be as follows: (IsFailed, QueueName, DelayUntilUTC).
    • make the primary key non-clustered, or get rid of it altogether, there is not much use of identities in queues.
    • get rid of the Predecessor and the LEFT JOIN. You’ll be cursingly troubleshoot deadlocks until retirement with them.

    I understand that the last item is probably very hard to palate, but correlation locking with readpast is basically impossible to achieve on a relational table.

    I have talked about this subject at length in Using tables as Queues. Queues must be simple in order to scale. Your design is too fancy and is not going to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic recursive table setup in SQL 2008 with the following structure:
I have the following sql 2008 table. ID RefundId RefundStatusId CreatedOn 334 549 3
What can be an efficient way for the following problem in SQL 2008? First
I am using SQL Server 2008 management studio to execute the following SQL statements,
In sql server 2008, I have the following query: select c.title as categorytitle, s.title
I have the following configuration: Visual Studio Team System 2008 SQL Server Developer Edition
I am successfully executing the following query in SQL Server 2008 built into VS2008:
I've got the following SQL: select * from transaction_log where stoptime like '%2008%' How
In Microsoft SQL 2008 Enterprise manager, when you right click a table to open
I have a SQL 2008 table with a field called RecDate of type DateTimeOffset

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.