dtm
A distributed transaction framework, supports workflow, saga, tcc, xa, 2-phase message, outbox patterns, supports many languages.
Top Related Projects
Temporal service
Process Orchestration Framework
Flexible framework for workflow and decision automation with BPMN and DMN. Integration with Quarkus, Spring, Spring Boot, CDI.
Quick Overview
DTM (Distributed Transaction Manager) is an open-source distributed transaction framework that supports multiple languages and storage systems. It aims to ensure data consistency in microservices architectures, providing support for various transaction modes such as TCC, SAGA, XA, and 2-phase messaging.
Pros
- Supports multiple programming languages (Go, Java, PHP, Python, Node.js)
- Offers various transaction modes to suit different scenarios
- High performance and low invasiveness to existing systems
- Provides strong consistency guarantees for distributed transactions
Cons
- Learning curve for understanding distributed transaction concepts
- May add complexity to simpler applications that don't require distributed transactions
- Requires careful configuration and management in production environments
- Limited community support compared to some more established frameworks
Code Examples
- Initiating a SAGA transaction in Go:
saga := dtmcli.NewSaga(dtmUrl, dtmcli.MustGenGid(dtmUrl)).
Add(busiUrl+"/TransOut", busiUrl+"/TransOutCompensate", &TransReq{Amount: 30}).
Add(busiUrl+"/TransIn", busiUrl+"/TransInCompensate", &TransReq{Amount: 30})
err := saga.Submit()
- Using TCC (Try-Confirm-Cancel) pattern in Go:
tcc := dtmcli.TccGlobalTransaction(dtmUrl, dtmcli.MustGenGid(dtmUrl))
err := tcc.Execute(func(tcc *dtmcli.Tcc) error {
resp, err := tcc.CallBranch(&TransReq{Amount: 30}, busiUrl+"/TccTry", busiUrl+"/TccConfirm", busiUrl+"/TccCancel")
if err != nil {
return err
}
return nil
})
- Implementing a SAGA participant in Go:
app.POST("/TransOut", dtmutil.WrapHandler2(func(c *gin.Context) interface{} {
return dtmcli.SagaBarrierFromGin(c).Call(txGet, func(db *sql.DB) error {
return SagaAdjustBalance(db, TransOutUID, -req.Amount)
})
}))
Getting Started
To use DTM in a Go project:
-
Install DTM:
go get -u github.com/dtm-labs/dtm
-
Import and use DTM in your code:
import "github.com/dtm-labs/dtm/client/dtmcli" // Initialize DTM client dtmClient := dtmcli.NewDtmClient(dtmUrl) // Use DTM for distributed transactions // (See code examples above for specific usage)
-
Configure DTM server and run your application.
Competitor Comparisons
Temporal service
Pros of Temporal
- More mature and widely adopted, with a larger community and ecosystem
- Supports multiple programming languages (Go, Java, PHP, TypeScript)
- Offers advanced features like versioning and searchable workflows
Cons of Temporal
- More complex setup and configuration
- Steeper learning curve for developers
- Requires more infrastructure resources
Code Comparison
DTM (Go):
saga := dtmcli.NewSaga(dtmUrl, gid).
Add(busiApi+"/TransOut", busiApi+"/TransOutCompensate", &req).
Add(busiApi+"/TransIn", busiApi+"/TransInCompensate", &req)
err := saga.Submit()
Temporal (Go):
workflow.Execute(ctx, workflow.ExecuteParams{
WorkflowID: "transfer-money",
TaskQueue: "money-transfer",
Args: []interface{}{fromAccount, toAccount, amount},
})
Both DTM and Temporal provide distributed transaction management solutions, but they differ in scope and complexity. DTM focuses on simplicity and ease of use, while Temporal offers a more comprehensive workflow orchestration platform. DTM is lightweight and suitable for smaller projects, whereas Temporal is better suited for complex, long-running workflows in larger applications.
Process Orchestration Framework
Pros of Camunda
- More mature and widely adopted in enterprise environments
- Offers a comprehensive suite of tools for business process management
- Supports BPMN 2.0 standard, providing a visual modeling approach
Cons of Camunda
- Steeper learning curve due to its extensive feature set
- Heavier resource footprint, potentially impacting performance
- More complex setup and configuration process
Code Comparison
DTM (Go):
saga := dtmcli.NewSaga(dtmUrl, gid).
Add(busiApi+"/TransOut", busiApi+"/TransOutCompensate", &req).
Add(busiApi+"/TransIn", busiApi+"/TransInCompensate", &req)
err := saga.Submit()
Camunda (Java):
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("paymentProcess");
Summary
DTM is a lightweight, Go-based distributed transaction manager, while Camunda is a comprehensive Java-based business process management platform. DTM focuses on simplicity and performance for distributed transactions, whereas Camunda offers a broader set of features for complex business processes. The choice between them depends on specific project requirements, team expertise, and the desired level of process management complexity.
Flexible framework for workflow and decision automation with BPMN and DMN. Integration with Quarkus, Spring, Spring Boot, CDI.
Pros of Camunda BPM Platform
- More comprehensive workflow management system with a wider range of features
- Supports BPMN 2.0 standard, allowing for visual process modeling
- Offers a user-friendly web interface for process management and monitoring
Cons of Camunda BPM Platform
- Steeper learning curve due to its extensive feature set
- Heavier resource consumption, potentially impacting performance in some scenarios
- More complex setup and configuration process
Code Comparison
DTM (Go):
saga := dtmcli.NewSaga(DtmServer, gid).
Add(busi.Busi+"/TransOut", busi.Busi+"/TransOutCompensate", &req).
Add(busi.Busi+"/TransIn", busi.Busi+"/TransInCompensate", &req)
err := saga.Submit()
Camunda BPM Platform (Java):
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transferMoney");
Both repositories focus on distributed transaction management, but with different approaches. DTM is lightweight and specifically designed for microservices, while Camunda BPM Platform offers a more comprehensive workflow management solution. DTM's code is more concise and focused on distributed transactions, while Camunda's code reflects its broader scope in process management.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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:
Who's using DTM (partial)
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
- 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!
Top Related Projects
Temporal service
Process Orchestration Framework
Flexible framework for workflow and decision automation with BPMN and DMN. Integration with Quarkus, Spring, Spring Boot, CDI.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot