Building a highly available Web site learning (i)

Source: Internet
Author: User

How a single server handles requests

The simplest form of a Web server is a program that listens for HTTP requests and responds after an HTTP request is received. Of course, what the server did after receiving the request was the focus of our attention. The following section also mentions How node is simply implementing a Web server.

It is common for servers such as Apache,tomcat to resolve Web requests. Now more people will use Nginx to resolve web requests, and there is some truth to this. This paper mainly studies the more mature multi-task processing methods of each server. It is mentioned in detail below.

Apache:

Speaking of Web application platform, basically the first reaction is LAMP this classic platform. where Apache uses the time is also more stable in many servers. For a single request, theApache process is similar to the basic Web Server process, and we look at Apache Multi-task Processing:

(1) we know that the operating system has already implemented the concurrency mechanism, then make full use of the concurrency mechanism of the operating system to improve the server's single-machine efficiency is the goal of the server developers. However, because the different operating system platform and computer hardware differences make the concurrency model does not have a common rule, on many platforms very good concurrency model to the other platform to become poor performance, and the same system platform but the tasks performed differently, the concurrency of the same concurrency model is very different ; therefore, the factors that affect concurrency performance include:

    • Operating system Platform
    • The task of running the server

(2) Apache is developed in C language, so the entrance is a main program, the main program once called the MPM model Ap_mpm_run function, the multi-process concurrency processing state is entered, in order to concurrently process the client's request, Apache produces multiple processes, and each process produces multiple threads .

Aapche provides multiple concurrency models:

  • Prefork: Typically used for Unix operating systems, process-based concurrency, with processes as a basic unit for processing client requests; There are multiple sub-processes ( but no threads ); The general Linux system defaults to the concurrency model
  • Worker: The new mpm, a colleague using threads and processes, is more efficient than the prefork MPM that simply uses the process ( existing process, also wired thread )
  • WinNT : For Windows system platforms, threading as the base unit for processing client requests

(3) MPM 's main task

the main task of MPM is to create processes or threads and manage them, including terminating redundant processes or threads at idle time, and starting more processes and threads at busy times, while another task is to set up a socket (socket) When the request arrives,MPM sends it to the created process and thread, and then to the request processing stage ( Each process or thread will be responsible for only one client's request from beginning to the next.

In fact, there are a lot of things we need to comb slowly. such as the Apache Process Model:Apache inside the multi-process mode (multithreaded mode). is A process pool maintained by Apache itself to allocate processes to process individual requests. The pros and cons of processes and threads are not described here.

Nginx article

Before I say Nginx , I want to talk about a better way to handle requests in Node, in the way of asynchronous I/O . As we program, single-threaded synchronous programming performs tasks in a way that is more consistent with the way programmers think, but because blocking I/O results in less-than-optimal use of hardware resources. And the multithreaded programming model also has the headache because of the deadlock in the programming, the state synchronization and so on.

First, let's look at the blocking and non-blocking two nouns (a simple description, specific knowledge of the operating system can be read):

One of the characteristics of blocking I/O is that the call will not end until after all operations have been completed at the kernel level of the system. The main thing about blocking I/O is the possibility of wasting time in the waiting process. Unable to maximize CPU processing power

The difference between nonblocking I/O and blocking is that non-blocking I/O does not return directly with data, and so on to get the data, it needs to be read again through the file descriptor. After non-blocking I/O returns,theCPU 's time slices can be used to handle other transactions. However , there are some problems with non-blocking I/O . Because the full I/o is not completed, the data returned immediately is not what the business layer expects, but only the state of the current call. In order to obtain the complete data, the application needs to repeatedly invoke I/O operations to confirm completion. The technique of repeating calls to determine whether an operation is complete is called polling.

Polling technology (main):

Select/poll: creates a collection of descriptors for the event of concern, waits for these events to occur, then polls the descriptor collection, checks for events, and processes them .

Epoll: The management of the descriptor list is entrusted to thekernel, and once an event occurs, the kernel notifies the process of the event's list of strokes, avoiding polling the entire descriptor list.

Rtsig: The worker process establishes a rtsig queue to hold a signal for the token event to occur through the system kernel.

The above description collates the complete asynchronous I/O : Event Loop (typical producer / Consumer), observer, request object, execution callback.

Node handles requests in an event-driven manner without having to create additional corresponding threads for each request, eliminating the overhead of creating threads and destroying threads, generating fewer threads, and reducing context switching.

Insert Picture

In fact, Nginx internal implementation and Node have the same wonderful. Let's talk about the Web request processing mechanism of Nginx server:

1. Multi-process approach: The advantage of multi-process mode is that each sub-process is independent of each other, the process of processing client requests is not disturbed, does not affect each other, provides the stability of the service. The disadvantage is also obvious: the operating system generates a child process that requires memory replication and other operations, which incur additional overhead in resources and events. (Apache can be improved with pre-build process mechanisms, and the process does not end when the interaction is complete)

2. Multithreading: Cons: Multiple threads are in the same process and can access the same memory space and interact with each other. It's up to the developer to manage the memory .

3. Async Mode

Synchronization mechanism: When the sender sends the request, it waits to receive the response from the receiving party before sending the next request;

Asynchronous mechanism: After sending a request, the sender continues to send the next request without waiting for the receiver to respond to the request. Form a queue and notify the sender when the receiver is finished processing.

Blocking is invoked as long as the current thread is suspended from the running state until the result is returned, before the call results are returned, and the CPU continues to execute after it has been fetched.

Non-blocking call: The thread is not suspended and immediately returns to the next call.

The Nginx server has a master process, multiple worker processes, and the worker process uses asynchronous nonblocking mode.

Nginx event handling mechanism (io status notification to the worker process, theio call is completed to proactively notify the worker process, event-driven model)

Event-driven Model: Event collector, event sender, event handler (multi- IO Multiplexing) refer to the implementation of polling.

Of course, Nginx has a lot of other features: for example, the reverse proxy cache and load balancing also have a better performance. The next few will be introduced.

As for Tomcat still in the study, the specific processing model will be compared with Apache and so on. I hope we can make a dispatch soon.

Summary: This article mainly learned the server multi-task processing request process. There are also a lot of things that are not clear. Please give us a lot of advice.

Building a highly available Web site learning (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.