SlideShare a Scribd company logo
How and When to Use
Multi-Document
Transactions
@aly_cabral
ALYSON CABRAL
Product Manager – Distributed
Systems, MongoDB
@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
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
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions
MongoDB World 2018: 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
MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions

More Related Content

PPTX
How and When to Use Multi-Document Distributed Transactions
PPTX
How and When to Use Multi-Document Distributed Transactions
PPTX
MongoDB.local DC 2018: How and When to Use Multi-Document Distributed Transac...
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 Seattle 2019: How and When to Use Multi-Document Distributed Tr...
PPTX
MongoDB.local Dallas 2019: How and When to Use Multi-Document Distributed Tra...
How and When to Use Multi-Document Distributed Transactions
How and When to Use Multi-Document Distributed Transactions
MongoDB.local DC 2018: How and When to Use Multi-Document Distributed Transac...
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 Seattle 2019: How and When to Use Multi-Document Distributed Tr...
MongoDB.local Dallas 2019: How and When to Use Multi-Document Distributed Tra...

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
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology
Chapter 3 Spatial Domain Image Processing.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25-Week II
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A comparative analysis of optical character recognition models for extracting...
Assigned Numbers - 2025 - Bluetooth® Document
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Ad

MongoDB World 2018: How and When to Use Multi-Document Distributed Transactions