• Stars
    star
    10,116
  • Rank 3,439 (Top 0.07 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created over 3 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A distributed transaction framework, supports workflow, saga, tcc, xa, 2-phase message, outbox patterns, supports many languages.

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

  • Support for multiple transaction modes: SAGA, TCC, XA, Workflow, Outbox
  • Multiple languages support: SDK for Go, Java, PHP, C#, Python, Nodejs
  • Better Outbox: 2-phase messages, a more elegant solution than Outbox, support multi-databases
  • Multiple database transaction support: Mysql, Redis, MongoDB, Postgres, TDSQL, etc.
  • Support for multiple storage engines: Mysql (common), Redis (high performance), BoltDB (dev&test), MongoDB (under planning)
  • Support for multiple microservices architectures: go-zero, go-kratos/kratos, polarismesh/polaris
  • Support for high availability and easy horizontal scaling

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!

More Repositories

1

rockscache

The First Redis Cache Library To Ensure Eventual Consistency And Strong Consistency With DB.
Go
541
star
2

client-csharp

The new client for dtm in csharp, including workflow, dtmcli, and dtmgrpc
C#
123
star
3

dtm-examples

various examples for dtm
Go
79
star
4

dtmcli-php

a php client for distributed transaction framework dtm.
PHP
32
star
5

dtm-cases

some classic applications of dtm
Go
26
star
6

dtmcli-java

Official java client for distributed transaction framework dtm
Java
26
star
7

dtmcli-node

a node client for distributed transaction framework dtm.
TypeScript
25
star
8

dtmcli-csharp-sample

dtmcli c# sample
C#
23
star
9

dtmgrpc-csharp

A c# gRPC client of distributed transaction manager DTM.
C#
21
star
10

dtm.pub

dtm-简体中文文档
Shell
19
star
11

quick-start-sample

quick start sample for client
Go
19
star
12

dtmcli-py

a python client for distributed transaction framework dtm.
Python
17
star
13

client

the new client for dtm in go, including workflow, dtmcli, and dtmgrpc
Go
15
star
14

dtmcli-java-sample

Java
13
star
15

dtmcli

a go http client for distributed transaction framework DTM.
Go
13
star
16

dtmcli-php-sample

PHP
12
star
17

dtmcli-csharp

A c# HTTP client of distributed transaction manager DTM. Deprecated, please refer to https://github.com/dtm-labs/client-csharp
C#
12
star
18

dtmdriver-clients

dtm sample project for microservice
Go
10
star
19

dtmcli-node-sample

node sample of dtmcli
JavaScript
7
star
20

dtmcli-py-sample

python sample for dtm
Python
7
star
21

dtmdriver-kratos

kratos driver for dtm
Go
6
star
22

dtmcli-go-sample

Go
4
star
23

dtmdriver-gozero

go-zero driver for supporting dtm
Go
4
star
24

logger

the lightweight logger wrapper for zap used by dtm-labs
Go
3
star
25

dtmdriver

dtm's driver interface for micro-services
Go
2
star
26

dtmgrpc-go-sample

a go sample for dtmgrpc
Go
2
star
27

dtmgrpc

grpc client for dtm. code is synced from github.com/yedf/dtm/dtmgrpc
Go
2
star
28

dtmdriver-dapr

dtm driver for dapr https://github.com/dapr/dapr
Go
2
star
29

dtmdriver-ego

Go
2
star
30

operator

[WIP]cloud native dtm operator
Go
1
star
31

dtmcli-java-spring-sample

dtmcli-java-spring-sample
Java
1
star
32

driver-gozero

dtm driver for gozero, used by the client of gozero to access dtmgrpc
Go
1
star
33

en.dtm.pub

english document for dtm
Shell
1
star
34

rockscache-java

A Redis Cache Library To Ensure Eventual Consistency And Strong Consistency With DB.
Kotlin
1
star