Download the PHP package weaving/php-dtm without Composer
On this page you can find all versions of the php package weaving/php-dtm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download weaving/php-dtm
More information about weaving/php-dtm
Files in weaving/php-dtm
Package php-dtm
Short Description A PHP coroutine client for distributed transaction manager DTM. 分布式事务管理器 DTM 的 PHP 协程客户端
License MIT
Informations about the package php-dtm
English | 中文
Introduction
dtm/dtm-client is the PHP client of Distributed Transaction Manager DTM. It has supported distributed transaction patterns of TCC pattern, Saga pattern, and two-phase message pattern. In communicate protocol it has supported communicate with DTM Server through HTTP protocol or gRPC protocol. Also the client can safely run in PHP-FPM and Swoole coroutine environment, and it has also make support more easier for Hyperf framework.
About DTM
DTM is an open source distributed transaction manager based on Go language, which provides the powerful function of combining transactions across languages and storage engines. DTM elegantly solves distributed transaction problems such as interface idempotent, null compensation, and transaction suspension, and also provides a distributed transaction solutions that are easy to use, high performance, and easy to scale horizontally.
Advantage
- Easy to start
- Start the service with zero configuration and provide a very simple and clear HTTP interface, which greatly reduces the difficulty of getting started with distributed transactions
- Cross Programming language
- Can be used by companies with multiple language stacks. It is convenient to use in various languages such as Go, Python, PHP, NodeJs, Ruby, C#, etc.
- Simple to use
- Developers no longer worry about transaction suspension, null compensation, interface idempotent and other issues, and the first sub-transaction barrier technology handles it for you
- Easy to deploy and expand
- Depends only on MySQL/Redis, easy to deploy, easy to cluster, and easy to scale horizontally
- Multiple distributed transaction protocol support
- TCC, SAGA, XA, two-stage message, one-stop solution to various distributed transaction problems
Comparison
In non-Java languages, there is still no mature distributed transaction manager other than DTM, so here is a comparison between DTM and Seata, the most mature open source project in Java:
Features | DTM | SEATA | Memo |
---|---|---|---|
language supports | Go、C#、Java、Python、PHP... | Java | DTM is easier implemented the client to a new language |
Storage Engine | Support Database, Redis, Mongo, etc. | Database | |
Exception Handle | Sub-transaction barrier is handled automatically | By manual | DTM solves transaction suspension, null compensation, interface idempotent etc. |
SAGA | Easy to use | Complex state machine | |
Two-phase message | ✓ | ✗ | Minimal Message Eventual Consistency Architecture |
TCC | ✓ | ✓ | |
XA | ✓ | ✓ | |
AT | XA is more recommended | ✓ | AT is similar to XA, but with dirty rollback |
Single service with multiple data sources | ✓ | ✗ | |
Communicate protocol | HTTP、gRPC | Dubbo etc. | DTM is more friendly to cloud native |
Github Stargazers | DTM released version 0.1 from 2021-06-04, developing rapidly |
From the characteristics of the comparison above, DTM has great advantages in many aspects. If you consider multi-language support and multi-storage engine support, then DTM is undoubtedly your first choice.
Installation
It is very convenient to install dtm-client through Composer
- Don't forget to start DTM Server before you use it
Configuration
Configuration file
If you are using the Hyperf framework, after installing the component, you can publish a configuration file to ./config/autoload/dtm.php
with the following vendor:publish
command
If you are using a non-Hyperf framework, copy the ./vendor/dtm/dtm-client/publish/dtm.php
file to the corresponding configuration directory.
Configure middleware
Before using it, you need to configure the DtmClient\Middleware\DtmMiddleware
middleware as the server's global middleware. This middleware supports the PSR-15 specification and is applicable to all frameworks that support this specification.
For middleware configuration in Hyperf, please refer to Hyperf Documentation - Middleware chapter.
Usage
The usage of dtm-client is very simple, we provide a sample project dtm-php/dtm-sample to help you better understand and debug. Before using this component, it is also strongly recommended that you read the DTM official documentation for a more detailed understanding.
TCC pattern
The TCC pattern is a very popular flexible distributed transaction solution. The concept of TCC is composed of the acronyms of the three words Try-Confirm-Cancel. It was first published in a paper named Life beyond Distributed Transactions:an Apostate’s Opinion by Pat Helland in 2007.
Three stages of TCC
Try phase: try to execute, complete all business checks (consistency), reserve necessary business resources (pre-isolation) Confirm stage: If all branches of the Try are successful, go to the Confirm stage. Confirm actually executes the business without any business check, and only uses the business resources reserved in the Try phase Cancel stage: If one of the Try of all branches fails, go to the Cancel stage. Releases the business resources reserved in the Try phase.
If we want to carry out a business similar to inter-bank transfer between banks, the transfer out (TransOut) and the transfer in (TransIn) are in different microservices, and a typical sequence diagram of a successfully completed TCC transaction is as follows:
Example
The following shows how to use it in the Hyperf framework, other frameworks are similar
Saga pattern
The Saga pattern is one of the most well-known solutions in the field of distributed transactions, and it is also very popular in major systems. It first appeared in the paper SAGAS published by Hector Garcaa-Molrna & Kenneth Salem in 1987.
Saga is an eventual consistency transaction, also a flexible transaction, also known as a long-running transaction . Saga is composed of a series of local transactions. After each local transaction updates the database, it will publish a message or an event to trigger the execution of the next local transaction in the Saga global transaction. If a local transaction fails because some business rules cannot be satisfied, Saga performs compensating actions for all transactions that were successfully committed before the failed transaction. Therefore, when the Saga pattern is compared with the TCC pattern, it often becomes more troublesome to implement the rollback logic due to the lack of resource reservation steps.
Sub-transaction split of Saga
For example, we want to carry out a business similar to inter-bank transfer between banks, and transfer 30 dollar in account A to account B. According to the principle of Saga transaction, we will split the entire global transaction into the following services:
- Transfer out (TransOut) service, the account A will deduct 30 dollar
- Transfer out compensation (TransOutCompensate) service, roll back the above transfer out operation, that is, increase the account A by 30 dollar
- Transfer in (TransIn) service, the account B will be increased by 30 dollar
- Transfer in compensation (TransInCompensate) service, roll back the above transfer in operation, that is, the account B is reduced by 30 dollar
The logic of the entire transaction is:
Execute the transfer out successfully => Execute the transfer in successfully => the global transaction is completed
If an error occurs in the middle, such as an error in transferring to the B account, the compensation operation of the executed branch will be called, namely:
Execute transfer out successfully => execute transfer in failure => execute transfer in compensation successfully => execute transfer out compensation successfully => global transaction rollback completed
The following is a typical sequence diagram of a successfully completed SAGA transaction:
Example
The following shows how to use it in the Hyperf framework, other frameworks are similar
All versions of php-dtm with dependencies
ext-json Version *
hyperf/context Version ^2.2
hyperf/guzzle Version ^2.2
psr/http-server-middleware Version ^1.0