Optimization 10

Hello, I'm Liu Chao. Today I will take you to optimize network traffic between the next service.
Last lecture, I mentioned the micro-services framework, which SpringCloud and Dubbo's most widely used within the industry there has always been a comparison between the two, a lot of technical people which is better both for the frame and debate.
I remember when we set up micro-services sector in the framework, also tangled a long time on technology selection, had also once had a heated discussion. Current SpringCloud hot, with a complete service of micro ecology, has been voted many of my colleagues, but our ultimate choice is Dubbo, this is why?

RPC communication is the core of large service framework

We often discuss the micro-service, first you should know what is the core of micro-services in the end is, so when we do the technical selection, in order to more accurately grasp the needs.
As far as I understand, I think the core of micro-services are telecommunications services and governance. Telecommunications provides a bridge of communication between services, service management provides logistical support services. So, when we do the technical selection, more is to consider these two core requirements.
We know that the split increases the cost of communication services, especially in some panic buying or promotional business scenario, if there is a method between service calls, for example, need to call after snapping up a successful order system, payment system, ticket packages system, such telecommunication systems easily become a bottleneck. Therefore, under the premise of governance needs to meet certain service, the main factors affecting the performance needs is technology selection remote communication.
Currently, many service communication micro service framework is based on RPC communication implemented under the premise of no assembly extension, SpringCloud is RPC communication Feign component based implementation (Http + Json serialization Based), Dubbo based SPI extension many RPC communications framework, including RMI, Dubbo, Hessian other RPC communication framework (default serialization Dubbo + Hessian). Different business scenarios, selection and optimization criteria are different RPC communication.
For example, I mentioned at the beginning of our department in the selection of micro-services framework, chose to Dubbo. At the time of selection criteria that RPC communication can support high concurrent buying class in this business scenario, the request is characterized by instantaneous peak, and request large incoming, outgoing packet parameters small. The Dubbo in Dubbo agreement well supported this request.
The following is based on the Dubbo: simple performance testing version of 2.6.4. Were tested Dubbo + Protobuf serialization and communication performance Http + Json serialized (mainly simulated performance comparison a single TCP connection + Protobuf long sequence and the short connecting Http + Json serialization). In order to verify the performance of both the amount of data under different circumstances that I have prepared a performance pressure measurement of small objects and large objects, we can indirectly understand both the lower level of RPC communications this way.
This test is accumulated before me, based on the test environment is complex, here I'll just give a result, if you are interested, you can leave a message and I discussed.
The above test results can be found: in terms of response time or throughput point of view, a single TCP connection + Protobuf long sequence of RPC communications framework have achieved a very distinct advantage.
In high concurrency scenarios, we chose to back-end services or middleware framework for departments to design services framework, RPC communication is the focus of optimized object.
In fact, the current maturity of RPC communication framework is very large, if your company does not own middleware team, you can do extended based on open source RPC communications framework. Before the formal optimization, we simply wish to review under the RPC.

What is RPC communication

A reference to RPC, do you still think of MVC, SOA concepts it? If you have not experienced the evolution of these architectures, it is easy to confuse these concepts. You can image below to learn about the history of the evolution of these architecture.
Whether it is micro-services, SOA, or the RPC architecture, they are distributed services architecture, we need to communicate with each other between services, we usually call this communication referred to as RPC communication.
RPC (Remote Process Call), i.e., the remote service invocation request via the network communication technology remote computer programs and services. RPC good package framework underlying network communication technologies such sequences, we only need to introduce the interface packet for each service in the project, can be achieved with the service call RPC method call native code in the same. Because of this convenient and transparent remote call, RPC is widely used in current enterprise and Internet projects, it is the core distributed systems.
RMI (Remote Method Invocation) is one of the first to achieve the JDK RPC communications framework, RMI implementation for building distributed Java applications critical systems is very important underlying Java technology, many open-source framework RPC communication is designed RMI-based implementation principle, including a frame Dubbo also access the RMI framework. Then we went to know the implementation of the principle under RMI, see which performance bottlenecks that exist to be optimized.

RMI: JDK own RPC communication framework

RMI currently has matured applied in the EJB and Spring framework, is a pure Java distributed application system core network solutions. RMI implements a virtual machine application calls to the remote method can be the same as calls to local methods, RMI help us a good package in which the content on telecommunications.

The principle of RMI

RMI RMI is a remote proxy object in the core component, in addition to the virtual machine where the object itself, other virtual machines can also call methods of this object. And these virtual machines may not be on the same host, remote applications can communicate with network protocols and services through remote proxy object.
We can map to a detailed understanding of the entire communication process under the RMI:

RMI performance bottleneck in a highly concurrent scenes

Java default serialization
RMI uses serialization is the default Java serialization, I introduce in detail in the 2009 lecture over Java serialization, we know it's not a very good performance, but also other languages ​​framework does not support Java serialization.
TCP short connection
Since RMI is a TCP connection to achieve short, in the case of high concurrency, will bring a large number of requests to create and destroy a large number of connections for this system is undoubtedly a very performance-based consumption.
Blocking network I / O
08 in the lecture, I mentioned the existence of I / O bottlenecks in network traffic, if conventional I / O Socket programming model, it is easy to produce I / O block network communication based on short connection achieved at high concurrency scenarios, performance will be greatly reduced.

A highly concurrent RPC communication path optimization scenario

SpringCloud RPC communication performance bottleneck and RMI communication is very similar. SpringCloud Http communication protocol is based on (a short connector) and Json serialization achieved in a highly concurrent scenarios no advantage. Then, in the transient high concurrency scenarios, how should we do to optimize a RPC communication?
RPC communication includes establishing communication, to achieve packet data transmission protocol and transmission codecs and other operations, then we set off from the optimization of each layer, the progressive realization of the overall performance optimization.

1. Select suitable communication protocol

To achieve network communication between different machines, we need to understand the basic principles of computer network communications systems. During the implementation of the communication network is a data flow exchanged between the two devices is a network transmission protocol and a codec-based data transmission to achieve. Wherein the network transport protocol is TCP, UDP protocol, these two protocols are based on the programming interface Socket for certain scenarios and extended out of the transport protocol. By the following two graphs, we can probably understand that to achieve Socket network communication based on TCP and UDP protocol is kind of a process.
Socket-based communication protocol is TCP connection, and data transmission reliability of data transmission to be achieved by the three-way handshake, and the transmission of data is no boundary in a byte stream mode.
Socket Communications UDP protocol-based client does not need to establish a connection, only need to create a socket to send data packets to the server, so it can not guarantee datagram will reach the server, so the transmission of data, based on the UDP protocol Socket communications achieved unreliability. UDP data transmitted using the datagram mode, each UDP datagram has a length which will be transmitted together with the data to the server.
By contrast, we can draw Optimization: To ensure the reliability of data transmission, we would normally use the TCP protocol. If in the case of a local area network and the reliability of data transmission is not required, we can also consider the use of UDP protocol, after all, the high efficiency of this protocol than TCP protocol.

2. The use of a single long connection

If the protocol is TCP Socket communication based on what we can do to optimize it?
Unlike the communication between the communication server between the client and the service. The client and server Because the number of clients to achieve short connection request based on avoided takes a long time to connect, resulting in waste of system resources.
But the communication between the service consumer side of the connection will not be as much a client, but the consumption side requests to the server, but the number as much as we achieve a long connection-based, it can save a lot of TCP connection establishment and close operation, thereby reducing the system performance overhead, saving time.

3. Optimize Socket Communications

Network communication to establish two machines, we generally use the Java Socket programming a TCP connection. Socket Communications mainly conventional I / O blocked, the thread model memory copy defects and other problems. We can use more mature communications framework, such as Netty. Netty4 of Socket Communications program is optimized in many ways, the specific see below.
Non-blocking I / O: 08 speaking, we mentioned the multiplexer Selector implements a non-blocking I / O communication.
Reactor efficient threading model: Netty using a primary receiver from the Reactor multithreaded model, the server requests the client is connected to a main thread, the main thread for the client's connection request operation, once the connection is established, I will listen / O events, creates a link request after listening to the event.
A request to register the link will be responsible for I / O operations of the I / O worker responsible for the subsequent I / O operations by the I / O worker threads. Using this threading model, it can be resolved in a high load, the case of high concurrency, since a single thread can not monitor the mass NIO client problems and meet a lot of I / O handling.
Serial design: the server after receiving the message, the existence of encoding, decoding, reading and sending a peer link operations. If these actions are based in parallel to achieve, it will undoubtedly cause serious lock contention, leading to decreased performance of the system. To improve the performance, Netty lock without using the serial link operation is complete, Netty Pipeline implement various operations provided the link is not performed during the thread switching operation.
Zero copy: 08 speaking, we mentioned that a data transmission from the memory to the network, there are two copies of operation, first copied from the kernel space to user space, then copied from the kernel space to the network I / O,. And ByteBuffer NIO Direct Buffers may be used to provide mode, a non-open direct physical heap memory, byte buffer does not require a secondary copy, the data may be directly written to the kernel space.
In addition to these optimized some TCP parameters we can also provide for socket programming configuration items, improve network throughput, Netty based ChannelOption can set these parameters.
TCP_NODELAY: TCP_NODELAY option is used to control whether to turn on the Nagle algorithm. Nagle algorithm cache way small data packet consisting of a large packet, so as to avoid a large number of small packets sent to network congestion and improve network transmission efficiency. We can close the algorithm, optimized for delay-sensitive applications scenarios.
SO_RCVBUF and SO_SNDBUF: to adjust the size of the socket send and receive buffers according to the scene.
SO_BACKLOG: backlog parameter specifies the client connection request buffer queue size. Processing server client connection requests are processed sequentially, so that at the same time can handle a client connection, when a plurality of clients coming in, the server will not handle the client connection request is queued waiting to be processed.
SO_KEEPALIVE: When this option is set, checks the connection status of the connection of the client data is not transmitted for a long time after detecting the client disconnects, the server will recover the connection. We can set the time shorter, to improve the efficiency of recovery of the connection.

4. The packet format tailored

The next step is to achieve packet, we need to design a set of packets, is used to describe specific contents of the checksum operation, the transmission data. In order to improve the efficiency of transmission, we can according to their own business and to consider the architecture design, try to realize reported are small, to meet the functional, easy to parse and other characteristics. We can refer to the following data formats:

The encoding and decoding

09 speaking, we analyzed the sequence of encoding and decoding process, to achieve a good network communication protocol, it is compatible with good serialization framework is very important. If only simple data transfer objects, we can choose the relatively good performance of Protobuf serialization, help to improve the performance of network communications.

6. Adjust the Linux TCP parameters setting options

If the RPC TCP short connection is achieved, we can optimize network traffic by modifying the Linux TCP-based configuration items. Before you start optimizing TCP configuration items, we start to understand the three-way handshake to establish a TCP connection and four-way handshake to close a TCP connection, the understanding of the content behind such help.
Three-way handshake
Four-way handshake
We can sysctl -a | grep net.xxx command to view the running Linux system default parameter settings of TCP, if you need to modify a particular configuration, you can edit vim / etc / sysctl.conf, adding the need to modify the configuration items, and by sysctl -p command to run the configuration item is set to take effect after the modification. Usually we will improve network throughput and reduce latency by modifying the following configuration items.
And that we, except for the last item of the Linux system configuration settings TCP tuning, others are more tuning from different levels of the RPC code programming optimization Detailed optimization from the perspective of the ultimate realization of a set of RPC communication framework the optimal path.
Understand this, you can do the technology selection according to their own business scenarios, but also a good solution to some performance issues arising in the course.

to sum up

In today's distributed systems, particularly systems to micro-services today, communication between services is particularly frequent, master communication theory and communication protocols between services optimization is an essential skill of your item.
In some scenarios are more concurrent system, I prefer to use this set of Dubbo RPC protocol implementation. Dubbo single long connected communication protocol is established, network I / O read and write operations of non-blocking NIO, a sequence more compatible framework Kryo, FST, Protobuf superior properties, very high concurrency, small business scenarios object transmission practical.
In the enterprise systems, business is often more complex than ordinary Internet products, may be more than data transmission, as well as transfer pictures and files between services and service, so RPC communication protocol design considerations are more functional requirements, not the pursuit of the ultimate in performance. Other communications framework in terms of functionality, ecological and easy to use, easy to entry and other more advantages.

Questions

Currently implementation framework Java RPC communication There are many RPC protocol for communication there are many, in addition to Dubbo agreement, you also have used other RPC communication protocol it? By talking about this study, you can compare their advantages and disadvantages to talk about it?
We look forward to seeing your opinions in the comments section. You are welcome to click on the "ask a friend to read", today the share content to their friends, invited him to study together.
Published 24 original articles · won praise 0 · Views 405

Guess you like

Origin blog.csdn.net/WillliveWillWork/article/details/105242588