Overview of socket server architecture

Source: Internet
Author: User

The socket server is mainly used to provide efficient and stable data processing, message forwarding, and other services. It directly determines the performance of foreground applications. Let's first look at the socket server as a whole. The socket server architecture is generally divided into the network layer, business logic layer, Session Layer, and data access layer,

(Figure 1)

(1) Network Layer

The network layer is used to listen for socket connections, create sockets, receive messages, send messages, and close connections. As a socket communication server, the performance of the network layer is very important. Therefore, when designing the network layer, we should make breakthroughs in the following aspects: the maximum number of connections, the maximum number of concurrent connections, and the number of messages processed in seconds. How to break through? Below I will introduce some of the technologies and skills commonly used at the network layer (for specific implementation, I will elaborate on them one by one in the blog ):

1) buffer management

Each socketasynceventargs object (saea) has its corresponding cache space in the memory. If these cache spaces are not managed in the same way, when the number of saea objects increases gradually, the cache space of these saea objects will increase. They are not contiguous in the system memory, resulting in a lot of memory fragments, and these caches cannot be reused. When you create or destroy saea objects, this causes a lot of extra CPU consumption and affects server performance. How can this problem be solved? Use the buffer pool for management!

2) Duplex Communication

Increasing the communication efficiency of socket servers is an eternal topic. There are many ways to improve the communication efficiency. duplex communication is one of them. A saea object can only be used to receive or send data at the same time. Some people think that if a saea object can both send data and receive data at the same time, it will certainly improve the socket communication efficiency. Well, I have a lot of ideas! But can you turn left and turn right at the same time? The answer is no. How can we implement duplex communication? Since a saea object can only do one thing at a time, I customize the duplexsaea object, which encapsulates two saea, one for acceptance and the other for sending, is the problem solved.

3) poolofaccepteventargs

What is poolofaccepteventargs? It is not a thing, it is a container, a container that holds the acceptsaea object. Can you quickly identify two socket servers with excellent performance? It is very easy. You can instantly connect to a server through connections of 60 thousand and to see if all connections will be connected. If they are connected, it indicates that the concurrent connection processing capability of this socket server is good. So how can we improve the concurrent connection capability of the socket server? Answer: poolofaccepteventargs!

4) Message Queue Scheduler

The message queue scheduler can be divided into two types: accept message queue and send message queue. Why message queue? It mainly improves the throughput of the socket server. First, we define a queue, and then write n schedulers to constantly schedule messages from the queue, and accept the queue schedulers to throw messages to the business logic layer for processing, the send queue scheduler is used to call the send message interface at the network layer to send data to a specified port.

5) heartbeat Scan

There is a confusion: the client connects to the socket server, the connection is not closed, but the client is down, so this connection becomes a dingtalk in the socket server, so it will not take root! A dingtalk can still endure. What about thousands of dingtalk users? Then it crashes! How can this problem be solved? Periodically scans each connection. If the connection has no IO response within the timeout period, it is disabled.

6) stick package

When the Server accepts a message packet, what if two packets are received by your server at the same time? Will you treat him as a data packet? If a data packet is broken and it is split into two packets that are received by your server, will you splice them together? These are the adhesive packages. How can this problem be solved? Regular Expression scan!

7) multi-thread programming

The programming of the socket server is multi-thread programming. In the face of multi-thread, how can we synchronize threads and avoid deadlocks? How to handle multi-threaded access to public resources is described in the following blog.

(2) Business logic layer

The Network Layer throws the unwrapped message package to the business logic layer. After receiving the message package, the business logic layer parses the Message Type and transfers it to the corresponding processing process for processing.

The network layer should provide an interface for sending messages for calls by the business logic layer, because the network layer does not actively send messages, and the operation for sending messages is controlled by the business logic layer, therefore, the business logic layer should encapsulate message sending methods for different functions based on specific business applications.

(3) Session Layer

The Session Layer is used to record online user information, which belongs to the business logic layer. Since it belongs to the business logic layer, why should it be independent? This is mainly used for distributed development and expansion in the future. Imagine how many people can be simultaneously online on a server? How many people are there in China? If 0.1 billion people are online at the same time, can one server support it? The answer must be no, so distributed development is required. Distributed development involves the synchronization of user information, so the Session Layer should be independent.

(4) data access layer

Is database execution efficiency the bottleneck of the entire socket server? Why? For example, assume that the number of messages processed by our socket server in seconds is 3000, and the historical records are saved for each message to be processed. If the data access layer does not want to hold the network layer back, then, the efficiency of SQL statement execution must reach 3000 per second! If the socket server and the database server are deployed on the same network segment, this speed is okay, but what if the database server is deployed on the Internet? Can the execution efficiency of your SQL statement be so high? Very difficult!

Another question: if the network-layer execution thread is the same as the database execution thread, the network-layer processing must wait until the database execution is complete! If the database execution efficiency is slow, it will be a devastating blow to the entire socket server.

How can we separate the data access layer from the network layer so that they do not affect each other? How can we improve database execution efficiency and balance the processing speed at the network layer with the processing speed at the data access layer? Answer: Connection Pool + SQL scheduler + Master/Slave database.

The overall architecture of the socket server is introduced here. I will introduce the implementation of various technologies.

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.