Parallelism and Concurrency in Web Servers (Part 2)

This is a continuation of Part 1.

Workload of Web Servers

A typical web server has a complicated request processing mechanism.  But I'm going to oversimplify it, so that we can focus on the areas of interest for us.

At a high-level, I've split the process into 3 steps,

  1. Read Request - This is where the web server reads the query string, the headers, the data etc. from the incoming request.  Let’s assume that this takes 0.5 seconds.
  2. Query Database - This is the step where our web server needs to pull in some data from some other database server.  Let’s assume that this takes 1 second.
  3. Construct Response - This is the step where our web server creates the final response that’ll go out to the client (typically a browser).

The point to note is that the querying step is not really work done by the web server.  



Similar to our lay-man’s example of a typical day at work, the thread is waiting for a response from an external system.  But instead of utilizing that time for some other task, the thread is idle, waiting for the response.  Now if we take another look at graph 1, we see a lot of white spaces.


These white spaces represent the duration where the thread is idle.  Now, this doesn’t look very efficient, does it?  Let’s look at the concurrency graph now.


There are no white spaces.  Those spaces are occupied by other tasks.  I think you’ll start to get an idea now.  Still, let’s take a closer look.  


The thread receives request 1 from the browser and it starts to process it.  But immediately afterwards, it receives request 2.  After .5 second mark, the thread sends the query to the database and picks up task 2 instead of waiting for the response.  Near 1 second mark, thread sends another query to the database and picks up task 3.  Near 1.5 second mark, the thread receives the response from the database server and creates the response.  This continues to happen and 4 requests are processed in just 4 seconds.

This is essentially how concurrency can help improve the efficiency of a web server.

Comments

Post a Comment