Here is the scenario:
There are clients sending requests to a server (it will be sockets or wcf server, that is not important).
Server will keep an open duplex channel and will use it to send an answer (serialized data) to a client. Server will process requests and involves query generation (basing on parameters from a request) and execution against data sources of various types (sql server, file system, analysis services server – olaps, offline cubes and so on…). So heavy IO-bound tasks – definitely often long running.
Performance is important, consider hundreds or maybe thousands of requests at the same time. It must be scalable.
I have never used TPL nor written a asynchronous server. But I’ve read a lot for a few days and… still can’t wrap my head around it.
- Is TPL (4.0, not 4.5) a good choice here?
- Should I create tpl Task for every request that comes to a server? (for async processing)
- Should I create those Tasks with LongRunning option? (so no ThreadPool involved)
- Should I implement any queue mechanism for requests? How?
- Should I chain all parts of a request processing (a. query generation b. query execution against data source) with separate tasks (continuations) or is it ok to use single a task for both a. and b.?
- Should I use .FromAsync task generation for query executions? Or standard .StartNew is enough?
- What are other important areas I should watch for, given those above requirements?
There has been a lot of discussions on the topic… See https://stackoverflow.com/a/908766/1876226 for example. There Chris Mullins’ posts are mentioned, that are lost. I had a link to some web-archive that has a cached version of one the post, will look for it.
UPDATE:
Found a note of Chris Mullins’s post:
see archived copy here: http://nleghari.wetpaint.com/page/Windows+Sockets+and+Threading%3A+How+well+does+it+scale%3F
I am still wrapping my head around the similar application I’m developing. Points learned so far that come to my mind:
Socketasynchronous operations (Begin*, End*, *Async) use IOCP;hundreds or maybe thousands of requests at the same time, then you may get that number of threads putting your system to it’s knees. Use LongRunning when you need to. Use customTaskSchedulerfor concurrency control;(.NET
MemoryCacheor custom) or batching DB requests that do notrequire a response (to make a bulk insert, for example);
partially became a part of .NET 4.5 Async. So you can have neet TPL
extensions in .NET 4.0. See tour A Tour of
ParallelExtensionsExtras
There are a lot of naive and buggy implementations of c# asynchronous socket server. Read, but recheck.