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
MongoDB.local Austin 2018:  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
@aly_cabral
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
@aly_cabral
Write Conflicts
@aly_cabral
@aly_cabral
TXN
Write
TXN
Write
{Doc 1}
TXN write is unable to obtain a write lock!
TXN is ABORTED
rolledback entirely
@aly_cabral
@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
@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
@aly_cabral
TXN
Read
TXN
Write
{Doc 1}
Reads do not lock a document from being modified
Only writes make write conflicts
@aly_cabral
@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
@aly_cabral
Production Considerations
@aly_cabral
@aly_cabral
P
S
S
Writes are replicated once committed
@aly_cabral
@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
@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
@aly_cabral
DDL Operations
createIndex()
createCollection()
dropDatabase()
namespace
Pending DDL
Intent Lock
@aly_cabral
Retrying Transactions
@aly_cabral
@aly_cabral
@aly_cabral
@aly_cabral
@aly_cabral
@aly_cabral
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
@aly_cabral
Thank You!
@aly_cabral

More Related Content

PPTX
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
PPTX
How and When to Use Multi-Document Distributed Transactions
PPTX
How and When to Use Transactions - Aly Cabral
PDF
MongoDB.local Berlin: How and when to use multi-document transactions
PDF
To Transact or Not to Transact
PPTX
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
PDF
MongoDB. local Houston 2019: Distributed Transactions: With Great Power Comes...
PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
How and When to Use Multi-Document Distributed Transactions
How and When to Use Transactions - Aly Cabral
MongoDB.local Berlin: How and when to use multi-document transactions
To Transact or Not to Transact
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
MongoDB. local Houston 2019: Distributed Transactions: With Great Power Comes...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas

More from MongoDB (20)

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...
PDF
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB
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...
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Encapsulation_ Review paper, used for researhc scholars
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
Ad

MongoDB.local Austin 2018: How and When to Use Multi-Document Distributed Transactions