Web servers are designed to handle a large number of requests at once. When a new request is received, the server looks for an existing response from a previous request in its cache. If the response is not found, the server creates a new response and sends it to the client. If the client cannot find the original response, it may send a request for the information needed to create a new one.


When learning about web servers and how they work, you may be curious if they are constantly listening for requests or if they wait until they receive a request to go into action. With that in mind, today’s SuperUser Q&A post has the answers to satisfy a reader’s curiosity.

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

Screenshot courtesy of xmodulo/Linux Screenshots (Flickr).

The Question

SuperUser reader user2202911 wants to know how web servers listen for new requests:

How does a web server listen for new requests?

The Answer

SuperUser contributor Greg Bowser has the answer for us:

Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

The server creates a listening socket and then blocks while waiting for new connections. During this time, the kernel puts the process into an interruptible sleep state and runs other processes. This is an important point; having the process poll continuously would waste CPU resources. The kernel is able to use the system resources more efficiently by blocking the process until there is work for it to do. When new data arrives on the network, the network card issues an interrupt. Seeing that there is an interrupt from the network card, the kernel, via the network card driver, reads the new data from the network card and stores it in memory. (This must be done quickly and is generally handled inside the interrupt handler. ) The kernel processes the newly arrived data and associates it with a socket. A process that is blocking on that socket will be marked runnable, meaning that it is now eligible to run. It does not necessarily run immediately (the kernel may decide to run other processes still). At its leisure, the kernel will wake up the blocked web server process. (Since it is now runnable. ) The web server process continues executing as if no time has passed. Its blocking system call returns and it processes any new data. Then go to step 1.