分布式事务最佳实践(java)

Best practices of distributed transactions(java)

请分享您使用分布式 transactions.What 种框架的经验(java),您会建议使用吗?

我会开始,例如这里 https://en.wikipedia.org/wiki/Java_Transaction_API#Open_source_JTA_implementations

作为 Narayana 项目的质量保证,我个人会推荐 http://narayana.io

分布式事务的实践远远落后于它的理论。只有一种分布式事务(2PC)的方法有大量的库和框架可供选择;而其他的你需要自己实现。不幸的是,2PC 也是最不先进的算法,因此选择它你牺牲了分区容错性和性能以换取开发的便利和速度。

让我们看一下分布式事务领域的其他主要算法。所有这些都允许您执行跨越多个数据源的事务。

两阶段提交算法(2PC)

2PC is the most developed algorithm. It's the heart of the X/Open XA standard which models a generic 2PC-based distributed transaction processing and formalizes the interaction between clients, a coordinator and resources. XA allows vendors do not integrate their solution with all the other solutions but just follow the standard and get the integration for free. JTA 是 Java 到 X/Open XA 模型的接口。

2PC的一些问题来自于协调器是单点故障。如果宕机则系统不可用,如果存在网络分区并且协调器恰好位于客户端和资源之外的其他分区则系统也不可用。

该算法的另一个问题是它的阻塞性质:一旦资源向协调器发送了协议消息,它将阻塞直到收到提交或回滚。因此,系统无法发挥其所用硬件的所有潜力。

渗滤器的交易

Percolator 的事务是分布式可序列化乐观事务。它们是在 Large-scale Incremental Processing Using Distributed Transactions and Notifications paper by Google and later were implemented in the Amazon's Transaction Library for DynamoDB and in the CockroachDB 数据库中引入的。

不同于 2PC Percolator 的交易:

  1. 不需要协调器,因此它们在存在网络分区和客户端执行事务并且受事务影响的资源恰好位于同一分区中时工作
  2. 使用类似于无锁算法的技术,因此它们是非阻塞的并且更好地利用集群的硬件

Percolator的事务可以在客户端实现,非常方便。唯一的要求是数据源必须可线性化并提供比较和设置操作。缺点是在竞争的情况下,并发事务可以相互中止。

您可以查看 Visualization of the Percolator's transaction 以了解它们的工作原理。

RAMP 交易

RAMP 事务是 Read Committed 隔离级别分布式事务。他们在 Scalable Atomic Visibility with RAMP Transactions paper by Peter Bailis. They are pretty new so they didn't get into any database yet but there are rumors that Cassandra may support them. Also Facebook reported 中被介绍,他们正在 Apollo 数据库上工作,该数据库使用 Paxos 进行复制,使用 CRDT 和 RAMP 进行跨分片交易。

与 2PC 一样,RAMP 事务需要类似协调器的服务器,但与它们不同的是,可以有任意数量的此类服务器,因此不会影响可用性。

就像 Percolator 的事务一样,RAMP 使用非阻塞方法,宽松的隔离级别有助于它避免争用问题并实现令人难以置信的性能,详情请参阅论文。

RAMP 对存储的要求也与 Percolator 的事务相同:线性化和比较并设置操作。

您可以查看 Visualization of the RAMP transaction 以了解它们的工作原理。