Seata golang first anniversary review


Author Liu Xiaomin
Source|Alibaba cloud official account

Seata is an easy-to-use, high-performance, open source one-stop distributed transaction solution. Seata has been sought after by everyone since its open source in January 2019. At present, hundreds of enterprises have implemented technology in the production environment.

In April 2020, we started the multilingual golang project based on Seata. After one year of development, we are glad that Seata golang has released version 1.0.0.

On April 17 this year, I was lucky to introduce Seata golang to gopher who is keen on golang at gopher meetup in Chengdu.

Seata golang first anniversary review

Seata golang first anniversary review

At the meeting, we demonstrated how to use Seata golang to access to micro services to ensure the data consistency between services. In addition, we also introduced the core principle of Seata, the principle and access of MySQL driver, and the future planning of Seata golang. Finally, we made a QA on the issues related to Seata that we are concerned about.

Seata golang first anniversary review

Seata principle

At the event, we shared the working principle of Seata with you in combination with the demo of {Seata golang. As shown in the figure below, it is a simple workflow of Seata at mode.

Seata golang first anniversary review

  • TC (the right half of the figure): Transaction Coordinator, which is a distributed transaction coordinator.
  • Tm: transaction manager, which is a transaction manager responsible for opening, committing and rolling back global transactions.
  • RM: resource manager, which manages branch transaction resources. After joining the global transaction group, it reports the execution status of branch transactions to TC.
  • XID: when TM starts a global transaction, a globalsession will be created in TC. The globally unique ID of globalsession is XID.
  • Branchid: after RM registers a branch transaction with TC, it generates a branchsession on the TC side. The branchid globally uniquely identifies the branchsession.

When RM reports branch execution failure to TC, TC will mark the status of this branchsession as failed. When TM initiates rollback, TC will find all successfully executed transaction branches according to XID and notify them to rollback.

MySQL Driver

The open source MySQL driver project developed recently is based on go SQL driver / MySQL version 1.5.0. It naturally integrates the distributed transaction capability of Seata golang and fully supports the abstraction of database / SQL library. Because many ORM frameworks are encapsulated based on database / SQL, Therefore, the support of database / SQL means that Seata golang can seamlessly access various ORM frameworks.

Seata golang first anniversary review

When the mysqltx object of driver executes commit or rollback, it will decide whether to interact with TC according to whether the connctx of mysqlconn has a value, and report the execution status of branch transactions. If commit is executed and connctx has a value, the undo log in sqlundoitemsbuffer and business data are submitted to the database, and then the submission status of the TC transaction branch (success or failure) is reported. Otherwise, normal commit is executed. If a rollback is executed and connctx has a value, it will be rolled back, and then the branch execution failure will be reported to TC. TC will roll back the whole global transaction according to this state. If connctx has no value, it only needs to be rolled back normally.

Seata golang first anniversary review

The above figure shows the serialized structural data of undolog JSON. We can see that the name of this data is “TXC” before modification and “GTS” after modification. If it is rolled back, a reverse compensation statement is generated: update product set name = ‘TXC’ since = 2014 where id = 1. If it is an insert operation, the reverse compensation operation is delete. If it is a delete operation, the direction compensation operation is insert.

Future planning

A small partner in the community has integrated MySQL driver into Gorm and used Seata golang in the production environment. At present, Seata golang only supports mysql. Partners can implement PgSQL and Oracle driver according to the idea of {MySQL driver. This is also one of the things that Seata golang# will plan to do in the future.

Seata golang first anniversary review

With the rise of micro service development in go language, the problem of distributed transaction will be paid more and more attention. I hope friends in the community can participate more in improving this framework, so as to give full play to its vitality, serve the community and create value.

If you have any questions, you are welcome to join the exchange group [nail group No. 33069364].

Introduction to the author

Liu Xiaomin(GitHub ID DK lockdown), currently working in H3C Chengdu Branch, is good at using Java / go language, dabbles in cloud native and micro service related technology, and currently specializes in distributed affairs.

reference material

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]