SlideShare a Scribd company logo
Scalable Network I/O with C++
Arindam Mukherjee
Pune C++ and Boost Meetup
Content
• Sockets
• Boost Asio programming model
• Synchronous model
• Key challenges to scaling
• Asynchronous model
• Asio coroutines
Objectives
• Get a feel of the subject
• Know where to look, what to look for
Sockets
• Applications bind to an IP address and a
numeric port to start communicating.
• Port: an unsigned integer identifying the point
of attachment of the app to the protocol.
• Endpoint: The treble of an IP address, port
and protocol (TCP, UDP, etc.).
• Socket: an unsigned integer identifying an
endpoint in use.
Socket API
• Berkeley Sockets API, part of POSIX standard.
• System calls
– socket: get a socket
– bind: bind to an endpoint (usually passive)
– listen: wait for incoming connections (passive)
– accept: establish the TCP connection (passive)
– connect: connect to a remote endpoint (active)
– close: tear down the connection
Boost Asio Programming Model
• asio::io_service as a bridge to the OS, and as
a task queue.
• IO objects like asio::ip::tcp::socket, likewise
for udp, etc.
• I/O functions both sync and async, like
asio::read/write/asycn_read.
• Helper classes for name resolution, IP address
manipulation.
Name resolution
• asio::ip::tcp::resolver provides the resolve
method.
– Takes asio::ip::tcp::resolver::query object.
– Returns iterator of type
asio::ip::tcp::resolver::iterator.
– Dereferencing the iterator return
asio::ip::tcp::endpoint objects.
Vectored I/O
• Gather data from disparate areas in memory
and write it to a connection as a single stream.
• Read data from a single connection and
partition the stream across a set of memory
areas.
• Provided by readv / writev system calls –
requires lesser context switches or processing
overheads.
Asio Buffers for vectored I/O
• Wrappers around memory buffers, initialized
with buffer base address and length in bytes.
• asio::const_buffer – source,
asio::mutable_buffer – sink.
• Sequence of buffers can be vectors, lists, etc.
Sync I/O with TCP (server)
• Create a TCP listener socket.
• Bind to a local endpoint.
• Listen for and accept incoming connections.
• Perform I/O and handle events on each
connection, using a per-connection socket.
Sync I/O with TCP (client)
• Create a TCP socket (and optionally bind to an
endpoint).
• Connect to a remote endpoint (server).
• Perform communication and handle events on
the connection.
TCP sync I/O interaction model
Key challenges to scaling
• Sync APIs block waiting for completion.
• One thread per connection is for toy
programs: scalability anti-pattern.
• Use a thread-pool and leverage the io_service
task queue: only limits threads, not waiting.
• Go async: wait-free.
• Async + thread-pool: wait-free, limited
threads.
io_service as a task queue
• Put a handler with signature void() on the queue,
using post or dispatch.
• Handlers associated with tasks, could be ready or
pending.
• Call run or run_one from threads to process tasks.
• Call poll or poll_one to process ready tasks.
• Create object of asio::io_service::work to make
run wait.
• Serialize handlers using asio::io_service::strand.
Async I/O
• Submit tasks to the OS via the io_service, and
be notified by callbacks.
• Wait-free, but difficult to code.
• Maintaining context / state across callback
invocations presents challenges.
Async I/O model
Coroutines
• A coroutine can be called, suspended and
resumed.
• Suspension through cooperative yielding.
• On suspension, control goes back to
counterpart (caller).
• On resumption, coroutine does not restart but
continues from where it left off. Stack
preserved.
• Boost.Coroutine provides implementation.
Async I/O with coroutines
• Enqueue a coroutine using asio::spawn.
• Coroutine runs, but instead of blocking,
registers the yield context as a handler, and
then yields.
• Control goes to caller (loop in run function).
• The run function handles other I/O requests.
• On I/O events, handler is invoked which
resumes coroutine.
Coroutine Sequence diagram
• We’ll make this during the presentation.
Q & A
• Books:
Learning Boost C++ Libraries (Packt), Chapter 11.
• References:
http://www.gamedev.net/blog/950/entry-
2249317-a-guide-to-getting-started-with-
boostasio/
• Collaboration diagrams from here:
http://think-async.com/Asio/asio-
1.4.1/doc/asio/overview/core/basics.html
Howto lambda
• Syntax for generating function objects inline:
[] { std::cout << "Hello, world!n"; }
• Can take args:
[](std::string name)
{ std::cout << "Hello, " + name + "!n"; }
• Can access vars from surrounding scope:
[&var1, var2](…) {
std::cout << var2 << 'n';
var1 = 10;}
Howto bind
• Syntax for adapting function arity (signature).
void foo(int n, std::string name); // binary
auto f = boost::bind(foo, ::_1, "hello"); //
unary
• Binding references:
void foo(const std::string& name); // unary
std::string str;
auto f = boost::bind(foo, boost::ref(str));
// nullary
• Placeholders for unbound params: ::_1, ::_2.

More Related Content

What's hot

Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
ScyllaDB
 
gen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixirgen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixir
Tomáš Koutský
 
Rust programming-language
Rust programming-languageRust programming-language
Rust programming-language
Mujahid Malik Arain
 
Introduction to Khronos SYCL
Introduction to Khronos SYCLIntroduction to Khronos SYCL
Introduction to Khronos SYCL
Min-Yih Hsu
 
XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...
XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...
XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...
The Linux Foundation
 
From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern Compilers
Min-Yih Hsu
 
Lindsay distributed geventzmq
Lindsay distributed geventzmqLindsay distributed geventzmq
Lindsay distributed geventzmqRobin Xiao
 
Developing high-performance network servers in Lisp
Developing high-performance network servers in LispDeveloping high-performance network servers in Lisp
Developing high-performance network servers in Lisp
Vladimir Sedach
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Codemotion
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
Iskren Chernev
 
Introduction to protocol buffer
Introduction to protocol bufferIntroduction to protocol buffer
Introduction to protocol buffer
Tim (文昌)
 
Experience protocol buffer on android
Experience protocol buffer on androidExperience protocol buffer on android
Experience protocol buffer on android
Richard Chang
 
Vimeo and Open Source (SMPTE Forum 2015)
Vimeo and Open Source (SMPTE Forum 2015)Vimeo and Open Source (SMPTE Forum 2015)
Vimeo and Open Source (SMPTE Forum 2015)
Derek Buitenhuis
 
libcurl, seven SSL libraries and one SSH library
libcurl, seven SSL libraries and one SSH librarylibcurl, seven SSL libraries and one SSH library
libcurl, seven SSL libraries and one SSH library
Daniel Stenberg
 
play framework async with scala
play framework async with scalaplay framework async with scala
play framework async with scala
vuhaininh88
 
pacproxy - Ruby hiroba 2014
pacproxy - Ruby hiroba 2014pacproxy - Ruby hiroba 2014
pacproxy - Ruby hiroba 2014
Hiroshi Ota
 
Php under the_hood
Php under the_hoodPhp under the_hood
Php under the_hoodfrank_neff
 
The Future of C# and Visual Basic
The Future of C# and Visual BasicThe Future of C# and Visual Basic
Duplicity
DuplicityDuplicity
Duplicity
mig5
 
OSDC 2017 - Casey Callendrello -The evolution of the Container Network Interface
OSDC 2017 - Casey Callendrello -The evolution of the Container Network InterfaceOSDC 2017 - Casey Callendrello -The evolution of the Container Network Interface
OSDC 2017 - Casey Callendrello -The evolution of the Container Network Interface
NETWAYS
 

What's hot (20)

Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
 
gen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixirgen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixir
 
Rust programming-language
Rust programming-languageRust programming-language
Rust programming-language
 
Introduction to Khronos SYCL
Introduction to Khronos SYCLIntroduction to Khronos SYCL
Introduction to Khronos SYCL
 
XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...
XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...
XPDDS17: Keynote: Towards a Configurable and Slimmer x86 Hypervisor - Wei Liu...
 
From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern Compilers
 
Lindsay distributed geventzmq
Lindsay distributed geventzmqLindsay distributed geventzmq
Lindsay distributed geventzmq
 
Developing high-performance network servers in Lisp
Developing high-performance network servers in LispDeveloping high-performance network servers in Lisp
Developing high-performance network servers in Lisp
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
 
Introduction to protocol buffer
Introduction to protocol bufferIntroduction to protocol buffer
Introduction to protocol buffer
 
Experience protocol buffer on android
Experience protocol buffer on androidExperience protocol buffer on android
Experience protocol buffer on android
 
Vimeo and Open Source (SMPTE Forum 2015)
Vimeo and Open Source (SMPTE Forum 2015)Vimeo and Open Source (SMPTE Forum 2015)
Vimeo and Open Source (SMPTE Forum 2015)
 
libcurl, seven SSL libraries and one SSH library
libcurl, seven SSL libraries and one SSH librarylibcurl, seven SSL libraries and one SSH library
libcurl, seven SSL libraries and one SSH library
 
play framework async with scala
play framework async with scalaplay framework async with scala
play framework async with scala
 
pacproxy - Ruby hiroba 2014
pacproxy - Ruby hiroba 2014pacproxy - Ruby hiroba 2014
pacproxy - Ruby hiroba 2014
 
Php under the_hood
Php under the_hoodPhp under the_hood
Php under the_hood
 
The Future of C# and Visual Basic
The Future of C# and Visual BasicThe Future of C# and Visual Basic
The Future of C# and Visual Basic
 
Duplicity
DuplicityDuplicity
Duplicity
 
OSDC 2017 - Casey Callendrello -The evolution of the Container Network Interface
OSDC 2017 - Casey Callendrello -The evolution of the Container Network InterfaceOSDC 2017 - Casey Callendrello -The evolution of the Container Network Interface
OSDC 2017 - Casey Callendrello -The evolution of the Container Network Interface
 

Similar to C++ scalable network_io

High performance network programming on the jvm oscon 2012
High performance network programming on the jvm   oscon 2012 High performance network programming on the jvm   oscon 2012
High performance network programming on the jvm oscon 2012
Erik Onnen
 
Python, do you even async?
Python, do you even async?Python, do you even async?
Python, do you even async?
Saúl Ibarra Corretgé
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
Chetan Giridhar
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programming
Infinit
 
How do event loops work in Python?
How do event loops work in Python?How do event loops work in Python?
How do event loops work in Python?
Saúl Ibarra Corretgé
 
Hands on with CoAP and Californium
Hands on with CoAP and CaliforniumHands on with CoAP and Californium
Hands on with CoAP and Californium
Julien Vermillard
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
hawkowl
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Lyon Yang
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrency
Mosky Liu
 
Evented Ruby VS Node.js
Evented Ruby VS Node.jsEvented Ruby VS Node.js
Evented Ruby VS Node.js
Nitin Gupta
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and Scala
Max Alexejev
 
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecksKernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Anne Nicolas
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
Yardena Meymann
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
LiviaLiaoFontech
 
HPC Controls Future
HPC Controls FutureHPC Controls Future
HPC Controls Future
rcastain
 
NoSQL afternoon in Japan Kumofs & MessagePack
NoSQL afternoon in Japan Kumofs & MessagePackNoSQL afternoon in Japan Kumofs & MessagePack
NoSQL afternoon in Japan Kumofs & MessagePackSadayuki Furuhashi
 
NoSQL afternoon in Japan kumofs & MessagePack
NoSQL afternoon in Japan kumofs & MessagePackNoSQL afternoon in Japan kumofs & MessagePack
NoSQL afternoon in Japan kumofs & MessagePackSadayuki Furuhashi
 
SDAccel Design Contest: Vivado HLS
SDAccel Design Contest: Vivado HLSSDAccel Design Contest: Vivado HLS
SDAccel Design Contest: Vivado HLS
NECST Lab @ Politecnico di Milano
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
Panagiotis Kanavos
 

Similar to C++ scalable network_io (20)

High performance network programming on the jvm oscon 2012
High performance network programming on the jvm   oscon 2012 High performance network programming on the jvm   oscon 2012
High performance network programming on the jvm oscon 2012
 
Python, do you even async?
Python, do you even async?Python, do you even async?
Python, do you even async?
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programming
 
How do event loops work in Python?
How do event loops work in Python?How do event loops work in Python?
How do event loops work in Python?
 
Hands on with CoAP and Californium
Hands on with CoAP and CaliforniumHands on with CoAP and Californium
Hands on with CoAP and Californium
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrency
 
Evented Ruby VS Node.js
Evented Ruby VS Node.jsEvented Ruby VS Node.js
Evented Ruby VS Node.js
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and Scala
 
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecksKernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
 
HPC Controls Future
HPC Controls FutureHPC Controls Future
HPC Controls Future
 
NoSQL afternoon in Japan Kumofs & MessagePack
NoSQL afternoon in Japan Kumofs & MessagePackNoSQL afternoon in Japan Kumofs & MessagePack
NoSQL afternoon in Japan Kumofs & MessagePack
 
NoSQL afternoon in Japan kumofs & MessagePack
NoSQL afternoon in Japan kumofs & MessagePackNoSQL afternoon in Japan kumofs & MessagePack
NoSQL afternoon in Japan kumofs & MessagePack
 
SDAccel Design Contest: Vivado HLS
SDAccel Design Contest: Vivado HLSSDAccel Design Contest: Vivado HLS
SDAccel Design Contest: Vivado HLS
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
 

Recently uploaded

OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 

Recently uploaded (20)

OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 

C++ scalable network_io

  • 1. Scalable Network I/O with C++ Arindam Mukherjee Pune C++ and Boost Meetup
  • 2. Content • Sockets • Boost Asio programming model • Synchronous model • Key challenges to scaling • Asynchronous model • Asio coroutines
  • 3. Objectives • Get a feel of the subject • Know where to look, what to look for
  • 4. Sockets • Applications bind to an IP address and a numeric port to start communicating. • Port: an unsigned integer identifying the point of attachment of the app to the protocol. • Endpoint: The treble of an IP address, port and protocol (TCP, UDP, etc.). • Socket: an unsigned integer identifying an endpoint in use.
  • 5. Socket API • Berkeley Sockets API, part of POSIX standard. • System calls – socket: get a socket – bind: bind to an endpoint (usually passive) – listen: wait for incoming connections (passive) – accept: establish the TCP connection (passive) – connect: connect to a remote endpoint (active) – close: tear down the connection
  • 6. Boost Asio Programming Model • asio::io_service as a bridge to the OS, and as a task queue. • IO objects like asio::ip::tcp::socket, likewise for udp, etc. • I/O functions both sync and async, like asio::read/write/asycn_read. • Helper classes for name resolution, IP address manipulation.
  • 7. Name resolution • asio::ip::tcp::resolver provides the resolve method. – Takes asio::ip::tcp::resolver::query object. – Returns iterator of type asio::ip::tcp::resolver::iterator. – Dereferencing the iterator return asio::ip::tcp::endpoint objects.
  • 8. Vectored I/O • Gather data from disparate areas in memory and write it to a connection as a single stream. • Read data from a single connection and partition the stream across a set of memory areas. • Provided by readv / writev system calls – requires lesser context switches or processing overheads.
  • 9. Asio Buffers for vectored I/O • Wrappers around memory buffers, initialized with buffer base address and length in bytes. • asio::const_buffer – source, asio::mutable_buffer – sink. • Sequence of buffers can be vectors, lists, etc.
  • 10. Sync I/O with TCP (server) • Create a TCP listener socket. • Bind to a local endpoint. • Listen for and accept incoming connections. • Perform I/O and handle events on each connection, using a per-connection socket.
  • 11. Sync I/O with TCP (client) • Create a TCP socket (and optionally bind to an endpoint). • Connect to a remote endpoint (server). • Perform communication and handle events on the connection.
  • 12. TCP sync I/O interaction model
  • 13. Key challenges to scaling • Sync APIs block waiting for completion. • One thread per connection is for toy programs: scalability anti-pattern. • Use a thread-pool and leverage the io_service task queue: only limits threads, not waiting. • Go async: wait-free. • Async + thread-pool: wait-free, limited threads.
  • 14. io_service as a task queue • Put a handler with signature void() on the queue, using post or dispatch. • Handlers associated with tasks, could be ready or pending. • Call run or run_one from threads to process tasks. • Call poll or poll_one to process ready tasks. • Create object of asio::io_service::work to make run wait. • Serialize handlers using asio::io_service::strand.
  • 15. Async I/O • Submit tasks to the OS via the io_service, and be notified by callbacks. • Wait-free, but difficult to code. • Maintaining context / state across callback invocations presents challenges.
  • 17. Coroutines • A coroutine can be called, suspended and resumed. • Suspension through cooperative yielding. • On suspension, control goes back to counterpart (caller). • On resumption, coroutine does not restart but continues from where it left off. Stack preserved. • Boost.Coroutine provides implementation.
  • 18. Async I/O with coroutines • Enqueue a coroutine using asio::spawn. • Coroutine runs, but instead of blocking, registers the yield context as a handler, and then yields. • Control goes to caller (loop in run function). • The run function handles other I/O requests. • On I/O events, handler is invoked which resumes coroutine.
  • 19. Coroutine Sequence diagram • We’ll make this during the presentation.
  • 20. Q & A • Books: Learning Boost C++ Libraries (Packt), Chapter 11. • References: http://www.gamedev.net/blog/950/entry- 2249317-a-guide-to-getting-started-with- boostasio/ • Collaboration diagrams from here: http://think-async.com/Asio/asio- 1.4.1/doc/asio/overview/core/basics.html
  • 21. Howto lambda • Syntax for generating function objects inline: [] { std::cout << "Hello, world!n"; } • Can take args: [](std::string name) { std::cout << "Hello, " + name + "!n"; } • Can access vars from surrounding scope: [&var1, var2](…) { std::cout << var2 << 'n'; var1 = 10;}
  • 22. Howto bind • Syntax for adapting function arity (signature). void foo(int n, std::string name); // binary auto f = boost::bind(foo, ::_1, "hello"); // unary • Binding references: void foo(const std::string& name); // unary std::string str; auto f = boost::bind(foo, boost::ref(str)); // nullary • Placeholders for unbound params: ::_1, ::_2.