mongodb4.0即将推出,最大的亮点莫过于,开始支持‘真正的’事务了。为什么说是真正的呢?之前的行级别原子性,两阶段提交,要么应用场景有限,要么实现成本太高,有点‘鸡肋’。
大年三十的时候,mongodb的CTO兼联合创始人Eliot Horowitz就发文给大家介绍了mongodb4.0的事务特性,让我们一起看一下吧!
有兴趣的可以去官网查看英文版,https://www.mongodb.com/blog/post/multi-document-transactions-in-mongodb,这里只是解读,不是翻译。
MongoDB 4.0 will add support for multi-documenttransactions, making it the only database to combine the speed, flexibility,and power of the document model with ACID data integrity guarantees. Throughsnapshot isolation, transactions provide a globally consistent view of data,and enforce all-or-nothing execution to maintain data integrity.
多文档事务:可以理解为多行,以前都是单行级别的原子性。
目标:高速、灵活、以及基于文档模型的事务支持
事务:通过快照实现、全局一致性,由此可鉴,应该是全局锁,性能不会太高
Transactions in MongoDB will feel just liketransactions developers are familiar with from relational databases. They willbe multi-statement, with similar syntax (e.g. start_transaction andcommit_transaction), making them familiar to anyone with prior transactionexperience. The changes to MongoDB that enable multi-document transactions willnot impact performance for workloads that do not require them. In MongoDB 4.0,which will be released this summer*, transactions will work across a singlereplica set, and MongoDB 4.2* will support transactions across a shardeddeployment.
事务可执行范围:4.0是副本集(难道可以跨库吗?really?),4.2是集群
Because documents can bring together relateddata that would otherwise be modeled across separate parent-child tables in arelational schema, MongoDB’s atomic single-document operations already providetransaction semantics that meet the data integrity needs of the majority ofapplications. But multi-document transactions will make it easier than ever fordevelopers to address a complete range of use-cases, while for many, simplyknowing that they are available will provide critical peace of mind. WithMongoDB 4.0, you’ll be able to rely on transactional integrity, regardless ofhow you model your data.
之前的版本,若要实现事务,可以将事务关联的数据放在一行,利用mongodb对每行操作的原子性来实现事务,当然这种实现应用场景有限。另外,可以通过代码中控制,使用mongodb所谓的‘两阶段提交’,来实现事务,但也够麻烦的,且事务中逻辑若复杂些,就极其考验开发者的技能了。mongodb4.0目标就是弥补上述的不足,让你可以像操作sql型数据库中事务一样来操作mongodb的事务。
The imminent arrival of transactions is theculmination of a multi-year engineering effort, beginning over 3 years ago withthe integration of the WiredTiger storage engine. We’ve laid the groundwork inalmost every part of the server – from the storage layer itself, to thereplication consensus protocol, to the sharding architecture. We’ve built outfine-grained consistency and durability guarantees, introduced a global logicalclock, refactored cluster metadata management, and more. We’ve also exposed allof these enhancements through APIs that are fully consumable by our drivers.We’re now about 85% of the way through the backlog of features that enabletransactions, as this diagram summarizes:
介绍了事务的起源,为了事务功能所做的努力,团队目标以及进展。看来当年收购WiredTiger是预谋已久(没有任何偏见,我也希望mongodb越来越强大、好用),也足以见得,mongodb早已计划在sql型数据库的天下分一杯羹,且大有吞并之势!走别人的路,让别人无路可走,哈哈。
但是,mongodb既要实现事务,又要兼顾高性能、灵活的属性,还有很长一段路要走。而且,当今的老大Oracle也并非数据库界的诺基亚,也将支持json格式的数据。看来sql和nosql以后的界限就没有那么明显了,两者的合并将是大势所趋。