503 Server Error and concurrent connections

Discussion in 'Site Programming, Development and Design' started by Steve Haunts, May 25, 2011.

  1. Hi

    I have had a problem with my website today which is hosted with Winhost. The site is http://www.hauntedhouserecords.co.uk

    The site is asp.net mvc 2 and connects to a sql server database via an SqlConnection using stored procedures.

    The site started giving a 503 un-available error. It has been running run for months without problem. The support agent made the following suggestion.

    "Please understand that your site is not releasing the concurrent connections which is not a normal function of ASP.Net websites. I can only ask to you that you optimize your code to ensure that you are releasing the http connections properly. "​

    They have increased the connection limit, but I am not sure what he means by making sure i release concurrent connections. Is he referring to the database connections? If so I release the connection and reader after each call to the database.

    Could this be a symptom of rate of traffic. My traffic isn't excessive. Over the past week I would say I have had on average 500-600 page views a day.

    Any advice would be gratefully appreciated.

    Last edited by a moderator: Oct 14, 2015
  2. Post some code and let us have a look. If you are hitting the connection limit, well, then your hitting the connection limit. The connections are not closing or they take too long to close. The additional traffic just compounds the problem.
  3. Hi Chuck,

    Here is a typical database call from my site. I call close on the connection and reader. do i need to call dispose explicitly also? I have also checked all the images on my site to make sure they are being called from relative paths and not by specifying http://... each time.



    public List<BannerAd> GetBannerAds() {
    List<BannerAd> banners = new List<BannerAd>();

    try {
    conn = new SqlConnection(connectionString);

    SqlCommand cmd = SetupSproc("GET_ALL_BANNER_ADS");

    // execute the command
    reader = cmd.ExecuteReader();

    while (reader.Read()) {
    banners.Add(new BannerAd(GetString("BANNER_NAME"), GetString("IMAGE_URL"), GetString("DESTINATION_URL"), GetBool("IS_LIVE"), GetInt("ID")));
    } finally {
    if (reader != null)
    if (conn != null)

    return banners;
  4. How often are you calling this? How many pages, times per user visit, etc?

    Also, Im going to assume such ads are fairly static? If so, why not cache the data in your model for a while?
  5. Wait a second, sorry, Im asleep at the wheel. I just re-read your first post again. This is about service unavailable. Its your HTTP connections that are the issue. Have you gone through your raw logs to see what kind of traffic you are getting? Pay close attention to the timeframe the service unavailable appeared. You should be able to see some pattern with regards to what assets are being pulled.
  6. the calls would happen for each page. each page will get the banner ads, the product list on the right hand side and the text content from the database.

    i guess i could look into caching some of this data. What would be the best way of invalidating that cache though. so if i cache the product list for example. if i add another product from my admin panel, i would want that to update for any user still on the site.

    cheers for your reply btw :)
  7. no i havn't looked any any raw logs. how do i get hold of them?
  8. rum

    rum Winhost Staff

    Last edited by a moderator: Oct 14, 2015
  9. I will look at caching today. I will first cache the data from the database. The cache object on the session allows putting an expiration time. Most of my content doesn't change too often so is a prime caching candidate.

    I'll also look at the output caching article you sent over.

    By reducing the db calls with the caching will that reduce the 503 unavailable error from happening and hitting the connection limits?

    I hope so becuase apart from the database acces i don't do anything out of the ordinary. I do not connect to any web services and all my images and assets are referenced with relative paths from the root and not done as http calls.
  10. Chuck, I have done what you have suggested. Any parts of pages that remain fairly static but is driven from the database has been put into a user control and tagged with the OutputCache markers so they get cached (set to 100 seconds at the moment until i think of a more suitable cache time).

    This actually means the majority of the content from the site is cached as it is mainly product info that remains fairly static so this should help reduce connections, fingers crossed.

    I have also implemented caching on all the database calls so there will definitely not be any db calls that aren't required. Certainly not lots of calls per page load.

    These changes should go live within the next 3 hours as we need to do soem more testing on it.

    Once these are live, is there anything else I should look at that might effect this connection limit problem that I may have over looked? This is my first mvc site so I could have missed something else?

    Any other Winhost customers had to fix any issues around connection limits and 503 errors?

    Last edited by a moderator: Oct 14, 2015
  11. Caching anything you can is always a good idea. Outside of the efficiency, it also increases performance of the application from the end users perspective.

    As for your question about connections. These are two different things. HTTP connections are HTTP connections. Thats what your hitting, not database connections.

    If you remember I mistakenly started talking about database connections, however its an area you needed to address anyway. (Will discuss this shortly)

    The more assets you have on a page, the more connections. Every time there is an asset that the browser issues a GET request for is an HTTP connection. Keep in mind, this includes AJAX/Jquery stuff too!

    If you have a page with 10 assets, thats 10 connections (Its actually more complicated than that, but for the purposes of this conversation it will suffice) Now if you multiply the 10 connections by 10 users, thats 100 connections.

    Now the same goes for database connections. If you need 3 database connections per page, times 10 users on the page at the same time, thats 30 database connections.

    The 503/server unavailable error, can be caused by many things. This is a generic error. However, 3 very common reasons, almost always go hand in hand. The number of HTTP connections, memory usage, and CPU usage.

    The issue with http connections is that they happen in milliseconds. Odds of two hitting at the same exact time are very low. So unless your site gets A LOT of traffic, this is not something you will see regularly. However the memory consumption will have a more lasting and tangible effect.

    The database connections, the dynamically generated content from the result of the database query, the number of users making HTTP connections, etc, etc, etc, will all impact your memory consumption. You hit your connection limit. But you also exceeded your memory allotment.

    Winhost Basic 100MB memory
    Winhost Max & Ultimate 200MB memory

    The changes you are making, will improve this issue.
  12. Ok That's great thanks.

    The first wave of changes went live yesterday (uk time). I have put the caching on the db calls using the cache in the httpcontext. The average visitor spends up to 5 mins on the site so I have set the cache time-out to 5 mins as my content is basically brochure-ware so doesn't change that often.

    I have split all my page areas into user controls too and these are cached with a refresh setting at 100 seconds, so hopefully these changes will stop that problem from happening again.

    I haven't optimised the pages in my admin area yet as it is only me that logs into that, but I will probs do some optimising on that today for the sake of completeness.

    I guess this could be an easy situation for someone to fall into if their site gains in popularity so hopefully this thread will be useful to other people. Have you had many other people get this 503 problem?


  13. Thats great news!

    Most customers dont hit the thresholds I mentioned above so its not a common issue. Application footprint/resource usage and traffic are the key components that cause this.

    But as you have seen, the issue isnt something thats resolved with the flick of a switch. So I agree with you, this thread would be a great read for someone who does get the error.

Share This Page