locked
Static Methods impact on scalability of servers RRS feed

  • Question

  • User-36054039 posted

    Hi Everyone, 

    i got a static class in my web application which contains the the utility/static methods for encryption & decryption. 

    These methods are going to get called at least thrice on each web request that is received.

    As only one instance of Static class is created for complete AppDomain, are the concurrent web requests are going to wait in queue, for their turn to invoke these static methods until current request is done with executing these methods..? hence resulting in scalability issues...?

    instead do i need to convert these static methods into instance methods to overcome these scalability issues..?

    Can you please suggest the best practice over here..?

    Thank you..

    Tuesday, August 14, 2018 2:24 PM

All replies

  • User475983607 posted

    kathipavanaspnet

    As only one instance of Static class is created for complete AppDomain, are the concurrent web requests are going to wait in queue, for their turn to invoke these static methods until current request is done with executing these methods..?

    In general, static methods are thread safe and use the stack to manage state just like an instance method.  Static variables are what you have to be careful with as those are a single memory location shared by the entire application.

    Tuesday, August 14, 2018 2:41 PM
  • User-36054039 posted

    Thank you for your response, I am not using any static variables in my static methods, so i am good over there.

    Can you please explain on, what will happen if my static methods are going to perform time consuming task for ex , lets say it is making synchronous web service call..?

    In that case does it (Static method Invocation on current web request) going to make other concurrent web requests  to wait in queue, for their turn to invoke this static method until current request is done with executing this method..?

    Thank you..

    Thursday, August 16, 2018 12:26 AM
  • User-893317190 posted

    Hi  kathipavanaspnet,

    If your method is not asynchronous, it will run synchronously which means if the thread meets a time consuming task  such as retrieving data from a database, it will wait until the data returns.

    However, c# web application is multithreaded , which means when a thread is waiting, it will not cause other threads  to wait. So other threads will not wait when one thread is waiting. They are divided in separate threads. If your method is run by separate threads , it will not cause waiting.

    But in the thread itself, it does stop. If you don’t want to wait even in a single thread , you could consider asynchronous method in c#.

    For more information about asynchronous method, please refer to

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

    Best regards ,

    Ackerly Xu

    Thursday, August 16, 2018 7:16 AM
  • User475983607 posted

    Can you please explain on, what will happen if my static methods are going to perform time consuming task for ex , lets say it is making synchronous web service call..?

    In that case does it (Static method Invocation on current web request) going to make other concurrent web requests  to wait in queue, for their turn to invoke this static method until current request is done with executing this method..?

    You are asking questions about static methods and async operations, but you are really asking about computer architecture - hardware.

    A static method compiles to instructions that a CPU executes. While the .NET language uses the static keyword to organize the .NET code, a method, static or not, is a block of instructions executed by a CPU.  A multi-core CPU might execute instructions from the same static method concurrently. 

    Your second question is about synchronous and asynchronous operations.  Computer hardware/drivers are asynchronous and the hardware/drivers have onboard processors of their own.   

    Making a synchronous web request is like making an online purchase then waiting at the mailbox for the purchase to arrive.  The same goes for making a synchronous web request except your making a thread wait.  Requests will stack up and wait for an available thread if the application is busy enough to exhaust the thread pool.  An asynchronous request lets the thread go back into the pool to be reused by another process.  The network card handling the request interrupts the system when the response arrives.  At which time the OS assigns another thread to complete the operation.

    Thursday, August 16, 2018 10:45 AM