Fixing the error is usually done by setting the max_queue_readers configuration option to a value higher than the current number of active readers or to 0 for unlimited, then restarting the service. This error typically occurs when a Service Broker queue is configured to wait for messages, but the server runs out of available worker threads to handle the incoming read requests, causing the queue to stall.

What You'll Need

  • SQL Server Management Studio (SSMS): The primary tool for connecting to the database engine and running T-SQL scripts. It is available for Windows and provides a graphical interface for most tasks.
  • Command Prompt (CMD): Accessible via the Start menu, used to restart the SQL Server service using the `net stop` and `net start` commands if SSMS cannot access the service.
  • Database Owner Permissions: You must have `ALTER ANY EXTERNAL EVENT NOTIFICATION` (for event notifications) or `ALTER` permission on the specific database to change configuration settings.
  • Access to Service Broker Configuration: Knowledge of whether you are working with a specific queue, a database-wide setting, or an event notification configuration.

Step-by-Step

  1. Identify the Scope of the Error: The error message usually includes the name of the database, the specific queue, or the event notification causing the issue. Open SSMS and connect to your SQL Server instance. In the Object Explorer, expand the Databases folder and locate the specific database mentioned in the error log or message.
  2. Check the Current Configuration and Queue Status: Right-click on the database and select Properties from the context menu. Navigate to the Advanced page. Look for the max_queue_readers setting. Note the current value. If the error message mentions a specific queue name, verify that queue is currently receiving messages. If the queue is empty, the error might resolve itself once traffic resumes.
  3. Adjust the Max Queue Readers Setting: If the current value is 0 (unlimited) or a low number (e.g., 1 or 2) and you are experiencing high load, you can increase the limit. In the same Properties window, change the max_queue_readers value to a higher integer (e.g., 10, 20, or 50) depending on your expected workload. For most standard Service Broker implementations, setting this to 0 (unlimited) is the safest and simplest fix, as it allows the queue to handle as many readers as the system resources allow.
  4. Apply the Changes (Restart Required): Configuration changes to the max_queue_readers setting are not dynamic; they require a restart of the SQL Server service to take effect. Click OK to close the properties window. You will receive a warning that changes will not take effect until the service is restarted. Open a new window in SSMS and connect to the server. In the Object Explorer, right-click on the server name and select Restart. Confirm the restart when prompted.
  5. Restart the Service via Command Prompt (Alternative): If you cannot restart the service via SSMS (for example, if the service is hung or you lack the necessary permissions), use the Command Prompt. Press the Windows Key, type cmd, right-click Command Prompt, and select Run as administrator. Type net stop MSSQLSERVER (or the specific instance name, e.g., net stop MSSQL$SQLEXPRESS) and press Enter. Wait for the service to stop. Then, type net start MSSQLSERVER and press Enter. Wait for the service to start and check the Status in the Services window to ensure it is running.
  6. Verify the Fix: After the service has fully started, try to run the query or operation that was previously failing. If the error persists, check the SQL Server Error Log. You can view the error log by right-clicking on the server in SSMS, selecting Reports, Standard Reports, and then Error Log. Look for any new errors related to the queue or event notification. If the error is gone, the fix was successful.
  7. Address Event Notification Specifics: If the error is specifically associated with an Event Notification (often seen with Service Broker), you may need to drop and recreate the event notification. Right-click the Event Notification in Object Explorer and select Delete. Then, right-click the database, select New Query, and run the T-SQL script to recreate the event notification. This forces the system to re-initialize the queue readers with the new configuration settings.

Common Mistakes

  • Assuming the Error is a Bug: Developers often assume this is a bug or corruption. In reality, it is a configuration limit. The server is designed to stop reading from a queue when it hits a safety limit to prevent the queue from consuming all available memory or worker threads.
  • Ignoring the Restart Requirement: A common mistake is changing the value in the properties window and immediately running a test query. Because the setting is static until the service restarts, the error will continue to appear until the SQL Server service is actually restarted.
  • Setting a Fixed Number Instead of 0: While setting a specific high number (like 100) can work, it creates a hard ceiling. If traffic spikes suddenly, you will hit that ceiling again. Setting the value to 0 removes the ceiling, allowing the system to scale naturally up to the limit of available hardware resources.
  • Not Checking Queue Depth: Sometimes the error occurs because the queue has grown too large and the system cannot keep up with the backlog. Increasing max_queue_readers helps the server process messages faster, but if the backlog is massive, it is also necessary to investigate why the queue is growing so quickly (e.g., a slow consumer or a bottleneck in the sender).

Tips

  • Monitor Performance: After applying the fix, keep an eye on the sys.dm_exec_requests dynamic management view. Look for sessions with a status of suspended and a wait_type of SQS (Service Broker Queue). This confirms that the queue readers are now active and processing messages.
  • Understanding the 0 Setting: Setting max_queue_readers to 0 is the default behavior for many SQL Server installations. If you are seeing this error despite having it set to 0, it might be overridden by an event notification or a specific database configuration. Always double-check the Properties page to ensure the value is what you expect.
  • Service Broker Best Practices: In high-throughput environments, ensure that your Service Broker conversation lifetimes are set appropriately. If messages are held in the queue indefinitely, the queue will grow, eventually overwhelming the reader capacity even if the limit is high.
  • Use Query Store (If Supported): If your SQL Server version supports the Query Store, you can monitor the performance of your Service Broker operations over time. This can help you identify trends in queue length and reader utilization, allowing you to make data-driven decisions about the max_queue_readers setting.

Troubleshooting

If the error persists after restarting the service, the issue may be deeper than a simple configuration limit. Follow these steps to diagnose the root cause.

Check for Blocking: Use the following query to identify any processes that might be blocking your queue readers. A blocked reader cannot dequeue messages, which can cause the queue to appear stalled.

SELECT
    r.session_id,
    r.status,
    r.blocking_session_id,
    r.wait_type,
    r.wait_time,
    t.text AS query_text
FROM
    sys.dm_exec_requests AS r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE
    r.status = 'blocked';

Verify the Service Broker Is Enabled: Ensure that Service Broker is actually enabled for the database in question. You can check this by running the following query:

SELECT is_broker_enabled
FROM sys.databases
WHERE database_id = DB_ID();

If the result is 0, you will need to enable it by running ALTER DATABASE [YourDatabaseName] SET ENABLE_BROKER;. Note that this can cause conversation endpoints to be reset.

Check for Deadlocks: If your Service Broker involves transactions, deadlocks can prevent messages from being processed. Check the Error Log for deadlock graphs or deadlock victim information. Addressing the logic behind the transactions is the only way to resolve deadlocks permanently.

Review Event Notification Dependencies: If you are using Event Notifications, ensure that the Service Broker service and queue you are targeting actually exist and are configured correctly. Event Notifications are a specific type of Service Broker usage and have their own set of requirements, such as needing a Service Broker enabled database and a valid target service.

Verify Worker Thread Availability: SQL Server uses worker threads to execute queries. If your server is under heavy load and has exhausted its available worker threads, it cannot accept new requests to read from queues. You can check the number of available worker threads by running the following query:

SELECT
    cpu_count,
    scheduler_count,
    max_workers_count
FROM sys.dm_os_sys_info;

Compare this with the number of active sessions. If the server is saturated, you may need to address the overall load on the instance before you can resolve the queue reader error.

Frequently Asked Questions

What does the error 'The valid range for max queue readers is 0 to 32767' mean?

This message appears when you attempt to set the max_queue_readers configuration option to a number that is outside the officially supported integer range for SQL Server. You must use a value between 0 and 32767.

Does changing max_queue_readers improve performance?

Yes, for Service Broker queues that are receiving a high volume of messages, increasing the number of readers can significantly improve throughput by allowing multiple messages to be processed in parallel. However, for queues with low traffic, the default setting is often sufficient.

Can I change max_queue_readers without restarting SQL Server?

No, the max_queue_readers setting is a static configuration option that is applied at service startup. You cannot change it dynamically; a restart of the SQL Server service is strictly required.

What happens if I set max_queue_readers to 0?

Setting it to 0 tells SQL Server to use an unlimited number of readers. The server will allocate as many readers as it needs based on the current workload, up to the limit of available hardware resources. This is generally the recommended setting for most Service Broker implementations.

Is 32767 the maximum number of readers?

Yes, 32767 is the hard limit defined in the SQL Server architecture for the max_queue_readers option. Attempting to set it higher will result in an error.

Why is my queue not processing messages even after the fix?

If messages are still not processing, check for underlying issues such as a slow consumer process, a deadlock between the reader and another transaction, or a misconfiguration in the Service Broker conversation path. You should also verify that the queue is not empty.