The promotion strategy of the main process (7): Talking about the server model

In the previous article " Main Process Promotion Strategy (6): CGI and FastCGI ", it was mentioned that the Web server and CGI/FastCGI can dynamically output content, thereby providing more powerful business processing capabilities. The architecture of the web server, which I call the Web mode, is the opposite of the Svr mode. Web mode and Svr mode are the two most common modes in the background of Internet projects. A few concepts are introduced first.

 

Synchronous Communication vs Asynchronous Communication

Synchronous communication means that in a connection, the next request cannot be sent before the response to a request comes back. The entire communication process is request 1 - response 1 - request 2 - response 2... This kind of. Asynchronous communication is the opposite of synchronous communication. Within a connection, requests can be sent at will, and the order in which the replies are received may not be the same as the order in which the requests were sent.

 

It can be understood from the description that the communication performance of synchronous communication is lower than that of asynchronous, but the advantage is that it is simple and does not need to consider the complexity of out-of-order responses.

 

Synchronous logic vs asynchronous logic

Synchronous logic means that when the code encounters a call that needs to wait (such as querying the database for data), it blocks and waits for the call to complete. Asynchronous logic does not block and continues to execute subsequent code.

 

Our common file IO interfaces read/write and network IO interfaces send/recv are synchronous by default, and special setting APIs need to be executed to become non-blocking. Synchronous logic conforms to the thinking mode of the human brain. Writing asynchronous logic needs to deal with various non-blocking and abnormal situations, which is extremely challenging for intelligence. Even if a finite state machine is used, it is also a very challenging task.

 

Stateless vs Stateful

Each time CGI/FastCGI is executed, it will obtain data from the data layer (db or data cache), and then write it back to the data layer after modification, which means that CGI/FastCGI will not cache data. This is stateless.

 

In a stateless architecture, it makes no difference whether the request is processed by this web server or that, because the data is obtained from elsewhere. The expansion of this architecture is very convenient, but it should be noted that when a request is multi-concurrent at the same time, it is necessary to prevent the data inconsistency vulnerability that may occur, that is, to prevent concurrency. In the future, I will write a separate article on anti-concurrency.

 

Stateful is a concept opposite to stateless, which means that data is cached in the server. In this architecture, because there is no need to repeatedly fetch data from the data layer, the performance will be much higher, but because the server caches the data, in order to maintain data consistency, the data requests can only be distributed to this server for processing. For games, the user data of each area is independent, and the real-time requirements for interaction are high, so it is appropriate to adopt a stateful architecture.

 

Web Mode vs Svr Mode

Most of the earliest web games are clients made of html+js. If the chat function is to be implemented, the browser needs to periodically request the server to obtain chat information. The web server cannot actively push messages to the client. I summarize the characteristics of the background of the web mode:

1. Communication is a request-response type, that is, the client requests first before the server responds, and the server cannot actively push messages to the client;

2. It is synchronous communication. In a connection, the next request can only be sent after receiving a response;

3. It is synchronous logic, and the Web mode rarely uses asynchronous logic;

4. It is a stateless architecture. CGI/FastCGI obtains data from the data layer every time, and then writes it back to the data layer after modification.

 

The Svr mode is the opposite. A long connection is used between the client and the server. The client's request may not be answered. The server can also actively push messages to the client. The communication is not limited to synchronous, and the client can continuously The request is sent, and the server's response may even be out of order with the request.

 

Compared with the Web mode, the Svr mode has stronger communication performance, because it adopts a long connection and asynchronous communication, and can actively push messages, which is an advantage. However, because of the use of long connections and asynchronous communication, the requirements for client development are higher, and it is necessary to handle disconnection and reconnection and support response disorder.

 

Because of the simplicity of the Web mode, the Web server itself implements general operations such as HTTP protocol processing and FastCGI process management. External programs such as FastCGI only need to process business logic, which reduces a lot of barriers. And because it is stateless, it is very convenient to expand capacity, and it can be done by adding processes and machines directly. The advantage of this smooth capacity expansion is very important in the Web era - the time cost of performance optimization and architecture optimization is relatively large and uncontrollable. , If you can add hardware to quickly resist, then it is an excellent architecture.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326447415&siteId=291194637