Coder Social home page Coder Social logo

daniel-007 / dtm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dtm-labs/dtm

0.0 1.0 0.0 1.9 MB

A distributed transaction framework that supports multiple languages, supports saga, tcc, xa, 2-phase message strategies.

Home Page: http://d.dtm.pub

License: BSD 3-Clause "New" or "Revised" License

Go 97.08% Shell 1.92% Makefile 0.40% JavaScript 0.09% Smarty 0.51%

dtm's Introduction

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 strategies 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

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/dtmcli-go-sample && cd dtmcli-go-sample
go run main.go

Code

Use

  // business micro-service address
  const qsBusi = "http://localhost:8081/api/busi_saga"
  // The address where DtmServer serves DTM, which is a url
  DtmServer := "http://localhost:36789/api/dtmsvr"
  req := &gin.H{"amount": 30} // micro-service payload
	// DtmServer is the address of DTM micro-service
	saga := dtmcli.NewSaga(DtmServer, dtmcli.MustGenGid(DtmServer)).
		// add a TransOut subtraction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom"
		Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req).
		// add a TransIn subtraction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom"
		Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req)
	// submit the created saga transaction,dtm ensures all subtractions either complete or get revoked
	err := saga.Submit()

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

Timing diagram

A timing diagram for a successfully completed SAGA transaction would be as follows:

saga-success

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 fail the forward operation of the second sub-transaction and watch what happens

app.POST(qsBusiAPI+"/TransIn", func(c *gin.Context) {
  log.Printf("TransIn")
  // c.JSON(200, "")
  c.JSON(409, "") // Status 409 for Failure. Won't be retried
})

The timing diagram for the intended failure is as follows:

saga-failed

More examples

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

Slack

You can join the DTM slack channel here.

Give a star! ⭐

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

dtm's People

Contributors

ansionfor avatar authurguo avatar dependabot[bot] avatar freemanxx avatar fsdfsffdsf avatar geffzhang avatar hitzhangjie avatar horselk avatar huanghao9015 avatar jacky-pony avatar jiahuizz avatar julian-chu avatar jxlwqq avatar kevwan avatar li-xiao-shuang avatar lsytj0413 avatar namejlt avatar neptuneg avatar pandaliu-1111 avatar parkma99 avatar rennbon avatar stulzq avatar tinattwang avatar wondar-chan avatar wuqinqiang avatar wz14 avatar xyctruth avatar ychensha avatar yedf2 avatar zxmfke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.