What does Asynchronous request handling mean
Let's try to answer the below two questions as part of this blog.
1) What is the difference between Asynchronous and Synchronous request handling?
2) What are the advantages of one over the other?
Lets us first understand what happens when we send a request to any website like amazon.com.
Step 1: Your request reaches the server via the network.
Step 2: The server does something in the background and responds to your request with some data.
Step 3: You will receive the response and see it in your browser.
If you observe carefully there is a time delay between steps 1 and 2 as the server needs to do something, the time taken is proportional to the complexity of the activity.
Based on how the server handles our request we can say if the process is asynchronous or synchronous.
Let us take an analogy of how we wait for service at banks. One way is we wait in a queue to withdraw our money OR receive a token and relax while we get a call once our money is ready. Now if guessed that the second way can be called asynchronous, then you are right.
Similarly, every server will have an HTTP thread pool that handles the incoming request. A thread pool is like the number of teller counters at the bank. You can have a finite number of threads, so once you reach the limit, the server cannot accept any new requests.
There can be many reasons for reaching a thread pool limit, such as threads waiting on IO operations or external systems like web services. To continue with our analogy, if you want to access a safe deposit box and bank rules say that at any point in time, only one person can be in that room, then you have a blocking scenario.
Once you enable Async request mode for such blocking scenarios, your HTTP server will create a background process and releases that thread back to the pool. Instead of creating a new background thread for every request we can make it more efficient design by creating counters like Java Semaphore.
Most HTTP Servers by default will create a new thread for every HTTP request and process them synchronously i.e thread will wait for the server to process the response and return it before getting released back to the pool.
Next, I will talk about another design paradigm called reactive programming and how it is related to Async request.
Comments