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.
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 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.
- 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.
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.
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.
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
- Seata official:https://seata.io
- Java version of Seata:https://github.com/seata/seata
- Seata golang project address:https://github.com/opentrx/seata-golang
- Driver address:https://github.com/opentrx/mysql
- Seata golang go night reading B station share:https://www.bilibili.com/video/BV1oz411e72T
- Detailed explanation of Seata golang communication model based on Getty:http://seata.io/zh-cn/blog/seata-golang-communication-mode.html
- Seata golang access guide
- Go MySQL driver integrates Seata golang to solve distributed transaction problems