dtm README

The project's README from github.com/dtm-labs/dtm. As one markdown file: README.md.

license Build Status codecov Go Report Card Go Reference Mentioned in Awesome Go

English | 简体中文

Distributed Transactions Manager

What is DTM

DTM is a distributed transaction framework which provides cross-service eventual data consistency. It provides saga, tcc, xa, 2-phase message, outbox, workflow patterns for a variety of application scenarios. It also supports multiple languages and multiple store engine to form up a transaction as following:

function-picture

Who's using DTM (partial)

Tencent

Bytedance

Ivydad

More

Features

Application scenarios.

DTM can be applied to data consistency issues in a large number of scenarios, here are a few common ones

Cook Book

Quick start

run dtm

git clone https://github.com/dtm-labs/dtm && cd dtm
go run main.go

Start an example

Suppose we want to perform an inter-bank transfer. The operations of transfer out (TransOut) and transfer in (TransIn) are coded in separate micro-services.

Here is an example to illustrate a solution of dtm to this problem:

git clone https://github.com/dtm-labs/quick-start-sample.git && cd quick-start-sample/workflow-grpc
go run main.go

Code

Usage

wfName := "workflow-grpc"
err = workflow.Register(wfName, func(wf *workflow.Workflow, data []byte) error {
  // ...
  // Define a transaction branch for TransOut
  wf.NewBranch().OnRollback(func(bb *dtmcli.BranchBarrier) error {
    // compensation for TransOut
    _, err := busiCli.TransOutRevert(wf.Context, &req)
    return err
  })
  _, err = busiCli.TransOut(wf.Context, &req)
  // check error

  // Define another transaction branch for TransIn
  wf.NewBranch().OnRollback(func(bb *dtmcli.BranchBarrier) error {
    _, err := busiCli.TransInRevert(wf.Context, &req)
    return err
  })
  _, err = busiCli.TransIn(wf.Context, &req)
  return err
}

// ...
req := busi.BusiReq{Amount: 30, TransInResult: ""}
data, err := proto.Marshal(&req)

// Execute workflow
_, err = workflow.ExecuteCtx(wfName, shortuuid.New(), data)
logger.Infof("result of workflow.Execute is: %v", err)

When the above code runs, we can see in the console that services TransOut, TransIn has been called.

Rollback upon failure

If any forward operation fails, DTM invokes the corresponding compensating operation of each sub-transaction to roll back, after which the transaction is successfully rolled back.

Let's purposely trigger the failure of the second sub-transaction and watch what happens

// req := busi.BusiReq{Amount: 30, TransInResult: ""}
req := busi.BusiReq{Amount: 30, TransInResult: "FAILURE"}
})

we can see in the console that services TransOut, TransIn, TransOutRevert has been called

More examples

If you want more quick start examples, please refer to dtm-labs/quick-start-sample

The above example mainly demonstrates the flow of a distributed transaction. More on this, including practical examples of how to interact with an actual database, how to do compensation, how to do rollback, etc. please refer to dtm-examples for more examples.

Give a star! ⭐

If you think this project is interesting, or helpful to you, please give a star!

dtm (dtm-labs/dtm)

Searching Database…

10.9K ·GitHub
这是什么?DTM 是一个分布式事务框架,提供跨服务的最终数据一致性。它实现了 Saga、TCC、XA、二阶段消息、本地消息表(outbox)和工作流等模式,适用于不同的应用场景。
它是引入到我代码里的库,还是一个单独运行的服务?两者都是:一个独立的 dtm 服务器负责处理事务,你的服务则通过 SDK 与它通信,支持 Go、Java、PHP、C#、Python 和 Node.js。
怎么跑起第一个事务?按照 README 的快速开始,先运行 dtm 服务器,再运行一个跨行转账示例,其中 TransOut 和 TransIn 分别位于两个不同的微服务里;更多示例在 dtm-labs/quick-start-sample 仓库中。
它能协调哪些数据库之间的事务?事务分支支持 MySQL/MariaDB、Redis、MongoDB、Postgres 和 TDSQL。至于 dtm 自身的存储,MySQL/MariaDB 是常见选择,Redis 是高性能方案,BoltDB 适合开发和测试。
如果事务执行到一半某一步失败了,会发生什么?DTM 会调用每个子事务对应的补偿操作,把整个事务回滚。README 的演示故意让第二个子事务失败,你就能看到 TransOutRevert 被调用。
它够稳定吗,能放心用在生产系统里吗?它专为高可用和轻松水平扩展而设计,README 列出的用户包括腾讯、字节跳动和爱羽爸。
README 里提到的 'better outbox' 是什么?指的是二阶段消息模式:作者把它定位成比经典 outbox 方案更优雅的解决方案,而且可以跨多个数据库使用。
它实际适用于哪些场景?缓存一致性管理、Redis 中的秒杀库存扣减(必须与创建的订单严格一致)、非单体架构的订单系统,以及事件的发布/订阅。
现在能用 MongoDB 作为 dtm 的存储吗?还不行:README 把 MongoDB 列为规划中的存储引擎,目前请使用 MySQL/MariaDB 或 Redis。
它能对接特定的 Go 微服务框架吗?可以,它开箱即用地支持 go-zero、go-kratos/kratos 和 polarismesh/polaris 这几种微服务架构。
Creator
dtm-labs
Size
3.4 MB
Stars
10.9K
Age
5y
Commits · 90 Days