SlideShare a Scribd company logo
.NET WAY
ASYNCHRONY
I Am Bit Tricky
To Catch
 Trendy
 Examples: AJAX, AMD etc. and numerous language-
level/addon implementations
 Languages supporting async patterns:
C#:Tasks,
Java7-8: Future and CompletableFuture
Go: Routines and channels,
Erlang: Processes and spawn
Python: tasks, coroutines (asyncio)
JavaScript (ES2017-ES8): Promises
and list goes on.
ASYNCHRONY
I'll Call You Back
 Occurrence of events independent of the main
program flow
 Enables this “main program” continue its work, not
blocking to wait for event results
 Implicit degree of parallelism
BENEFITS
I Am Not Just
For UI Apps
1. Scalability and
2. Offloading (e.g. responsiveness, parallelism)
 Most client apps care about asynchrony for
“Offloading” reasons
 For store, desktop and phone apps: primary benefit is
responsiveness
 Most server apps care about asynchrony for
“Scalability” reasons
 Node.js, ASP.NET Core are inherently asynchronous,
hence scaling is their key.
Common Question: Does async have a place on the
server?
GOAL
I Am About To
Discussed More?
 History of Asynchronous APIs on .NET
 Thread,Threadpool and Task
 Conceptual overview of request handling on
ASP.NET
 I won’t be covering C# async-await API syntax
HISTORY
.Net And .Net
Languages have
Always Been
Loving Async
 APM (.NET1.1) : Socket class
 Begin<MethodName> and End<MethodName>
methods
 EAP (.NET2) : e.g. BackgroundWorker class
 <MethodName>Async methods
 Task and TPL (.NET4)
 Task-based Asynchronous Pattern (TAP) (.NET4.5)
 Async and await
 Language level support on C# and VB (2012), F#
(2010)
 TAP: Recommended asynchronous design pattern
THREAD
I Am An Action
Lady
 First appears in IBM OS/360 in 1967
 Its actual OS-level thread
 Gives life to a process
 Each process has at least one thread (Main thread)
 Threads share their address space
CLR THREAD
You should hate
new Thread()
 Freaking expensive: Memory and time overhead associated
with them
Thread =
(Thread Kernel Object) x86-700BYTE, x64-1240BYTE
+ (Thread Environment Block) x86-4KB, x64-8KB
+ (User Mode Stack) x86, x64-1MB
+ (Kernel Mode Stack) x86-12KB, x64-24KB
 CLR thread directly maps to Windows thread
 Highest degree of control that programmers don’t want 
 Only spun up new if heavy computations on multiple CPUs
needs to be done.
 Foreground and background threads
THREADPOOL
I manage threads
for you, sir!
 Thread pool is a set of threads available readily and
maintained by the CLR.
 No control but pool size: Submit work to execute,
wait for good to happen 
 Best for large no. of short operations where the
caller does not need the result.
CONTINUED…
 Threadpool size (no. of threads)
 Default minimum = No. of processors
 Default maximum = 5000 (.NET4.5)
 Thread Injection: Starvation avoidance and hill-
climbing algorithm
 Threads are added or removed every 500ms
 Thread pool categorizes its threads as
 Worker threads
 I/O completion port (IOCP) threads
IIS: REQUEST
HANDLING
Web Dev? You
Need To
Understand Me
 An app/web server
 Kernel mode and user mode (Native mode)
 App pool: Grouping of URLs that is routed to one or
more worker processes.
OLD SYNCHRONOUS
WAY
You Devs Love Me,
Don’t You?
 ASP.NET takes one of its thread pool threads and
assigns it to just arrived request
 Request handler call that external resource
synchronously and blocks it until result returns
BAD PART
All Busy,Try
Later
 Thread count saturation
 Available threads blocked and wasted
 New request have to wait and in danger of 503
ASYNCHRONOUS
WAY
Don’t Trust Me?
Try Then
 Async don’t waste precious threadpool threads
 Server could cope new request easily
 Smaller number of threads to handle a larger
number of requests.
THREADPOOL SIZE
Just Increase Me
And Forget Async
Altogether.
I Do Joke Too 
 Async does not replace the thread pool, rather
makes optimum use of it
 Scales both further and faster than blocking
threadpool threads
 Less memory pressure
 Can respond to sudden swings in request volume
Common question: What About the Thread Doing
the AsynchronousWork? There must something
monitoring at it, right?
No, not at all
TASKS
Some Call Me
FUTURES Others
PROMISES
 Best of both worlds
 TaskScheduler
 Thread Pool Task Scheduler
 Synchronization Context Task Scheduler
 Task Factories
 Task Continuation, Progress and Cancellation
 All newer high-level concurrency APIs are all built
on Task.
 Task<T> promises to return us a T saying:“not
right now honey, I'm kinda busy, why don't you
come back later? In fact, I will inform you when
I am done or you may cancel me any time you
want”
CPU-BOUND
TASK
I Love CPU,
Not You;
Leave Me
Alone
 Async Don’ts
 Too lightweight I/O (<30ms)
 CPU-Intensive Operations
 Historic problems with async:
 Asynchronous code is difficult
 Database backend is a bottleneck
 But today ( past few years )
 Bottleneck pushed back to app server
ASP.NET ASYNC
PATTERNS
Where Were
You?
As If You
Cared? Always
There, Just Bit
Shy
 In Asp.Net since very beginning
 AsynchronousWeb pages introduced in ASP.NET 2.0
 MVC got asynchronous controllers in ASP.NET MVC 2
 However, always been awkward to write and
difficult to maintain
 Now, the tables have turned
 In ASP.NET 4.5, async-await is savior
 More and more companies are embracing async
and await on ASP.NET.
ASYNC FLOW
I Yield
Control Back
To The Caller
NOT TO CONFUSE
WITH TPL
We Sound
Similar But
Inherently
Different
TPL…
TPL API…
ASYNC BEST
PRACTICES
Be Clever Else
You Are Busted
Old New Description
task.Wait await task Wait/await for a task to complete
task.Result await task Get the result of a completed task
Task.WaitAny await Task.WhenAny Wait/await for one of a collection of tasks to
complete
Task.WaitAll await Task.WhenAll Wait/await for every one of a collection of tasks to
complete
Thread.Sleep await Task.Delay Wait/await for a period of time
Task constructor Task.Run or TaskFactory.StartNew Create a code-based task
Problem Solution
Create a task to execute code
Task.Run or TaskFactory.StartNew (not the Task constructor
or Task.Start)
Create a task wrapper for an operation or event TaskFactory.FromAsync or TaskCompletionSource<T>
Support cancellation CancellationTokenSource and CancellationToken
Report progress IProgress<T> and Progress<T>
Handle streams of data TPL Dataflow or Reactive Extensions
Synchronize access to a shared resource SemaphoreSlim
Asynchronously initialize a resource AsyncLazy<T>
Async-ready producer/consumer structures TPL Dataflow or AsyncCollection<T>
REENTRANCY
Don’t Exploit Me
For God Sake
Else My Curse
Will Hurt You
 Reentering an asynchronous operation before it has
completed
 Prevent reentrancy or it can cause unexpected
results
 Disable subsequent invokes until its done
 Cancel and Restart operation
 Run multiple operations and Queue the output
FINE TUNING
Want Precision
And Flexibility
To Your Async
App? More APIs
For You
 CancellationToken, Task.WhenAll and
Task.WhenAny
 Start multiple tasks and await their completion by
monitoring a single task.
 Use cases:
 Cancel an Async Task or a List of Tasks
 Cancel Async Tasks after a Period of Time
 Cancel Remaining Async Tasks after One Is Complete
 Start Multiple Async Tasks and Process Them As They
Complete
 To understand why asynchronous requests scale, let’s trace a (simplified) example
of an asynchronous I/O call. Let’s say a request needs to write to a file.The request
thread calls the asynchronous write method.WriteAsync is implemented by the
Base Class Library (BCL), and uses completion ports for its asynchronous I/O. So,
the WriteAsync call is passed down to the OS as an asynchronous file write.The OS
then communicates with the driver stack, passing along the data to write in an I/O
request packet (IRP).
 This is where things get interesting: If a device driver can’t handle an IRP
immediately, it must handle it asynchronously. So, the driver tells the disk to start
writing and returns a “pending” response to the OS.The OS passes that “pending”
response to the BCL, and the BCL returns an incomplete task to the request-
handling code.The request-handling code awaits the task, which returns an
incomplete task from that method and so on. Finally, the request-handling code
ends up returning an incomplete task to ASP.NET, and the request thread is freed
to return to the thread pool.
 Now, consider the current state of the system.There are various I/O structures that
have been allocated (for example, the Task instances and the IRP), and they’re all
in a pending/incomplete state. However, there’s no thread that is blocked waiting
for that write operation to complete. Neither ASP.NET, nor the BCL, nor the OS, nor
the device driver has a thread dedicated to the asynchronous work.
 When the disk completes writing the data, it notifies its driver via an interrupt.The
driver informs the OS that the IRP has completed, and the OS notifies the BCL via
the completion port. A thread pool thread responds to that notification by
completing the task that was returned from WriteAsync(); this in turn resumes the
asynchronous request code.
 Yes, there were a few threads “borrowed” for very short amounts of time during
this completion-notification phase, but no thread was actually blocked while the
write was in progress.
 Above example is drastically simplified, but it gets across the primary point: no
thread is required for true asynchronous work. No CPU time is necessary to
actually push the bytes out.
 At the device driver level, all non-trivial I/O is asynchronous. Many developers
have a mental model that treats the “natural API” for I/O operations as
synchronous, with the asynchronous API as a layer built on it. However, that’s
completely backward: in fact, the natural API is asynchronous; and it’s the
synchronous APIs that are implemented using asynchronous I/O.

More Related Content

What's hot

Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
Filip Ekberg
 
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트) 마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
Amazon Web Services Korea
 
Apache Camel v3, Camel K and Camel Quarkus
Apache Camel v3, Camel K and Camel QuarkusApache Camel v3, Camel K and Camel Quarkus
Apache Camel v3, Camel K and Camel Quarkus
Claus Ibsen
 
[수정본] 우아한 객체지향
[수정본] 우아한 객체지향[수정본] 우아한 객체지향
[수정본] 우아한 객체지향
Young-Ho Cho
 
Hands On Introduction To Ansible Configuration Management With Ansible Comple...
Hands On Introduction To Ansible Configuration Management With Ansible Comple...Hands On Introduction To Ansible Configuration Management With Ansible Comple...
Hands On Introduction To Ansible Configuration Management With Ansible Comple...
SlideTeam
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
NarayanaMurthy Ganashree
 
AWS CDK introduction
AWS CDK introductionAWS CDK introduction
AWS CDK introduction
leo lapworth
 
코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우
Arawn Park
 
Spring boot Under Da Hood
Spring boot Under Da HoodSpring boot Under Da Hood
Spring boot Under Da Hood
Michel Schudel
 
AWS CodeCommit, CodeDeploy & CodePipeline
AWS CodeCommit, CodeDeploy & CodePipelineAWS CodeCommit, CodeDeploy & CodePipeline
AWS CodeCommit, CodeDeploy & CodePipeline
Julien SIMON
 
DevOps - CI/CD 알아보기
DevOps - CI/CD 알아보기DevOps - CI/CD 알아보기
DevOps - CI/CD 알아보기
SeungYong Baek
 
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
Arawn Park
 
클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA
클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA
클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA
VMware Tanzu Korea
 
AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021
AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021
AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021
AWSKRUG - AWS한국사용자모임
 
주니어의 쿠버네티스 생태계에서 살아남기
주니어의 쿠버네티스 생태계에서 살아남기주니어의 쿠버네티스 생태계에서 살아남기
주니어의 쿠버네티스 생태계에서 살아남기
InfraEngineer
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
Paulo Gandra de Sousa
 
IBM File Net P8
IBM File Net P8IBM File Net P8
IBM File Net P8
Mohammed El Rafie Tarabay
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
Mattia Battiston
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 

What's hot (20)

Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트) 마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
 
Apache Camel v3, Camel K and Camel Quarkus
Apache Camel v3, Camel K and Camel QuarkusApache Camel v3, Camel K and Camel Quarkus
Apache Camel v3, Camel K and Camel Quarkus
 
[수정본] 우아한 객체지향
[수정본] 우아한 객체지향[수정본] 우아한 객체지향
[수정본] 우아한 객체지향
 
Hands On Introduction To Ansible Configuration Management With Ansible Comple...
Hands On Introduction To Ansible Configuration Management With Ansible Comple...Hands On Introduction To Ansible Configuration Management With Ansible Comple...
Hands On Introduction To Ansible Configuration Management With Ansible Comple...
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
AWS CDK introduction
AWS CDK introductionAWS CDK introduction
AWS CDK introduction
 
Rich domain model
Rich domain modelRich domain model
Rich domain model
 
코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우
 
Spring boot Under Da Hood
Spring boot Under Da HoodSpring boot Under Da Hood
Spring boot Under Da Hood
 
AWS CodeCommit, CodeDeploy & CodePipeline
AWS CodeCommit, CodeDeploy & CodePipelineAWS CodeCommit, CodeDeploy & CodePipeline
AWS CodeCommit, CodeDeploy & CodePipeline
 
DevOps - CI/CD 알아보기
DevOps - CI/CD 알아보기DevOps - CI/CD 알아보기
DevOps - CI/CD 알아보기
 
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
 
클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA
클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA
클라우드 네이티브 IT를 위한 4가지 요소와 상관관계 - DevOps, CI/CD, Container, 그리고 MSA
 
AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021
AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021
AWS와 함께하는 무중단 배포 파이프라인 개선기 - 황성찬 (AUSG) :: AWS Community Day Online 2021
 
주니어의 쿠버네티스 생태계에서 살아남기
주니어의 쿠버네티스 생태계에서 살아남기주니어의 쿠버네티스 생태계에서 살아남기
주니어의 쿠버네티스 생태계에서 살아남기
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
IBM File Net P8
IBM File Net P8IBM File Net P8
IBM File Net P8
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
 

Similar to Asynchronous programming - .NET Way

Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
ahmed sayed
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
Praveen Prajapati
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
Betclic Everest Group Tech Team
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and await
vfabro
 
High Performance Coding2.pptx
High Performance Coding2.pptxHigh Performance Coding2.pptx
High Performance Coding2.pptx
ShymmaaQadoom1
 
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование....NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
NETFest
 
Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011
rob_dimarco
 
Natural Laws of Software Performance
Natural Laws of Software PerformanceNatural Laws of Software Performance
Natural Laws of Software Performance
Gibraltar Software
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
Rob Tweed
 
Achieving mass scale with Quasar Fibers
Achieving mass scale with Quasar FibersAchieving mass scale with Quasar Fibers
Achieving mass scale with Quasar Fibers
Idan Sheinberg
 
Proposal
ProposalProposal
Rust's Journey to Async/await
Rust's Journey to Async/awaitRust's Journey to Async/await
Rust's Journey to Async/await
C4Media
 
A first look into the Project Loom in Java
A first look into the Project Loom in JavaA first look into the Project Loom in Java
A first look into the Project Loom in Java
Lukas Steinbrecher
 
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
Mohammad Hossein Karami
 
MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011
Mike Willbanks
 
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
Ilya Grigorik
 
Asynchronyin net
Asynchronyin netAsynchronyin net
Asynchronyin net
Soacat Blogspot
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
Doug Jones
 
Everyone loves PHP
Everyone loves PHPEveryone loves PHP
Everyone loves PHP
Abhijit Das
 
Async programming in c#
Async programming in c#Async programming in c#
Async programming in c#
Ahasanul Kalam Akib
 

Similar to Asynchronous programming - .NET Way (20)

Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and await
 
High Performance Coding2.pptx
High Performance Coding2.pptxHigh Performance Coding2.pptx
High Performance Coding2.pptx
 
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование....NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
 
Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011
 
Natural Laws of Software Performance
Natural Laws of Software PerformanceNatural Laws of Software Performance
Natural Laws of Software Performance
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
 
Achieving mass scale with Quasar Fibers
Achieving mass scale with Quasar FibersAchieving mass scale with Quasar Fibers
Achieving mass scale with Quasar Fibers
 
Proposal
ProposalProposal
Proposal
 
Rust's Journey to Async/await
Rust's Journey to Async/awaitRust's Journey to Async/await
Rust's Journey to Async/await
 
A first look into the Project Loom in Java
A first look into the Project Loom in JavaA first look into the Project Loom in Java
A first look into the Project Loom in Java
 
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
 
MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011
 
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
 
Asynchronyin net
Asynchronyin netAsynchronyin net
Asynchronyin net
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
 
Everyone loves PHP
Everyone loves PHPEveryone loves PHP
Everyone loves PHP
 
Async programming in c#
Async programming in c#Async programming in c#
Async programming in c#
 

Recently uploaded

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 

Recently uploaded (20)

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 

Asynchronous programming - .NET Way

  • 2. ASYNCHRONY I Am Bit Tricky To Catch  Trendy  Examples: AJAX, AMD etc. and numerous language- level/addon implementations  Languages supporting async patterns: C#:Tasks, Java7-8: Future and CompletableFuture Go: Routines and channels, Erlang: Processes and spawn Python: tasks, coroutines (asyncio) JavaScript (ES2017-ES8): Promises and list goes on.
  • 3. ASYNCHRONY I'll Call You Back  Occurrence of events independent of the main program flow  Enables this “main program” continue its work, not blocking to wait for event results  Implicit degree of parallelism
  • 4. BENEFITS I Am Not Just For UI Apps 1. Scalability and 2. Offloading (e.g. responsiveness, parallelism)  Most client apps care about asynchrony for “Offloading” reasons  For store, desktop and phone apps: primary benefit is responsiveness  Most server apps care about asynchrony for “Scalability” reasons  Node.js, ASP.NET Core are inherently asynchronous, hence scaling is their key. Common Question: Does async have a place on the server?
  • 5. GOAL I Am About To Discussed More?  History of Asynchronous APIs on .NET  Thread,Threadpool and Task  Conceptual overview of request handling on ASP.NET  I won’t be covering C# async-await API syntax
  • 6. HISTORY .Net And .Net Languages have Always Been Loving Async  APM (.NET1.1) : Socket class  Begin<MethodName> and End<MethodName> methods  EAP (.NET2) : e.g. BackgroundWorker class  <MethodName>Async methods  Task and TPL (.NET4)  Task-based Asynchronous Pattern (TAP) (.NET4.5)  Async and await  Language level support on C# and VB (2012), F# (2010)  TAP: Recommended asynchronous design pattern
  • 7. THREAD I Am An Action Lady  First appears in IBM OS/360 in 1967  Its actual OS-level thread  Gives life to a process  Each process has at least one thread (Main thread)  Threads share their address space
  • 8. CLR THREAD You should hate new Thread()  Freaking expensive: Memory and time overhead associated with them Thread = (Thread Kernel Object) x86-700BYTE, x64-1240BYTE + (Thread Environment Block) x86-4KB, x64-8KB + (User Mode Stack) x86, x64-1MB + (Kernel Mode Stack) x86-12KB, x64-24KB  CLR thread directly maps to Windows thread  Highest degree of control that programmers don’t want   Only spun up new if heavy computations on multiple CPUs needs to be done.  Foreground and background threads
  • 9. THREADPOOL I manage threads for you, sir!  Thread pool is a set of threads available readily and maintained by the CLR.  No control but pool size: Submit work to execute, wait for good to happen   Best for large no. of short operations where the caller does not need the result.
  • 10. CONTINUED…  Threadpool size (no. of threads)  Default minimum = No. of processors  Default maximum = 5000 (.NET4.5)  Thread Injection: Starvation avoidance and hill- climbing algorithm  Threads are added or removed every 500ms  Thread pool categorizes its threads as  Worker threads  I/O completion port (IOCP) threads
  • 11. IIS: REQUEST HANDLING Web Dev? You Need To Understand Me  An app/web server  Kernel mode and user mode (Native mode)  App pool: Grouping of URLs that is routed to one or more worker processes.
  • 12. OLD SYNCHRONOUS WAY You Devs Love Me, Don’t You?  ASP.NET takes one of its thread pool threads and assigns it to just arrived request  Request handler call that external resource synchronously and blocks it until result returns
  • 13. BAD PART All Busy,Try Later  Thread count saturation  Available threads blocked and wasted  New request have to wait and in danger of 503
  • 14. ASYNCHRONOUS WAY Don’t Trust Me? Try Then  Async don’t waste precious threadpool threads  Server could cope new request easily  Smaller number of threads to handle a larger number of requests.
  • 15. THREADPOOL SIZE Just Increase Me And Forget Async Altogether. I Do Joke Too   Async does not replace the thread pool, rather makes optimum use of it  Scales both further and faster than blocking threadpool threads  Less memory pressure  Can respond to sudden swings in request volume Common question: What About the Thread Doing the AsynchronousWork? There must something monitoring at it, right? No, not at all
  • 16. TASKS Some Call Me FUTURES Others PROMISES  Best of both worlds  TaskScheduler  Thread Pool Task Scheduler  Synchronization Context Task Scheduler  Task Factories  Task Continuation, Progress and Cancellation  All newer high-level concurrency APIs are all built on Task.  Task<T> promises to return us a T saying:“not right now honey, I'm kinda busy, why don't you come back later? In fact, I will inform you when I am done or you may cancel me any time you want”
  • 17. CPU-BOUND TASK I Love CPU, Not You; Leave Me Alone  Async Don’ts  Too lightweight I/O (<30ms)  CPU-Intensive Operations  Historic problems with async:  Asynchronous code is difficult  Database backend is a bottleneck  But today ( past few years )  Bottleneck pushed back to app server
  • 18. ASP.NET ASYNC PATTERNS Where Were You? As If You Cared? Always There, Just Bit Shy  In Asp.Net since very beginning  AsynchronousWeb pages introduced in ASP.NET 2.0  MVC got asynchronous controllers in ASP.NET MVC 2  However, always been awkward to write and difficult to maintain  Now, the tables have turned  In ASP.NET 4.5, async-await is savior  More and more companies are embracing async and await on ASP.NET.
  • 19. ASYNC FLOW I Yield Control Back To The Caller
  • 20. NOT TO CONFUSE WITH TPL We Sound Similar But Inherently Different
  • 23.
  • 24. ASYNC BEST PRACTICES Be Clever Else You Are Busted
  • 25. Old New Description task.Wait await task Wait/await for a task to complete task.Result await task Get the result of a completed task Task.WaitAny await Task.WhenAny Wait/await for one of a collection of tasks to complete Task.WaitAll await Task.WhenAll Wait/await for every one of a collection of tasks to complete Thread.Sleep await Task.Delay Wait/await for a period of time Task constructor Task.Run or TaskFactory.StartNew Create a code-based task
  • 26. Problem Solution Create a task to execute code Task.Run or TaskFactory.StartNew (not the Task constructor or Task.Start) Create a task wrapper for an operation or event TaskFactory.FromAsync or TaskCompletionSource<T> Support cancellation CancellationTokenSource and CancellationToken Report progress IProgress<T> and Progress<T> Handle streams of data TPL Dataflow or Reactive Extensions Synchronize access to a shared resource SemaphoreSlim Asynchronously initialize a resource AsyncLazy<T> Async-ready producer/consumer structures TPL Dataflow or AsyncCollection<T>
  • 27. REENTRANCY Don’t Exploit Me For God Sake Else My Curse Will Hurt You  Reentering an asynchronous operation before it has completed  Prevent reentrancy or it can cause unexpected results  Disable subsequent invokes until its done  Cancel and Restart operation  Run multiple operations and Queue the output
  • 28. FINE TUNING Want Precision And Flexibility To Your Async App? More APIs For You  CancellationToken, Task.WhenAll and Task.WhenAny  Start multiple tasks and await their completion by monitoring a single task.  Use cases:  Cancel an Async Task or a List of Tasks  Cancel Async Tasks after a Period of Time  Cancel Remaining Async Tasks after One Is Complete  Start Multiple Async Tasks and Process Them As They Complete
  • 29.
  • 30.
  • 31.  To understand why asynchronous requests scale, let’s trace a (simplified) example of an asynchronous I/O call. Let’s say a request needs to write to a file.The request thread calls the asynchronous write method.WriteAsync is implemented by the Base Class Library (BCL), and uses completion ports for its asynchronous I/O. So, the WriteAsync call is passed down to the OS as an asynchronous file write.The OS then communicates with the driver stack, passing along the data to write in an I/O request packet (IRP).  This is where things get interesting: If a device driver can’t handle an IRP immediately, it must handle it asynchronously. So, the driver tells the disk to start writing and returns a “pending” response to the OS.The OS passes that “pending” response to the BCL, and the BCL returns an incomplete task to the request- handling code.The request-handling code awaits the task, which returns an incomplete task from that method and so on. Finally, the request-handling code ends up returning an incomplete task to ASP.NET, and the request thread is freed to return to the thread pool.
  • 32.  Now, consider the current state of the system.There are various I/O structures that have been allocated (for example, the Task instances and the IRP), and they’re all in a pending/incomplete state. However, there’s no thread that is blocked waiting for that write operation to complete. Neither ASP.NET, nor the BCL, nor the OS, nor the device driver has a thread dedicated to the asynchronous work.  When the disk completes writing the data, it notifies its driver via an interrupt.The driver informs the OS that the IRP has completed, and the OS notifies the BCL via the completion port. A thread pool thread responds to that notification by completing the task that was returned from WriteAsync(); this in turn resumes the asynchronous request code.  Yes, there were a few threads “borrowed” for very short amounts of time during this completion-notification phase, but no thread was actually blocked while the write was in progress.
  • 33.  Above example is drastically simplified, but it gets across the primary point: no thread is required for true asynchronous work. No CPU time is necessary to actually push the bytes out.  At the device driver level, all non-trivial I/O is asynchronous. Many developers have a mental model that treats the “natural API” for I/O operations as synchronous, with the asynchronous API as a layer built on it. However, that’s completely backward: in fact, the natural API is asynchronous; and it’s the synchronous APIs that are implemented using asynchronous I/O.

Editor's Notes

  1. AMD: Asynchronous Method Dispatch Trendy subject: Largest paradigm shift - Sequential to asynchronous programming
  2. If a developer needs to achieve better scalability, they can use any async APIs exposed, and they don’t have to pay additional overhead for invoking a faux async API.  If a developer needs to achieve responsiveness or parallelism with synchronous APIs, they can simply wrap the invocation with a method like Task.Run Scalability benefits is achieved by modifying the actual implementation, whereas offloading can be achieved by just wrapping sync implementations
  3. TAP: System.Threading.Tasks (Task and Task<TResult>)
  4. -In most cases the thread pool will perform better with its own algorithm for allocating threads. -every process has at least one thread in it Thread Kernel Object: The OS allocates these data structure to each of the thread created which contains a set of CPU registers. This also contains threads context and consumes space of 700 bytes in X86 and 1240 bytes in X64 bit machines. Thread Environment Block (TEB): This is the block of memory allocated and initialized in user mode . This consumes 4kb in X84 and 8kb in X64. It helps in storing threads local storage data as well as data structures used in GDI and OpenGL graphics. User Mode Stack: The user mode stack is used for local variables and arguments passed to methods. It also contains the next statement to be executes when thread returns from method. By default Windows allocates 1MB of memory. Kernel Mode Stack: It is used when application code passes arguments to Kernel mode function in the OS. This is used mainly for security reasons i.e. when Windows copies any data passed from User mode stack to Kernel mode. Post this process Windows validates the data and operates on them. It consumes 12kb in X86 and 24kb in X64 bit machines.
  5. Worker threads: Async operations as accessing file system, database, services etc. I/O completion port (IOCP) threads: Used to notify when async operations are completed Starvation avoidance, the .Net thread pool continues to add worker threads if there is no visible progress on the queued items. In the latter case, the .Net thread pool tries to maximize the throughput using as few threads as possible. Hence, if your system has four cores, you would have four worker threads and four IOCP threads by default. 
  6. Worker threads: Async operations as accessing file system, database, services etc. I/O completion port (IOCP) threads: Used to notify when async operations are completed Strategies: Starvation avoidance, the .Net thread pool continues to add worker threads if there is no visible progress on the queued items. In the latter case, the .Net thread pool tries to maximize the throughput using as few threads as possible. Hence, if your system has four cores, you would have four worker threads and four IOCP threads by default. 
  7.  W3WP process which ultimately responds to the request. App pool: application pool is a way to create compartments in a web server through process boundaries, and route sets of URLs to each of these compartments.
  8. This is all well and good—until your ASP.NET server gets more requests than it has threads to handle. At this point, the extra requests have to wait for a thread to be available before they can run. Those threads are just blocked waiting for an external call to return. They’re not in a running state and are not given any CPU time. Those threads are just being wasted while there’s a request in need. This is the situation addressed by asynchronous requests. The third request is already in the system. Its timer is going, and it’s in danger of an HTTP Error 503 (Service unavailable).
  9. All green
  10. Asynchronous code does not replace the thread pool, rather makes optimum use of it Thread pool has a limited injection rate, a thread per 0.5 second What About the Thread Doing the Asynchronous Work? Async code frees up the request thread, but only at the expense of another thread elsewhere in the system, right?
  11. All newer high-level concurrency APIs, including the Parallel.For*() methods, PLINQ, C# 5 await, and modern async methods in the BCL, are all built on Task.
  12. Not A Silver Bullet. Well, Nothing in this world Is Except silver bullet itself. Async and await is all about I/O: Excel at reading and writing files, database records, and REST APIs Two valid arguments (thus more developer time compared to just purchasing larger servers); and second Scaling the app server makes little sense if the database back end is the bottleneck Modern back ends such as Microsoft Azure SQL Database, NoSQL etc. scale much further, pushing the bottleneck back to the Web server
  13. Many companies decided it was easier all around to just develop the code synchronously and pay for larger server farms or more expensive hosting in ASP.NET 4.5, asynchronous code using async and await is almost as easy as writing synchronous code. As large systems move into cloud hosting and demand more scaling, more and more companies are embracing async and await on ASP.NET.
  14. TPL – Effort on Natural Parallelism Make Your Code Span Across CPU Cores
  15. WhenAny returns a task that completes when any task in a collection is complete. WhenAll returns a task that completes when all tasks in a collection are complete. Use cases: Cancel an Async Task or a List of Tasks (C#). Cancel Async Tasks after a Period of Time (C#) Cancel Remaining Async Tasks after One Is Complete (C#) Start Multiple Async Tasks and Process Them As They Complete (C#)
  16. I/O request packets (IRPs) are kernel mode structures that are used by Windows Driver Model (WDM) and Windows NT device drivers to communicate with each other and with the operating system. They are data structures that describe I/O requests, and can be equally well thought of as "I/O request descriptors" or similar.