How Service Broker Uses Tempdb
Service Broker manages message processing through dialogs, which store conversation state data. While this data is primarily kept in memory, SQL Server pages the conversation state to tempdb when:
Why Does Service Broker Store Data in Tempdb?
The conversation data is cached in memory, but when the memory footprint grows beyond a manageable size, Service Broker offloads this data to tempdb. Unlike other SQL Server components that can expire data from cache, Service Broker must persist conversation states until explicitly ended.
Connection Between sysdercv, sysdesend, and Tempdb
When Service Broker pages data to tempdb, it primarily interacts with two internal tables: sysdercv (received messages) and sysdesend (sent messages). These tables store message data that must persist until the associated conversation is explicitly ended. Since Service Broker does not differentiate between an inactive but still valid conversation and an orphaned one, all conversation data is retained indefinitely in tempdb unless explicitly removed.
The primary issue arises when:
Since these tables are not indexed for efficient cleanup at scale, querying them for stale conversations becomes increasingly slow, making manual cleanup extremely difficult.
Identifying the Problem
In this case, the customer's tempdb was consuming excessive disk space despite routine maintenance. Investigation revealed that the internal tables sysdercv (received messages) and sysdesend (sent messages) were not clearing, leading to massive accumulation of conversation state data.
Further analysis showed:
The root cause was an application bug where conversations were not explicitly closed, leading to the unchecked accumulation of state data in tempdb.
Attempts to Resolve the Issue
Manual Cleanup Attempts
I attempted several cleanup strategies recommended in SQL Server Books Online. They all failed because of the sheer amount of data caused by the lack of proper indexes.
Resetting Service Broker
Another approach was to reset Service Broker. This caused Service Broker to enter a limbo state—assigned a new SID but unable to start correctly. The issue remained unresolved.
Final Solution: Migrating to a New Instance
After multiple failed attempts, the only effective solution was to create a new SQL Server instance and manually migrate only user objects and data, excluding all system-related data. The objective was to ensure a clean installation that did not carry over any lingering Service Broker state from the previous instance.
Migration Steps:
This approach successfully eliminated the uncontrolled tempdb growth, allowing Service Broker to function correctly.
Conclusion
Service Broker provides powerful messaging capabilities but requires careful management to prevent excessive tempdb growth. By proactively monitoring and managing conversation states, SQL Server professionals can avoid performance bottlenecks and ensure long-term system stability.
In this case, after exhausting several cleanup and reset options, migrating to a clean instance was the only viable solution. Adhering to best practices will help prevent such scenarios in the future, keeping Service Broker deployments efficient and manageable.
I hope this was helpful! đź’š