none
How to use ServicePointManager.UseNagleAlgorithm RRS feed

  • Question

  • Hello,

    I am doing alot of webrequests. Atleast 1 per second. Now I try to find ways to do this more effectively and have come accross:

    ServicePointManager.UseNagleAlgorithm

    As I understand this is put to TRUE by default. I have understood that this algorithm is used to GET data when doing a webrequest and by default always uses the algorithm to put the data in packages.

    Now I am not sure if I have undstood this correct. This algorithm has not an advantage of small webrequests but it does for BIG requests. Is that correct?

    QUESTION 1:

    If that is correct. What counts as a big and small requests. The big request I do contains of a JSON response of 1-2 A4 pages. The small requests I do contains a JSON response of only 5 lines of text.

    Does that count for BIG and small requests or is my BIG request small as well in this sense?

    QUESTION 2:

    Is the below code correct where it is possible to deactivate the algorithm for a SPECIFIC request, in my case the small requests while the big requests DO use the algorithm?

    Is it correct that by putting it to false for google.ca this will remain like this all the time when a webrequest is done for google.ca? Also it is possible to add let us say thousands of URLs in the same manner?

    //You can deactivate the algorithm for a single endpoint as follows:
    var servicePoint = ServicePointManager
                            .FindServicePoint(new Uri("http://www.google.ca"));
    servicePoint.UseNagleAlgorithm = false;
    
    //you can also deactivate the algorithm globally for a process as follows:
    ServicePointManager.UseNagleAlgorithm = false;




    • Edited by Silvers2 Saturday, March 26, 2016 12:13 AM
    Saturday, March 26, 2016 12:00 AM

Answers

  • "I have understood that this algorithm is used to GET data when doing a webrequest and by default always uses the algorithm to put the data in packages."

    The Nagle algorithm is specific to TCP connections and knows nothing about HTTP, GET etc. Of course, since HTTP requests travel over TCP the Nagle algorithm will affect HTTP requests but it is not GET specific.

    "This algorithm has not an advantage of small webrequests but it does for BIG requests. Is that correct?"

    If affects both small and large network sends but it's specifically designed for small sends. The algorithm simply tries to avoid sending TCP packets with very small payload and it does by delaying sends by a small amount of time in the hope that the application will send more data.

    "What counts as a big and small requests."

    Depends on the network. For TCP over Ethernet network the packet size depends on the Ethernet frame size which traditionally is ~1500 bytes but today many gigabit network cards support frames up to ~9000 bytes. Since the algorithm is trying to minimize the number of TCP packets network sends that are less than the packet size may be subject to delays. Thing is, if a send spans multiple packets the last packet may too be subject to delays so this small/large request thing is not quite accurate.

    "The big request I do contains of a JSON response of 1-2 A4 pages. The small requests I do contains a JSON response of only 5 lines of text."

    A4 pages and lines of text are not an unit of measure I can recognize. The only unit of measure I know is bytes :)

    "it is possible to deactivate the algorithm for a SPECIFIC request"

    A service point is specific to a server, not to a connection. I suppose you can disable Nagle, make a request, enable Nagle, make another request etc. Even if that works you won't be able to do multiple requests at the same time with different Nagle settings.

    Best thing to do would be to disable Nagle and see if it improves anything in your scenario. There's a pretty good chance that it does not and then you don't need to bother will all this.

    • Marked as answer by Silvers2 Sunday, March 27, 2016 12:26 AM
    Saturday, March 26, 2016 8:06 AM
    Moderator

All replies

  • "I have understood that this algorithm is used to GET data when doing a webrequest and by default always uses the algorithm to put the data in packages."

    The Nagle algorithm is specific to TCP connections and knows nothing about HTTP, GET etc. Of course, since HTTP requests travel over TCP the Nagle algorithm will affect HTTP requests but it is not GET specific.

    "This algorithm has not an advantage of small webrequests but it does for BIG requests. Is that correct?"

    If affects both small and large network sends but it's specifically designed for small sends. The algorithm simply tries to avoid sending TCP packets with very small payload and it does by delaying sends by a small amount of time in the hope that the application will send more data.

    "What counts as a big and small requests."

    Depends on the network. For TCP over Ethernet network the packet size depends on the Ethernet frame size which traditionally is ~1500 bytes but today many gigabit network cards support frames up to ~9000 bytes. Since the algorithm is trying to minimize the number of TCP packets network sends that are less than the packet size may be subject to delays. Thing is, if a send spans multiple packets the last packet may too be subject to delays so this small/large request thing is not quite accurate.

    "The big request I do contains of a JSON response of 1-2 A4 pages. The small requests I do contains a JSON response of only 5 lines of text."

    A4 pages and lines of text are not an unit of measure I can recognize. The only unit of measure I know is bytes :)

    "it is possible to deactivate the algorithm for a SPECIFIC request"

    A service point is specific to a server, not to a connection. I suppose you can disable Nagle, make a request, enable Nagle, make another request etc. Even if that works you won't be able to do multiple requests at the same time with different Nagle settings.

    Best thing to do would be to disable Nagle and see if it improves anything in your scenario. There's a pretty good chance that it does not and then you don't need to bother will all this.

    • Marked as answer by Silvers2 Sunday, March 27, 2016 12:26 AM
    Saturday, March 26, 2016 8:06 AM
    Moderator
  • >> Best thing to do would be to disable Nagle and see if it improves anything in your scenario. There's a pretty good chance that it does not and then you don't need to bother will all this.

    Yes, I tried to disable the Nagle and I couldn't really see either an improvement or a slower response. So I beleive as you said I will just leave it as it is and it should be fine.

    Thank you for your help!

    Sunday, March 27, 2016 12:26 AM