SlideShare a Scribd company logo
How and When to Use
Multi-Document
Transactions
@aly_cabral
Product Manager –
Distributed Systems,
MongoDB
ALYSON CABRAL
@aly_cabral
3000+ Beta Signups
{_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}}
So now you have transactions…
@aly_cabral
Owners
ID First Name Last Name Account Type
Pets
ID Name Type Weight OwnerID
Address
ID Street City OwnerID
Contact
ID Phone Number OwnerID
How and When to Use Multi-Document Distributed Transactions
Transactions Use Cases
@aly_cabral
Many to Many Relationships
Positions and Trades
Event Processing
Account Creation
Application Event Logging
Application Auditing
with client.start_session() as s:
s.start_transaction():
try:
owners.insert_one(doc1, session=s)
owners.insert_one(doc2, session=s)
except:
s.abort_transaction()
raise
s.commit_transaction()
@aly_cabral
Transactions Characteristics
Multi- Collection
Database
Statement
Document@aly_cabral
All or Nothing Execution
@aly_cabral
TXN
Application
MongoDB
Uncommitted Writes
{ Document 1 } { Document 2 }
{ Document 3 }
Committed Writes
Snapshot Isolation
@aly_cabral
100 101 102 103 104 105 106 107 108 109 110 111
Snapshot
102
Snapshot
108
Read Your Own Writes
@aly_cabral
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
Write Conflicts
@aly_cabral
TXN
Write
TXN
Write
{Doc 1}
TXN write is unable to obtain a write lock!
TXN is ABORTED
rolledback entirely
@aly_cabral
TXN
Write
Operation
Non-Transactional Write
{Doc 1}
Operation Blocks Behind TXN Commit
Infinitely retries with Backoff Logic until MaxTimeMS is reached
Operation non-transactional write is unable to obtain a write lock!
@aly_cabral
When non-transactional writes
encounter write conflicts
Operation Blocks Behind TXN
Commit
Infinitely retries with Backoff Logic until MaxTimeMS is reached
When transactions encounter write
conflicts
Transactions abort
rollback entirely
@aly_cabral
TXN
Read
TXN
Write
{Doc 1}
Reads do not lock a document from being modified
Only writes make write conflicts
@aly_cabral
TXN
Write
TXN
Write
{_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: “94th”
No-op writes are not writes
Only writes make write conflicts
Production Considerations
@aly_cabral
P
S
S
Writes are replicated once committed
@aly_cabral
… …
keys
K K K KVV V V
txn
time
data
next
Update Structure
txn
time
data
next
1) Cache pressure builds up by write volume after a snapshot
2) Transactions use the same snapshot throughout it’s duration
3) The update structure can only be cleaned up after the snapshot is evicted
WiredTiger Cache
@aly_cabral
How to Avoid WiredTiger Cache
Pressure:
transactionLifetimeLimitSeconds = default to 60s
Commit READ-ONLY Transactions
Abort Abandoned Transactions
<1000 Documents Modified, <16MB Oplog Entry
@aly_cabral
DDL Operations
createIndex()
createCollection()
dropDatabase()
namespace
Pending DDL
Intent Lock
Retrying Transactions
How and When to Use Multi-Document Distributed Transactions
How and When to Use Multi-Document Distributed Transactions
How and When to Use Multi-Document Distributed Transactions
How and When to Use Multi-Document Distributed Transactions
How and When to Use Multi-Document Distributed Transactions
1) All data modeling rules still apply
2) Transactions should not be the most common
operation (sanity check for rule 1)
3) Pass in the session to all statements
4) Implement retry logic, transactions can always
abort
5) Don’t unnecessarily leave snapshots open
6) To trigger write conflicts, make sure you’re
doing writes
7) Plan for DDL Operations
Thank You!

More Related Content

PPTX
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
PPTX
MongoDB.local DC 2018: How and When to Use Multi-Document Distributed Transac...
PPTX
How and When to Use Multi-Document Distributed Transactions
PPTX
MongoDB.local Austin 2018: How and When to Use Multi-Document Distributed Tr...
PPTX
How and When to Use Transactions - Aly Cabral
PDF
Sweet Web Animations API - Rodolfo Dias - Codemotion Rome 2018
PPTX
MongoDB.local Dallas 2019: How and When to Use Multi-Document Distributed Tra...
PPTX
MongoDB.local Seattle 2019: How and When to Use Multi-Document Distributed Tr...
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
MongoDB.local DC 2018: How and When to Use Multi-Document Distributed Transac...
How and When to Use Multi-Document Distributed Transactions
MongoDB.local Austin 2018: How and When to Use Multi-Document Distributed Tr...
How and When to Use Transactions - Aly Cabral
Sweet Web Animations API - Rodolfo Dias - Codemotion Rome 2018
MongoDB.local Dallas 2019: How and When to Use Multi-Document Distributed Tra...
MongoDB.local Seattle 2019: How and When to Use Multi-Document Distributed Tr...

Similar to How and When to Use Multi-Document Distributed Transactions (18)

PDF
MongoDB.local Berlin: How and when to use multi-document transactions
PPTX
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
PDF
To Transact or Not to Transact
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 Munich 2019: Distributed Transactions: With Great Power Comes ...
PDF
MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Com...
PDF
MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Com...
PPTX
Are Transactions Right For You? Using and Not Abusing Transactions
PDF
MongoDB World 2018: Building a New Transactional Model
PPTX
Mongo Nosql CRUD Operations
PPTX
Using Change Streams to Keep Up with Your Data
PPTX
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
PPTX
Concurrency Patterns with MongoDB
PPTX
Mongo db tips and advance features
KEY
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
PPTX
Using Change Streams to Keep Up with Your Data
PDF
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
MongoDB.local Berlin: How and when to use multi-document transactions
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
To Transact or Not to Transact
MongoDB. local Houston 2019: Distributed Transactions: With Great Power Comes...
MongoDB World 2019: Distributed Transactions: With Great Power Comes Great Re...
MongoDB .local Munich 2019: Distributed Transactions: With Great Power Comes ...
MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Com...
MongoDB .local Bengaluru 2019: Distributed Transactions: With Great Power Com...
Are Transactions Right For You? Using and Not Abusing Transactions
MongoDB World 2018: Building a New Transactional Model
Mongo Nosql CRUD Operations
Using Change Streams to Keep Up with Your Data
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
Concurrency Patterns with MongoDB
Mongo db tips and advance features
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Using Change Streams to Keep Up with Your Data
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
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: MongoDB Atlas Jumpstart
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...
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: MongoDB Atlas Jumpstart
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...
Ad

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
A Presentation on Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Empathic Computing: Creating Shared Understanding
PPTX
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
A Presentation on Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
NewMind AI Weekly Chronicles - August'25-Week II
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Empathic Computing: Creating Shared Understanding
sap open course for s4hana steps from ECC to s4

How and When to Use Multi-Document Distributed Transactions