Go: Overview of the overall architecture of the socket server

Source: Internet
Author: User

Socket server is mainly used to provide efficient and stable data processing, message forwarding and other services, it directly determines the performance of the foreground application. We first understand the overall socket server, the socket server from the architecture is generally divided into: Network layer, business logic layer, session layer, data access layer,

(Fig. 1)

(a) Network layer

The network layer is primarily used to listen for socket connections, create sockets, accept messages, send messages, and close connections. As a socket communications Server, the performance of the network layer is very important, so when we design the network layer, we should focus on the following aspects to achieve a breakthrough: the maximum number of connections, the maximum number of concurrent, the number of seconds processing messages. How to break it? Here I introduce some of the network layer commonly used by some of the techniques and techniques (concrete implementation, I will be blog post one by one specific):

1) Buffer Management

Each SocketAsyncEventArgs object (hereinafter referred to as SAEA) has its corresponding cache space in memory, and if these cache spaces are not managed the same, the cache space for these Saea objects will grow larger as Saea objects grow. They are not contiguous in system memory, causing a lot of memory fragmentation, and these caches cannot be reused, resulting in a lot of extra CPU consumption when creating and destroying Saea objects, affecting server performance. How to deal with this problem? Manage with buffer pool!

2) Duplex Communication

Socket server Improving communication efficiency is an eternal topic, there are many ways to improve communication efficiency, duplex communication is one of them. A Saea object can only be used to receive data or send data at the same time, and some people think that if a Saea object can both send and receive data at the same time, it will certainly improve the efficiency of the socket communication. Well, there's a lot of ideas! But can you make your head turn left and turn right at the same time? The answer is no, how to achieve duplex communication? Since a Saea object can only do one thing at a time, I customize the Duplexsaea object, encapsulating two Saea in the object, one for acceptance and one for sending, and the problem is not solved.

3) Poolofaccepteventargs

What's a Poolofaccepteventargs? It's not a thing, it's a container, a container that holds Acceptsaea objects. Give you two socket server, you can quickly distinguish two server performance is excellent? Very simple, you instantly to a server to enter 5, 60,000 of the connection, to see if it will all connect, if all connected, that the socket server's ability to handle the concurrent connection is good. How do you improve the concurrent connectivity of the socket server? Answer: poolofaccepteventargs!

4) Message Queuing Scheduler

There are two main types of Message Queuing Scheduler: Accept Message Queuing, send Message queue. Why use Message Queuing? The main is to improve the socket server throughput. First we define a queue of queues, then write n schedulers, continuously dispatch messages from the queue, the Accept queue Scheduler is used to throw messages to the business logic layer processing, the Send Queue Scheduler is used to call the network layer to send the message interface, send data to the specified port.

5) Heartbeat scan

There is a confusion: the client connection to the socket server, the connection is not disconnected, but the client hung up, so that the connection in the socket server is a nail house, the roots do not go! A nail house can endure, thousands of them? Then it collapses! How can we solve this problem? Periodically scans each connection and closes it if the connection does not have an IO response within the timeout period.

6) Sticky Bag

When the server accepts the message packet, what if two packets are received by your server at the same time? Would you think of him as a packet? If a packet is broken and divided into two times by your server, will you stitch them up? These are sticky bags, how to solve? Regular expression Scan!

7) Multithreaded Programming

Socket server programming is multithreaded programming, in the face of multi-threading, how synchronization between threads, how to avoid deadlocks? Multi-threaded access to public resources how to deal with, in the following blog post, I will be specifically elaborated for you.

(ii) Business logic Layer

The network layer throws the unpacked message packets to the business logic layer, and the business logic layer receives the message packet, parses the message type, and then goes to the appropriate processing process.

The network layer should provide an interface for sending messages to be called by the business logic layer, because the network layer does not actively send messages, the operation of sending messages is controlled by the business logic layer, so the business logic layer should encapsulate the methods of sending messages for different functions according to the specific business application.

(iii) Session level

The session layer is primarily used to record online user information, which is subordinate to the business logic layer. Since it belongs to the business logic layer, why should it be independent? This is mainly for the future of distributed development, imagine how many people can be supported by a single server at the same time online? How many people are there in China? If 100 million people are online at the same time, can you support a single server? The answer must be no, so distribute the development. Distributed development involves the problem of user information synchronization, so the session layer will be independent.

(iv) Data access layer

Database execution efficiency is the bottleneck of the entire socket server? Why is it? For example, let's say that our socket server's second-processing message has a number of 3000 bars, each processing a message will save the history, then, if the data access layer does not want to drag the network layer's hind legs, then his execution SQL statement efficiency must reach 3000 per second! If the socket server and database server are deployed on the same network segment, this speed is no problem, but what if the database server is deployed in an external network? Does your SQL statement perform as efficiently as possible? Very difficult!

Consider one more question: if the network layer execution thread and the database execution thread are the same thread, the network layer processing must wait for the database to complete before it can be performed! If the database execution is slow, it will be a devastating blow to the entire socket server.

So how to separate the data access layer from the network layer, so that they do not affect each other? How to improve the efficiency of database execution, so that the processing speed of the network layer and the processing speed of the data access layer to achieve a balance? Answer: Connection pool +sql Scheduler + master-slave database.

The overall architecture of the socket server is introduced here, I will give you a detailed description of the implementation of each technology.

Go: Overview of the overall architecture of the socket server

Related Article

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.