SlideShare a Scribd company logo
1 of 33
.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

スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)
スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)
スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)昌桓 李
 
What Is React | ReactJS Tutorial for Beginners | ReactJS Training | Edureka
What Is React | ReactJS Tutorial for Beginners | ReactJS Training | EdurekaWhat Is React | ReactJS Tutorial for Beginners | ReactJS Training | Edureka
What Is React | ReactJS Tutorial for Beginners | ReactJS Training | EdurekaEdureka!
 
rsyncで差分バックアップしようぜ!
rsyncで差分バックアップしようぜ!rsyncで差分バックアップしようぜ!
rsyncで差分バックアップしようぜ!Hiro H.
 
ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計Tadayoshi Sato
 
Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017
Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017
Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017Cloudera Japan
 
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52Yahoo!デベロッパーネットワーク
 
Fontconfigことはじめ
FontconfigことはじめFontconfigことはじめ
FontconfigことはじめTakao Baba
 
ネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分けネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分けモノビット エンジン
 
Inkscape for web and UI mockups
Inkscape for web and UI mockupsInkscape for web and UI mockups
Inkscape for web and UI mockupsDonna Benjamin
 
足を地に着け落ち着いて考える
足を地に着け落ち着いて考える足を地に着け落ち着いて考える
足を地に着け落ち着いて考えるRyuji Tamagawa
 
Advanced WebLogic Monitoring: JMX and WLSDM Automation
Advanced WebLogic Monitoring: JMX and WLSDM AutomationAdvanced WebLogic Monitoring: JMX and WLSDM Automation
Advanced WebLogic Monitoring: JMX and WLSDM AutomationM. Fevzi Korkutata
 
Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)
Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)
Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)NTT DATA OSS Professional Services
 
Jbatch実践入門 #jdt2015
Jbatch実践入門 #jdt2015Jbatch実践入門 #jdt2015
Jbatch実践入門 #jdt2015Norito Agetsuma
 
5分でわかるクリーンアーキテクチャ
5分でわかるクリーンアーキテクチャ5分でわかるクリーンアーキテクチャ
5分でわかるクリーンアーキテクチャKenji Tanaka
 
ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)
ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)
ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)NTT DATA Technology & Innovation
 
Front-End Frameworks: a quick overview
Front-End Frameworks: a quick overviewFront-End Frameworks: a quick overview
Front-End Frameworks: a quick overviewDiacode
 
システム間連携を担うSpring Integrationのエンタープライズ開発での活用
システム間連携を担うSpring Integrationのエンタープライズ開発での活用システム間連携を担うSpring Integrationのエンタープライズ開発での活用
システム間連携を担うSpring Integrationのエンタープライズ開発での活用apkiban
 
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話JustSystems Corporation
 
HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019 #hc...
HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019  #hc...HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019  #hc...
HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019 #hc...Yahoo!デベロッパーネットワーク
 

What's hot (20)

スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)
スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)
スケールアップファーストのNoSQL、ScyllaDB(スキュラDB)
 
What Is React | ReactJS Tutorial for Beginners | ReactJS Training | Edureka
What Is React | ReactJS Tutorial for Beginners | ReactJS Training | EdurekaWhat Is React | ReactJS Tutorial for Beginners | ReactJS Training | Edureka
What Is React | ReactJS Tutorial for Beginners | ReactJS Training | Edureka
 
rsyncで差分バックアップしようぜ!
rsyncで差分バックアップしようぜ!rsyncで差分バックアップしようぜ!
rsyncで差分バックアップしようぜ!
 
MapReduce入門
MapReduce入門MapReduce入門
MapReduce入門
 
ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計ドメインロジックの実装方法とドメイン駆動設計
ドメインロジックの実装方法とドメイン駆動設計
 
Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017
Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017
Apache Kuduは何がそんなに「速い」DBなのか? #dbts2017
 
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52
 
Fontconfigことはじめ
FontconfigことはじめFontconfigことはじめ
Fontconfigことはじめ
 
ネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分けネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分け
 
Inkscape for web and UI mockups
Inkscape for web and UI mockupsInkscape for web and UI mockups
Inkscape for web and UI mockups
 
足を地に着け落ち着いて考える
足を地に着け落ち着いて考える足を地に着け落ち着いて考える
足を地に着け落ち着いて考える
 
Advanced WebLogic Monitoring: JMX and WLSDM Automation
Advanced WebLogic Monitoring: JMX and WLSDM AutomationAdvanced WebLogic Monitoring: JMX and WLSDM Automation
Advanced WebLogic Monitoring: JMX and WLSDM Automation
 
Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)
Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)
Apache Spark超入門 (Hadoop / Spark Conference Japan 2016 講演資料)
 
Jbatch実践入門 #jdt2015
Jbatch実践入門 #jdt2015Jbatch実践入門 #jdt2015
Jbatch実践入門 #jdt2015
 
5分でわかるクリーンアーキテクチャ
5分でわかるクリーンアーキテクチャ5分でわかるクリーンアーキテクチャ
5分でわかるクリーンアーキテクチャ
 
ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)
ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)
ストリーム処理におけるApache Avroの活用について(NTTデータ テクノロジーカンファレンス 2019 講演資料、2019/09/05)
 
Front-End Frameworks: a quick overview
Front-End Frameworks: a quick overviewFront-End Frameworks: a quick overview
Front-End Frameworks: a quick overview
 
システム間連携を担うSpring Integrationのエンタープライズ開発での活用
システム間連携を担うSpring Integrationのエンタープライズ開発での活用システム間連携を担うSpring Integrationのエンタープライズ開発での活用
システム間連携を担うSpring Integrationのエンタープライズ開発での活用
 
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
 
HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019 #hc...
HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019  #hc...HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019  #hc...
HDFSのスケーラビリティの限界を突破するためのさまざまな取り組み | Hadoop / Spark Conference Japan 2019 #hc...
 

Similar to Asynchronous programming - .NET Way

Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentationahmed 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 programmingPraveen Prajapati
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and awaitvfabro
 
High Performance Coding2.pptx
High Performance Coding2.pptxHigh Performance Coding2.pptx
High Performance Coding2.pptxShymmaaQadoom1
 
.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 2011rob_dimarco
 
Natural Laws of Software Performance
Natural Laws of Software PerformanceNatural Laws of Software Performance
Natural Laws of Software PerformanceGibraltar Software
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA PresentationRob Tweed
 
Achieving mass scale with Quasar Fibers
Achieving mass scale with Quasar FibersAchieving mass scale with Quasar Fibers
Achieving mass scale with Quasar FibersIdan Sheinberg
 
Rust's Journey to Async/await
Rust's Journey to Async/awaitRust's Journey to Async/await
Rust's Journey to Async/awaitC4Media
 
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 JavaLukas Steinbrecher
 
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 2011Mike 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.9Ilya Grigorik
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the ServerDoug Jones
 
Everyone loves PHP
Everyone loves PHPEveryone loves PHP
Everyone loves PHPAbhijit Das
 

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

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

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.