I have a Windows Service which executes large number of tasks in parallel using the Task Parallel Library (TPL). This is about to be extended to handle tasks which interact with an SQL Server on an external server.
TPL is supposed to be good at measuring load and assigning the right number of parallel threads to the tasks. Is there a way to make it aware of load to the external SQL Server instance? The actual code to run for each task on the local server is quite small, but the calls to the database can be quite heavy.
Am I not likely to end up with my service bogging down the database with request because TPL sees that the local server has loads of free resources, or is there a known way to handle this?
There’s is nothing native to TPL that will help you with this. TPL is about managing/maximizing the CPU load of your local application. It has no idea about SQL load, let alone on another machine.
That said, if you wanted to get crazy, there is an extensibility point called the
TaskScheduler. You could theoretically implement a customTaskSchedulerthat can watch the load on the SQL server and only schedule tasks to execute if that load is at some defined threshold.Honestly though, I don’t think it’s the right solution to the problem. Managing load against a shared resource like a SQL server is a completely different beast than what TPL is designed to solve. You’d be much better off just making sure you design your application such that it doesn’t abuse the SQL server in its own right by load testing, finding a sweet spot and configuring your application not go out outside those bounds. From there it would be up to your DBA to determine the right solution for the SQL server infrastructure itself to manage that application’s needs along with any other external load.