MCPcopy Index your code
hub / github.com/dtm-labs/dtm

github.com/dtm-labs/dtm @v1.19.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.19.0 ↗
1,246 symbols 5,829 edges 161 files 380 documented · 30%
README

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/MariaDB, Redis, MongoDB, Postgres, TDSQL, etc.
  • Support for multiple storage engines: MySQL/MariaDB (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 * cache management: thoroughly guarantee the cache final consistency and strong consistency * flash-sales to deduct inventory: in extreme cases, it is also possible to ensure that the precise inventory in Redis is exactly the same as the final order created, without the need for manual adjustment * Non-monolithic order system: Dramatically simplifies the architecture * Event publishing/subscription: better outbox pattern

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!

Extension points exported contracts — how you extend this code

Store (Interface)
Store defines storage relevant interface [3 implementers]
dtmsvr/storage/store.go
DBSpecial (Interface)
DBSpecial db specific operations [2 implementers]
client/dtmcli/dtmimp/db_special.go
TransBaseOption (FuncType)
TransBaseOption setup func for TransBase
client/dtmgrpc/options.go
WfFunc (FuncType)
WfFunc is the type for workflow function
client/workflow/workflow.go
SleepCancelHandler (FuncType)
SleepCancelHandler 1
test/busi/base_http.go
GlobalComponents (Interface)
(no doc)
admin/src/components.d.ts
StorageFactory (Interface)
StorageFactory is factory to get storage instance. [1 implementers]
dtmsvr/storage/registry/registry.go
TccGlobalFunc (FuncType)
TccGlobalFunc type of global tcc call
client/dtmcli/trans_tcc.go

Core symbols most depended-on inside this repo

GetFuncName
called by 175
client/dtmcli/dtmimp/utils.go
waitTransProcessed
called by 99
test/types.go
E2P
called by 86
client/dtmcli/dtmimp/utils.go
Add
called by 71
client/dtmgrpc/msg.go
GenReqHTTP
called by 58
test/busi/base_types.go
reqFrom
called by 52
test/busi/base_types.go
CallBranch
called by 48
client/dtmcli/xa.go
WrapHandler
called by 44
dtmutil/utils.go

Shape

Function 612
Method 498
Struct 94
Interface 25
FuncType 15
TypeAlias 2

Languages

Go97%
TypeScript3%

Modules by API surface

test/busi/busi_grpc.pb.go135 symbols
client/dtmgrpc/dtmgpb/dtmgimp.pb.go87 symbols
client/dtmgrpc/dtmgpb/dtmgimp_grpc.pb.go55 symbols
dtmsvr/storage/boltdb/boltdb.go35 symbols
client/dtmcli/dtmimp/utils.go29 symbols
dtmsvr/storage/redis/redis.go28 symbols
client/workflow/workflow.go24 symbols
dtmsvr/storage/sql/sql.go23 symbols
test/busi/busi.pb.go20 symbols
dtmsvr/storage/store.go20 symbols
dtmsvr/trans_status.go19 symbols
test/busi/base_grpc.go18 symbols

Dependencies from manifests, versioned

github.com/aliyun/alibaba-cloud-sdk-gov1.61.1800 · 1×
github.com/armon/go-metricsv0.4.1 · 1×
github.com/beorn7/perksv1.0.1 · 1×
github.com/cespare/xxhash/v2v2.2.0 · 1×
github.com/chenzhuoyu/base64xv0.0.0-2022111506244 · 1×
github.com/coreos/go-semverv0.3.0 · 1×
github.com/dapr/daprv1.10.9 · 1×

Datastores touched

user_accountCollection · 1 repos
barrierCollection · 1 repos
dtm_busiDatabase · 1 repos
(mongodb)Database · 1 repos
dtm_barrierDatabase · 1 repos

For agents

$ claude mcp add dtm \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact