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
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
project resource management chapter-09.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Mushroom cultivation and it's methods.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Hybrid model detection and classification of lung cancer
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
NewMind AI Weekly Chronicles - August'25-Week II
Unlocking AI with Model Context Protocol (MCP)
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
Assigned Numbers - 2025 - Bluetooth® Document
project resource management chapter-09.pdf
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
cloud_computing_Infrastucture_as_cloud_p
Mushroom cultivation and it's methods.pdf
Group 1 Presentation -Planning and Decision Making .pptx
OMC Textile Division Presentation 2021.pptx
Hybrid model detection and classification of lung cancer
A comparative analysis of optical character recognition models for extracting...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Web App vs Mobile App What Should You Build First.pdf
DP Operators-handbook-extract for the Mautical Institute
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
MIND Revenue Release Quarter 2 2025 Press Release

MongoDB World 2016: Implementing Async Networking in MongoDB 3.2