TarsGo: A high performance microservice framework in golang which is 5 times higher than the performance of gRPC

service governance and multi programing language support

The microservices architecture has become particularly hot in the past two years, and it has become the most mainstream architectural model. When we mention the microservices framework, we can naturally cite many well-known projects such as Dubbo, gRPC and Spring Cloud. These microservice frameworks can be divided into the following four categories depending on whether we support service governance and whether we support multiple programming languages:

Timer performance issues:

When each request comes in, Tars-Go will create a goroutine for processing, in order to process the call timeout, a timer will be created, and the timer will be deleted at the end. When the concurrency is up, the timer will be created and deleted frequently. Take up a lot of CPU time for service.

Package net SetDeadline call performance issue

In order to set the read and write timeout of the network connection, Tars-Go uses the related calls of SetReadDeadline/SetWriteDeadline of the package net, but it is found from the profile that when the concurrency is very large, these two calls take a lot of CPU time. In order to circumvent these two related calls, Sysfd is used to set the Socket read and write timeout.

Bytes Buffer related issue

As can be seen from the figure below, there is a considerable part of the time spent on the slice-related operations. In the process of encoding and decoding the tars protocol, the bytes.Buffer is used for temporary storage. When the bytes of the bytes.Buffer are not enough., slice expand is performed. At that time, a certain amount of memory space will be allocated, and the efficiency of frequent allocation is very low, so that the performance degradation is obvious when the package is large.

Other aspects of optimization

TCP connection optimization:

Use persistent connection, set up large read and write buffers, and selectively set tcpNodelay

goroutine pool

Go’s coroutines are lightweight and efficient, but high-concurrency creation and destruction of coroutines often results in some performance loss.

avoid multi-goroutine competition over chan

When chan reads and writes, there is a lock overhead. When the concurrency is large, it will affect the overall performance.

Use pointers instead of values as function parameter

Especially for large structures, when function design, function parameters use pointers instead of values, which will have a good performance improvement.

Avoid using reflection

Reflections are sometimes useful, but they often become performance killers.

Tars-Go programming example

The Tars protocol is a binary protocol. It is a language-independent IDL language. The tool automatically generates the server and client code. Below is an example of the Tars protocol:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store