Reputation: 787
I need to do mass HTTP(for a financial app, host doesnt offer better API) Requests(around 800 per second) and process their response(in JSON, usually not more than 1kb large) with low latency(doing only deserialization and comparing some values) to finally make another request based on the response(time between Response and next request shouldnt be more than 1-2 ms).
Currently, I use traditional Threads with synchronous Requests where around 50% of the threads only do requests after 40-60 seconds and the other 50% are always requesting
While this approach worked fine with around 50-100 requests per seconds, I experienced that with 800 requests per second the time between a response and the next request of a thread is way too high(often 50-200ms).
As I want to fix that I'd like to ask:
1. Are Asynchronous Operations the better approach for that?
Read a lot about Scalability and Responsiveness gaining with Asyncs, but not sure if its good for low latency(Context switching, Task creation etc. overhead )
2. Can I tweak the Thread aprroach? (Independent from Quest #1) I was thinking of something like giving threads a higher priority(over .ThreadPriority
) when they currently process the responses, but that didnt really work out/Maybe completely stop execution of the other threads while processing
(3. Which Class/Lib for HTTP Request should I use?) Currently using HttpWebRequest
s, which are a bit faster than the HttpClient
ones in my tests, or should I use something else?
Any help will be greatly appreciated
Upvotes: 3
Views: 1361
Reputation: 99989
In my experience, System.Net.Http.HttpClient
performs well enough for your needs. One challenge with that is setting the number of parallel connections. You can't use the HttpWebRequest.ConnectionLimit
property directly, so you have to go through the ServicePointManager
:
ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.ConnectionLimit = connectionLimit;
You can then use tasks for your parallel operations, and wait on the result with Task.WhenAll
.
Upvotes: 0
Reputation: 12375
I went through and solved this exact problem (for downloading lots of XML files from a server in parallel) and in my experience, using Async was about 20%-50% faster, depending on the size of the file.
Unfortunately this was a few months ago, but I was using a WebClient within each request and just doing a WebClient.DownloadString, so if this is your use case it might work for you.
The real answer is: try both and profile it. It shouldn't be hard to switch between the two!
Upvotes: 3