SlideShare a Scribd company logo
Implementing Async
Networking in MongoDB
Samantha Ritter
MongoDB Engineer
Why?
mongosapp
shards
connect auth send recv done
mongosapp
shards
connect auth send recv done
Execution engine
Standalone ASIO
http://think-async.com/
connect auth send recv done
{work
queue
B: send
B: recv
D: auth
A: send
F: done
C++11 lambdas
Constructs a closure: an
unnamed function object capable
of capturing variables in scope.
auto lambda = [capture list](params) {
// body
};
…
lambda(); // runs body
send
// the “send” task
void send_task(NetworkOp* op) {
// pass a lambda to async_send
async_send(op->socket,
op->command,
[op](error_code err) {
if (err) { return done(op); }
receive_task(op);
});
}
connect
auth
recv
send
done
mongos
Network Errors
connect
auth
recv
send
done
mongos
X
Network
Error!
Network errors are fine: they are
on the primary path of execution
The primary path controls
operation lifetime
// the “send” task
void send_task(NetworkOp* op) {
// pass a lambda to async_send
async_send(op->socket,
op->command,
[op](error_code err) {
if (err) { return done(op); }
receive_task(op);
});
}
{work
queue
B: send
B: recv
D: auth
A: send
F: done
B: recv
XNetwork
Error!
clean up B
Cancellations
connect
auth
recv
send
done
mongos
recv
!
Warning!
cancel
job
// the “send” task
void send_task(NetworkOp* op) {
// pass a lambda to async_send
async_send(op->socket,
op->command,
[op](error_code err) {
if (err) { return done(op); }
receive_task(op);
});
}
Cancellations are NOT fine: they are
on the secondary path of execution
On the secondary path we can’t
make assumptions about lifetime
Only the primary path can end
an operation
Rule of ownership:
// Basic “network operation” class
class NetworkOp {
bool cancelled;
};
// Primary path
if (op->cancelled) {
done(op);
}
// Secondary path
cancel(NetworkOp *op) {
op->cancelled = true;
}
// the “send” task
void send_task(NetworkOp* op) {
// pass a lambda to async_send
async_send(op->socket,
op->command,
[op](error_code err) {
if (err || op->cancelled)
return done(op);
receive_task(op);
});
}
connect
auth
recv
send
done
mongos
Please
cancel
yourself
Ok!
connect
auth
send
mongos
recvdone
Poof!
Please
cancel
your…
?!@!&?
// Secondary path
cancel(NetworkOp *op) {
// op could be a null pointer!
op->cancelled = true;
}
Operation access is protected
Rule of cooperation:
// “network operation” class
class NetworkOp {
bool cancelled;
};
// "access control" object
class SafeOp {
mutex lock;
NetworkOp* op;
};
shared_ptr
// Primary path
done(shared_ptr<SafeOp> safe) {
// lock before cleanup
safe->lock.lock();
// cleanup
safe->op = nullptr;
safe->lock.unlock();
}
// Secondary path
cancel(shared_ptr<SafeOp> safe) {
// once we lock, can’t change under us
safe->lock.lock();
if (safe->op) {
safe->op->cancelled = true;
}
safe->lock.unlock();
}
connect
auth
send
mongos
recvdone
Poof!
Please
cancel
your…
JK!!
Why?
Threading is better
Engineering Process
1. Iterate!
2. Use language
features where possible
3. Use external libraries
where appropriate
MongoDB World 2016: Implementing Async Networking in MongoDB 3.2
@SamWhoCodes
mongodb.com/careers
Thanks!
MongoDB World 2016: Implementing Async Networking in MongoDB 3.2

More Related Content

PDF
Chapter24 operator-overloading
PDF
Callbacks and control flow in Node js
PDF
Wrapping java in awesomeness aka condensator
PDF
Cocoa heads 09112017
PDF
Serverless and React
PDF
Extending Retrofit for fun and profit
PPTX
Avoiding Callback Hell with Async.js
PDF
DataEngConf SF16 - Routing Billions of Analytics Events with High Deliverability
Chapter24 operator-overloading
Callbacks and control flow in Node js
Wrapping java in awesomeness aka condensator
Cocoa heads 09112017
Serverless and React
Extending Retrofit for fun and profit
Avoiding Callback Hell with Async.js
DataEngConf SF16 - Routing Billions of Analytics Events with High Deliverability

What's hot (20)

PDF
Advanced functional programing in Swift
PDF
Monads in Swift
PDF
Finagle - an intro to rpc & a sync programming in jvm
PDF
RxJS - The Reactive Extensions for JavaScript
PDF
ReactiveCocoa Goodness - Part I of II
PDF
An Introduction to Reactive Cocoa
PDF
Reactive cocoa made Simple with Swift
PPTX
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
PDF
PDF
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
PDF
IoT Best practices
PPT
Asynchronous handlers in asp.net
PPTX
Mca 2nd sem u-4 operator overloading
PDF
Learn You a ReactiveCocoa for Great Good
PDF
Functional Reactive Programming in Clojurescript
PDF
How to send gzipped requests with boto3
PDF
Présentation de HomeKit
PPTX
Gearman & PHP
PPTX
Sharding and Load Balancing in Scala - Twitter's Finagle
PDF
ReactiveCocoa in Practice
Advanced functional programing in Swift
Monads in Swift
Finagle - an intro to rpc & a sync programming in jvm
RxJS - The Reactive Extensions for JavaScript
ReactiveCocoa Goodness - Part I of II
An Introduction to Reactive Cocoa
Reactive cocoa made Simple with Swift
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
IoT Best practices
Asynchronous handlers in asp.net
Mca 2nd sem u-4 operator overloading
Learn You a ReactiveCocoa for Great Good
Functional Reactive Programming in Clojurescript
How to send gzipped requests with boto3
Présentation de HomeKit
Gearman & PHP
Sharding and Load Balancing in Scala - Twitter's Finagle
ReactiveCocoa in Practice
Ad

Similar to MongoDB World 2016: Implementing Async Networking in MongoDB 3.2 (20)

ODP
Servlet 3.1 Async I/O
PDF
Asynchronous development in JavaScript
PDF
Writing Redis in Python with asyncio
PDF
Fabric Python Lib
PDF
How to Leverage Go for Your Networking Needs
PDF
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
PPT
JS everywhere 2011
PDF
Asynchronous programming done right - Node.js
PDF
Our challenge for Bulkload reliability improvement
PDF
Introducing Automorph - RPC library for Scala
PPT
Server side JavaScript: going all the way
PDF
Node.js - async for the rest of us.
PDF
maxbox starter72 multilanguage coding
PDF
Introduction to the New Asynchronous API in the .NET Driver
PDF
Serverless Architecture - A Gentle Overview
PDF
BUILDING APPS WITH ASYNCIO
PPTX
Ruby C10K: High Performance Networking - RubyKaigi '09
PDF
Tech Webinar: AUMENTARE LA SCALABILITÀ DELLE WEB APP CON SERVLET 3.1 ASYNC I/O
PDF
Fast and Reliable Swift APIs with gRPC
PPTX
JavaScript Multithread or Single Thread.pptx
Servlet 3.1 Async I/O
Asynchronous development in JavaScript
Writing Redis in Python with asyncio
Fabric Python Lib
How to Leverage Go for Your Networking Needs
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
JS everywhere 2011
Asynchronous programming done right - Node.js
Our challenge for Bulkload reliability improvement
Introducing Automorph - RPC library for Scala
Server side JavaScript: going all the way
Node.js - async for the rest of us.
maxbox starter72 multilanguage coding
Introduction to the New Asynchronous API in the .NET Driver
Serverless Architecture - A Gentle Overview
BUILDING APPS WITH ASYNCIO
Ruby C10K: High Performance Networking - RubyKaigi '09
Tech Webinar: AUMENTARE LA SCALABILITÀ DELLE WEB APP CON SERVLET 3.1 ASYNC I/O
Fast and Reliable Swift APIs with gRPC
JavaScript Multithread or Single Thread.pptx
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...

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
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
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Machine Learning_overview_presentation.pptx
Chapter 3 Spatial Domain Image Processing.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Assigned Numbers - 2025 - Bluetooth® Document
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Machine Learning_overview_presentation.pptx

MongoDB World 2016: Implementing Async Networking in MongoDB 3.2