SlideShare a Scribd company logo
MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Comes Great Responsibility
Distributed Transactions: with Great
Power Comes Great Responsibility
Ankur Raina, Sr. TSE @ MongoDB
But, Really…
MongoDB Transactions are
ACID Compliant
(yes, even across shards)
Relational vs document data modeling
Transactions make application
development easier
• Don’t need to worry about interim states
• Either a customer is signed-up or they are not
• Rollback/Abort is handled automatically if any
single statement fails
• Point-in-time consistency across reads/writes
def insertDocuments(s):
s.client.db.col.insert_one({'abc': 1}, session=s)
s.client.db.col.insert_one({'xyz': 999}, session=s)
with client.start_session() as s:
s.with_transaction(insertDocuments);
New API in MongoDB 4.2!
New in MongoDB 4.2 -- Cross Shard Transactions
Transactions Guarantees
Multi- Collection
Database
Statement
Document
Shard
All or Nothing Execution
TXN
Application
MongoDB
Uncommitted Writes
{ Document 1 } { Document 2 }
{ Document 3 }
Committed Writes
Snapshot Isolation
100 101 102 103 104 105 106 107 108 109 110 111
Snapshot
102
Snapshot
108
Read Your Own Writes
Operation
Read
TXN
{_id: “OWNER12345”,
name: { first: “Aly”,
last: “Cabral”},
accountType: “Paid”,
pets: [ { type: “Dog”,
name: “Lady”}],
contact: { phoneNumber: 8008675309},
Address: { city: “New York”,
street: “94th”,
zipecode: 10025}}
Uncommitted Writes
street: “92nd”
Address.street = 94th
Address.street = 92nd
Transactions Guidelines
• All data modeling rules still apply
• Transactions should not be the most common
operation
• Pass in the session to all statements
def insertDocuments(s):
s.client.db.col.insert_one({'abc': 1}, session=s)
s.client.db.col.insert_one({'xyz': 999}, session=s)
with client.start_session() as s:
s.with_transaction(insertDocuments);
• Implement retry logic, transactions can always
abort
• Don’t unnecessarily leave snapshots open
… …
keys
K K K KVV V V
txn
time
data
next
Update Structure
txn
time
data
next
WiredTiger Cache
• To trigger write conflicts, make sure you’re doing
writes
{_id: “OWNER12345”,
name: { first: “Aly”,
last: “Cabral”},
accountType: “Paid”,
pets: [ { type: “Dog”,
name: “Lady”}],
contact: { phoneNumber: 8008675309},
Address: { city: “New York”,
street: “94th”,
zipecode: 10025}}
street: “92nd”
street: “94th”
• Plan for DDL Operations
Distributed Transactions
Workflow
Participant
any shard that executes operations on behalf
of a given transaction
Coordinator
A single shard that is responsible for
coordinating the commit across shards for a
given transaction
P
S
S
S
P
S
Query Router
Insert {_id : “abc”}Insert {_id : “xyz”}
Coordinator!
Commit transaction
Show that the commit happens across all replicas
Imagine a world without transactions
“ We see at least 2x performance
improvement when running MongoDB
transactions vs logic on our own, in-app”
Even better perf results
Collocate your data
Even better perf results
Write to a single shard
READ CONCERN = READ ISOLATION
Options: LOCAL, MAJORITY,
SNAPSHOT, LINEARIZABLE
Fun Fact: ReadConcern: Majority does not need to
coordinate across nodes to return results!
Read Concern: Snapshot
P
S
S
P
S
S
P
S
S
time: 102
Majority
or
Local
time: 102 time: 110 time: 104
Snapshot Isolation is expensive
All shards need to coordinate the time
The same snapshot is used throughout the duration of
the transaction
Even once a snapshot time is selected, some shards
may not have that time
More New Functionality in 4.2
Transactions Larger than 16MB
{ <statement 1>,
<statement 2>,
<statement 3>,
<statement 4>}
Prior to MongoDB 4.2:
This does not mean you can have infinitely large
transactions --
WT still has to maintain the history since the snapshot
time
Transactions automatically abort after 60 seconds –
TUNABLE.
transactionLifetimeLimitSeconds
Mutable Shard Key Values
Tiered storage: Aging out older documents to low
cost storage shard
Global-redistribution: Rehoming documents to a
new region
• Use MongoDB 4.2 drivers
• Transactions targeting a single shard will be faster than those
spanning multiple shards
• Best practice: No more than 1,000 documents modified in a
single transaction
• No arbiters
• Chunk migrations will block behind running transactions
You can get started with $200 of free credits on MongoDB Atlas (https://cloud.mongodb.com)
Use code MONGODB4DOT2, until the end of 2019.
Remember
Thank You!
• Free Online
• Register Now! -
https://university.mongodb.com/courses/M042/about

More Related Content

PDF
MongoDB. local Houston 2019: Distributed Transactions: With Great Power Comes...
PPTX
MongoDB World 2019: Distributed Transactions: With Great Power Comes Great Re...
PDF
MongoDB .local Bengaluru 2019: Realm: The Secret Sauce for Better Mobile Apps
PDF
Node withoutservers aws-lambda
PDF
MongoDB .local Bengaluru 2019: Lift & Shift MongoDB to Atlas
PPTX
Data analytics at scale implementing stateful stream processing - publish
PPTX
How ddd, cqrs and event sourcing constitute the architecture of the future
PPTX
Apache Incubator Samza: Stream Processing at LinkedIn
MongoDB. local Houston 2019: Distributed Transactions: With Great Power Comes...
MongoDB World 2019: Distributed Transactions: With Great Power Comes Great Re...
MongoDB .local Bengaluru 2019: Realm: The Secret Sauce for Better Mobile Apps
Node withoutservers aws-lambda
MongoDB .local Bengaluru 2019: Lift & Shift MongoDB to Atlas
Data analytics at scale implementing stateful stream processing - publish
How ddd, cqrs and event sourcing constitute the architecture of the future
Apache Incubator Samza: Stream Processing at LinkedIn

What's hot (20)

PDF
AWS DynamoDB Streams - A quick introduction
PDF
10 - MongoDB
PDF
MongoDB World 2016: Scaling Targeted Notifications in the Music Streaming Wor...
PDF
Bank of China Tech Talk 2: Introduction to Streaming Data and Stream Processi...
PPT
Amazon’s Cloud Computing Efforts
PDF
MongoDB .local Bengaluru 2019: MongoDB Atlas Data Lake Technical Deep Dive
PPTX
Beyond Microservices: Streams, State and Scalability
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
First Steps with Apache Kafka on Google Cloud Platform
PDF
New Features in MongoDB Atlas
PDF
Ali AlShehab Insight Demo LetsHang
PDF
Devoxx university - Kafka de haut en bas
PPTX
Redis For Distributed & Fault Tolerant Data Plumbing Infrastructure
PDF
Netflix Keystone—Cloud scale event processing pipeline
PDF
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
PDF
Troubleshooting as Your Kafka Clusters Grow (Krunal Vora, Tinder) Kafka Summi...
PDF
Using Apache Kafka to Analyze Session Windows
PDF
MongoDB at Baidu
PDF
Event Sourcing in less than 20 minutes - With Akka and Java 8
PDF
Use Apache Gradle to Build and Automate KSQL and Kafka Streams (Stewart Bryso...
AWS DynamoDB Streams - A quick introduction
10 - MongoDB
MongoDB World 2016: Scaling Targeted Notifications in the Music Streaming Wor...
Bank of China Tech Talk 2: Introduction to Streaming Data and Stream Processi...
Amazon’s Cloud Computing Efforts
MongoDB .local Bengaluru 2019: MongoDB Atlas Data Lake Technical Deep Dive
Beyond Microservices: Streams, State and Scalability
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
First Steps with Apache Kafka on Google Cloud Platform
New Features in MongoDB Atlas
Ali AlShehab Insight Demo LetsHang
Devoxx university - Kafka de haut en bas
Redis For Distributed & Fault Tolerant Data Plumbing Infrastructure
Netflix Keystone—Cloud scale event processing pipeline
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
Troubleshooting as Your Kafka Clusters Grow (Krunal Vora, Tinder) Kafka Summi...
Using Apache Kafka to Analyze Session Windows
MongoDB at Baidu
Event Sourcing in less than 20 minutes - With Akka and Java 8
Use Apache Gradle to Build and Automate KSQL and Kafka Streams (Stewart Bryso...
Ad

Similar to MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Comes Great Responsibility (20)

PPTX
MongoDB World 2018: What's Next? The Path to Sharded Transactions
PDF
MongoDB .local Munich 2019: Distributed Transactions: With Great Power Comes ...
PPTX
Are Transactions Right For You? Using and Not Abusing Transactions
PPTX
Sharding in MongoDB 4.2 #what_is_new
PPTX
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
PDF
To Transact or Not to Transact
PDF
MongoDB World 2018: Building a New Transactional Model
PDF
Шардинг в MongoDB, Henrik Ingo (MongoDB)
PDF
MongoDB.local Berlin: How and when to use multi-document transactions
PDF
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
PPTX
Sharding
PDF
MongoDB - How to model and extract your data
PDF
MongoDB FabLab León
PPTX
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
PDF
MongoDB.pdf
PDF
MongoDB for Coder Training (Coding Serbia 2013)
PPTX
MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
PPTX
How and When to Use Multi-Document Distributed Transactions
PPTX
MongoDB.local Dallas 2019: How and When to Use Multi-Document Distributed Tra...
PPTX
MongoDB.local Austin 2018: How and When to Use Multi-Document Distributed Tr...
MongoDB World 2018: What's Next? The Path to Sharded Transactions
MongoDB .local Munich 2019: Distributed Transactions: With Great Power Comes ...
Are Transactions Right For You? Using and Not Abusing Transactions
Sharding in MongoDB 4.2 #what_is_new
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
To Transact or Not to Transact
MongoDB World 2018: Building a New Transactional Model
Шардинг в MongoDB, Henrik Ingo (MongoDB)
MongoDB.local Berlin: How and when to use multi-document transactions
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
Sharding
MongoDB - How to model and extract your data
MongoDB FabLab León
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
MongoDB.pdf
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
How and When to Use Multi-Document Distributed Transactions
MongoDB.local Dallas 2019: How and When to Use Multi-Document Distributed Tra...
MongoDB.local Austin 2018: How and When to Use Multi-Document Distributed Tr...
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
PDF
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
Spectral efficient network and resource selection model in 5G networks
NewMind AI Monthly Chronicles - July 2025
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Modernizing your data center with Dell and AMD
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm

MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Comes Great Responsibility

  • 2. Distributed Transactions: with Great Power Comes Great Responsibility Ankur Raina, Sr. TSE @ MongoDB
  • 3. But, Really… MongoDB Transactions are ACID Compliant (yes, even across shards)
  • 4. Relational vs document data modeling
  • 5. Transactions make application development easier • Don’t need to worry about interim states • Either a customer is signed-up or they are not • Rollback/Abort is handled automatically if any single statement fails • Point-in-time consistency across reads/writes
  • 6. def insertDocuments(s): s.client.db.col.insert_one({'abc': 1}, session=s) s.client.db.col.insert_one({'xyz': 999}, session=s) with client.start_session() as s: s.with_transaction(insertDocuments); New API in MongoDB 4.2! New in MongoDB 4.2 -- Cross Shard Transactions
  • 9. All or Nothing Execution TXN Application MongoDB Uncommitted Writes { Document 1 } { Document 2 } { Document 3 } Committed Writes
  • 10. Snapshot Isolation 100 101 102 103 104 105 106 107 108 109 110 111 Snapshot 102 Snapshot 108
  • 11. Read Your Own Writes Operation Read TXN {_id: “OWNER12345”, name: { first: “Aly”, last: “Cabral”}, accountType: “Paid”, pets: [ { type: “Dog”, name: “Lady”}], contact: { phoneNumber: 8008675309}, Address: { city: “New York”, street: “94th”, zipecode: 10025}} Uncommitted Writes street: “92nd” Address.street = 94th Address.street = 92nd
  • 13. • All data modeling rules still apply • Transactions should not be the most common operation • Pass in the session to all statements def insertDocuments(s): s.client.db.col.insert_one({'abc': 1}, session=s) s.client.db.col.insert_one({'xyz': 999}, session=s) with client.start_session() as s: s.with_transaction(insertDocuments);
  • 14. • Implement retry logic, transactions can always abort • Don’t unnecessarily leave snapshots open
  • 15. … … keys K K K KVV V V txn time data next Update Structure txn time data next WiredTiger Cache
  • 16. • To trigger write conflicts, make sure you’re doing writes {_id: “OWNER12345”, name: { first: “Aly”, last: “Cabral”}, accountType: “Paid”, pets: [ { type: “Dog”, name: “Lady”}], contact: { phoneNumber: 8008675309}, Address: { city: “New York”, street: “94th”, zipecode: 10025}} street: “92nd” street: “94th” • Plan for DDL Operations
  • 18. Participant any shard that executes operations on behalf of a given transaction Coordinator A single shard that is responsible for coordinating the commit across shards for a given transaction
  • 19. P S S S P S Query Router Insert {_id : “abc”}Insert {_id : “xyz”} Coordinator! Commit transaction Show that the commit happens across all replicas
  • 20. Imagine a world without transactions “ We see at least 2x performance improvement when running MongoDB transactions vs logic on our own, in-app”
  • 21. Even better perf results Collocate your data
  • 22. Even better perf results Write to a single shard
  • 23. READ CONCERN = READ ISOLATION Options: LOCAL, MAJORITY, SNAPSHOT, LINEARIZABLE Fun Fact: ReadConcern: Majority does not need to coordinate across nodes to return results!
  • 24. Read Concern: Snapshot P S S P S S P S S time: 102 Majority or Local time: 102 time: 110 time: 104
  • 25. Snapshot Isolation is expensive All shards need to coordinate the time The same snapshot is used throughout the duration of the transaction Even once a snapshot time is selected, some shards may not have that time
  • 27. Transactions Larger than 16MB { <statement 1>, <statement 2>, <statement 3>, <statement 4>} Prior to MongoDB 4.2:
  • 28. This does not mean you can have infinitely large transactions -- WT still has to maintain the history since the snapshot time Transactions automatically abort after 60 seconds – TUNABLE. transactionLifetimeLimitSeconds
  • 29. Mutable Shard Key Values Tiered storage: Aging out older documents to low cost storage shard Global-redistribution: Rehoming documents to a new region
  • 30. • Use MongoDB 4.2 drivers • Transactions targeting a single shard will be faster than those spanning multiple shards • Best practice: No more than 1,000 documents modified in a single transaction • No arbiters • Chunk migrations will block behind running transactions You can get started with $200 of free credits on MongoDB Atlas (https://cloud.mongodb.com) Use code MONGODB4DOT2, until the end of 2019. Remember
  • 32. • Free Online • Register Now! - https://university.mongodb.com/courses/M042/about