SlideShare a Scribd company logo
Parallel and Asynchronous Programming
The server-side story
Panagiotis Kanavos
Parallel Extensions
• Best kept .NET secret known by all
• Since 2010 and .NET 4.0
• Coves Task, Data based processing models
• Close cousins
• TPL Dataflow
• System.Reactive
• Available through NuGet
• SIMD through RyuJIT
Use on the desktop
• async/await for asynchronous events
• Task.Run for background Processing
• Parallel computation
• Priorities
• Avoid blocking the UI
• Reduce execution time
• Use all available cores
What’s different on the Server
• No UI thread
• Multiple concurrent users/requests
• Throughput/Scalability more important than
request durattion
• Then there are timeouts
• Lots of services, lots of combined timeouts
• And …
Server Meltdown
Server Meltdown
• Load Balanced farm
• High load
• First server stuck at 100% CPU
• Timeouts, App pool Recycle
• More requests redirected to other servers
• Second server reaches 100%
• Don’t drop the ball!
• …
Request Queuing in ASP.NET
IIS Queue
• Req 1
• Req 2
• Req 3
ASP.NET Worker
Threads
• Thread 1
• Thread 3
App Domain
• Thread 2
Why …
• Lots of threads  Worse Scalability
• Can’t run more threads than cores
• Thrashing
• When everyone awaits everyone else, no-one finishes
• HATES blocking calls!
• Thread Pool starvation  Can’t serve
• 500 Too Busy
• Immediate actio
• App Pool Recycle
• Only possible action really …
Asynchronous Execution
• More important for server than speed
• Stackless Python  No blocking calls
• A view of the real world
• No blocking I/O at the OS level
• I/O = File, Database, WS/REST calls
• On IIS – IO Threads
• Since the IO Thread is doing the job, why am I
waiting for it?
Async on WebForms
• Basic support through async event handlers
• async void Button_Click μπρρρ…
• <%@ Page Async=“true” %>
• Use Page.RegisterAsyncTask for jobs longer than a
single request
• Raw Background threads can be killed at any time
Async on MVC/Web API
• Async actions!
• Plays well with Database/EF async
• HostingEnvironment.QueueBackgroundWorkItem
• Notifies IIS
• Gives 90 sec to task to finish in case of shutdown/recycle
• HangFire, Azure Web Jobs for repeating tasks
Parallel.For and PLINQ
• Data Parallelism
• Cuts the data in chunks
• Task per chunk
• Generally, 1 Task/Core
• Also uses the calling thread
• Looks like blocking (it’s not)
• Not a concern on the server
Dataflow
DB call
• I:Date
• O:Sales
Web call
• I:Sale
• O:Details
• X3
• Bounded
Analyze Store
• Bulk
Insert to
DB
Dataflow – Old Friend
The basics
• Processing models
• Agent and Pipeline
• and MapReduce data processing
• One task per block (configurable)
• Input/Output Buffer
• Block Types
• Buffering
• Execution
• Grouping
• Links between blocks
Execution Blocks
• Action Block
• Classic Job Queue
• Rudimentary Agent implementation
• Transform Block
• Transform one Input item to one Output
• Call a web service for a Stock to get History as a package
• TransformMany Block
• For one input item multiple output items
• Parallel Execution
Throttling
• BoundedCapacity
• Maximum input size
• Blocks sources
• MaxMessagesPerTask
• Recycle a Task after X messages
• CPU Throttling
Execution Block Demos
Buffering Blocks
• Buffer Block
• Easy Publisher/Subscriber implementation
• Broadcast Block
• Last event sent to many subscribers
• WriteOnce block
Batch Block Demo
Grouping Blocks
• Batching Block
• Join Block
• 2-3 Inputs, 1 Tuple<T1,T2,T3> output
• BatchedJoin Block
• Batch  Join
• T1, T2  Tuple< IList<T1>, IList<T2>>
Grouping Block Demos
Links
• LinkTo  IDisposable
• Disconnect  Dispose
• Settings
• Filter messages through Lambda
• Propagate Completion
Custom combination with Encapsulate
• New block
• One Input, One Output
Error Handling
In case of error a block
• Discards input messages
• Refuses new messages
• Propagate exception
• Caught with await/.Wait()
Dataflow Demos
Reactive Extensions
• Event monitoring and processing
• Control Events
• Records in Event Log
• Calls to a Web Service
• Time-based events
• Handle as a stream
• Works like LINQ
• Foundation for Event Processing
Sources
• Anything implementing IObservable<>
• Time
• Observable.Interval
• Events
• Observable.FromEventPattern
• BeginXXX/EndXXX
• Observable.FromAsyncPattern
• State machines / “Loops”
• Observable.Generate
• Tasks
• Every IEnumerable<T>
• Subject<T>  mySubject.OnNext(data)
Reactive Demos
Useful Functions
• Interval
• Buffer
• Sample
• Throttle
• Window
• Skip
• Take
• TakeUntil
Transformations
• Anything goes
• task ... AsObservable()
• block … AsObservable()
• await Observable
• ToTask<T>
SignalR
• Real Time communication from Server to Client
• Push Notifications
• Long process progress
• Eg. Search for tickets
Multiple Techniques
• Web sockets, falling back to …
• Server Sent Events
• Forever Frame
• Ajax Long Polling
Call function on Browser
• Server-side:
Clients.All.DoSomething(data)
• Browser-side
hub.client.updateProgress = function (data)
Connections and Hubs
Hub
Browser
Browser
Client
application
Browser
Persistent
Connection
Connections and Hubs
Scaleout through Backplane
Supported backplanes
• Windows Azure Bus
• SQL Server
• Redis
• Recommended Distributed memory cache for new Azure
projects
• Open Source Rulez!
SignalR Demos
Useful links- Courses
MVA Course
• Lighting up Real-Time Web Communications with SignalR
Pluralshight Course
• Async and Parallel Programming: Application Design
Books
• The C# Concurrency Cookbook, Stephen Cleary, O’Reilly
Useful Links - Sites
Sites
• Signal R
• TPL Dataflow
• Reactive Extensions
• Using Asynchronous Methods in ASP.NET
• 101 Rx Samples
Blogs
• Stephen Cleary
• The Magic of using Acync in ASP.NET, Scott Hanselman
• How to run background tasks in ASP.NET, Scott Hanselman
Session Evaluation
Your feedback is
important and valuable
Submit before the event’s
close session to WIN
prizes
2ways
to access
Go to
m.itprodevconnections.gr
Ask for an Evaluation Sheet from
the registration desk
Microsoft Virtual Academy for IT Pros
Όλα τα trainings που χρειάζεσαι δωρεάν σε ένα site!
/virtualization/
/server-infrastructure/
/private-cloud/
/hybrid-cloud/
/desktop-devices-management/
/licensing/
/και πολλά άλλα…
www.microsoftvirtualacademy.com
MVA
Microsoft Virtual Academy LIVE
Όλα τα trainings που χρειάζεσαι δωρεάν σε ένα site!
www.microsoftvirtualacademy.com
4 ΜΕΡΕΣ Εκπαίδευση με το Mark Russinovich
ΔΩΡΕΑΝ ΠΡΟΕΤΟΙΜΑΣΙΑ για την Εξέταση 70-533: Implementing Azure Infrastructure Solutions
Και 50% ΕΚΠΤΩΤΙΚΟ VOUCHER ΣΤΟ ΚΟΣΤΟΣ ΤΗΣ ΕΞΕΤΑΣΗΣ
1 ως 4 Δεκεμβρίου
MVA
Microsoft Virtual Academy for Devs
Όλα τα trainings που χρειάζεσαι δωρεάν σε ένα site!
/game development /
/web development /
/cloud development /
/mobile development /
/C#-XAML-HTML/
/app development/
/visual studio και πολλά άλλα…
www.microsoftvirtualacademy.com
MVA
The server side story:  Parallel and Asynchronous programming in .NET - ITProDevConnections 2014

More Related Content

PPTX
Asynchronous programming in ASP.NET
PPTX
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
PPTX
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
PPTX
Debugging your Way through .NET with Visual Studio 2015
PDF
NDev Talk - Serverless Design Patterns
PDF
Oracle 12c Parallel Execution New Features
PPTX
Strata+Hadoop 2015 NYC End User Panel on Real-Time Data Analytics
PDF
Migrating to aws
Asynchronous programming in ASP.NET
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
Debugging your Way through .NET with Visual Studio 2015
NDev Talk - Serverless Design Patterns
Oracle 12c Parallel Execution New Features
Strata+Hadoop 2015 NYC End User Panel on Real-Time Data Analytics
Migrating to aws

What's hot (20)

PDF
Building & Testing Scalable Rails Applications
PPTX
From VMs to Containers: Introducing Docker Containers for Linux and Windows S...
PPTX
MVC 6 - the new unified Web programming model
PPTX
Getting Started with Web Services
PPTX
A (XPages) developers guide to Cloudant - MeetIT
PPTX
4th Lecture: JSP and such
PPTX
Microsoft ASP.NET 5 - The new kid on the block
PPT
Real World Rails Deployment
PPTX
Tiki.vn - How we scale as a tech startup
PPTX
Learn AJAX at ASIT
PPT
Zarafa SummerCamp 2012 - Steve Hardy Friday Keynote
PDF
CFWheels - Pragmatic, Beautiful Code
PDF
DEV03 - How Watson, Bluemix, Cloudant, and XPages Can Work Together In A Real...
PPT
Four Ways to Improve ASP .NET Performance and Scalability
PPTX
10 performance and scalability secrets of ASP.NET websites
PDF
Net coreandsimplcommerce
PDF
Performance Testing w/ WebPage Test Private Instance (DrupalCamp Ohio)
PPT
Performance optimization - JavaScript
PPTX
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
PPT
Performance optimization - Advanced techniques
Building & Testing Scalable Rails Applications
From VMs to Containers: Introducing Docker Containers for Linux and Windows S...
MVC 6 - the new unified Web programming model
Getting Started with Web Services
A (XPages) developers guide to Cloudant - MeetIT
4th Lecture: JSP and such
Microsoft ASP.NET 5 - The new kid on the block
Real World Rails Deployment
Tiki.vn - How we scale as a tech startup
Learn AJAX at ASIT
Zarafa SummerCamp 2012 - Steve Hardy Friday Keynote
CFWheels - Pragmatic, Beautiful Code
DEV03 - How Watson, Bluemix, Cloudant, and XPages Can Work Together In A Real...
Four Ways to Improve ASP .NET Performance and Scalability
10 performance and scalability secrets of ASP.NET websites
Net coreandsimplcommerce
Performance Testing w/ WebPage Test Private Instance (DrupalCamp Ohio)
Performance optimization - JavaScript
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Performance optimization - Advanced techniques
Ad

Similar to The server side story: Parallel and Asynchronous programming in .NET - ITProDevConnections 2014 (20)

PDF
I see deadlocks : Matt Ellis - Techorama NL 2024
PPTX
Asynchronous programming - .NET Way
PPT
Web services, WCF services and Multi Threading with Windows Forms
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
PPTX
Async and Await on the Server
PPTX
C# 5 deep drive into asynchronous programming
PDF
Building Web APIs that Scale
PPTX
Async/Await
PPTX
End to-end async and await
PDF
Async await...oh wait!
PDF
Concurrecny inf sharp
PPTX
Concurrency in c#
PPTX
Async await
PDF
F#语言对异步程序设计的支持
PPTX
Coding For Cores - C# Way
PDF
Concurrency, Parallelism And IO
PDF
F# and SignalR for a FastWeb
KEY
node.js: Javascript's in your backend
PDF
Event driven programming -- Node.JS
PPTX
What's New in .Net 4.5
I see deadlocks : Matt Ellis - Techorama NL 2024
Asynchronous programming - .NET Way
Web services, WCF services and Multi Threading with Windows Forms
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
Async and Await on the Server
C# 5 deep drive into asynchronous programming
Building Web APIs that Scale
Async/Await
End to-end async and await
Async await...oh wait!
Concurrecny inf sharp
Concurrency in c#
Async await
F#语言对异步程序设计的支持
Coding For Cores - C# Way
Concurrency, Parallelism And IO
F# and SignalR for a FastWeb
node.js: Javascript's in your backend
Event driven programming -- Node.JS
What's New in .Net 4.5
Ad

More from Panagiotis Kanavos (9)

PPTX
77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
PPTX
65ο DotNetZone event: Tpl data flow και railway oriented programming
PPTX
Περατζάδα στο Azure Event Hub
PPTX
Ο βασιλιάς Git!
PPTX
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
PPTX
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
PPTX
Pithos - Architecture and .NET Technologies
PPTX
Πίθος - Αρχιτεκτονική και τεχνολογίες .NET
PPTX
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)
77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
65ο DotNetZone event: Tpl data flow και railway oriented programming
Περατζάδα στο Azure Event Hub
Ο βασιλιάς Git!
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
Pithos - Architecture and .NET Technologies
Πίθος - Αρχιτεκτονική και τεχνολογίες .NET
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPT
Introduction Database Management System for Course Database
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
medical staffing services at VALiNTRY
PDF
System and Network Administraation Chapter 3
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Migrate SBCGlobal Email to Yahoo Easily
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Online Work Permit System for Fast Permit Processing
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ManageIQ - Sprint 268 Review - Slide Deck
Introduction Database Management System for Course Database
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
CHAPTER 2 - PM Management and IT Context
Softaken Excel to vCard Converter Software.pdf
Digital Strategies for Manufacturing Companies
Design an Analysis of Algorithms II-SECS-1021-03
medical staffing services at VALiNTRY
System and Network Administraation Chapter 3
VVF-Customer-Presentation2025-Ver1.9.pptx

The server side story: Parallel and Asynchronous programming in .NET - ITProDevConnections 2014

  • 1. Parallel and Asynchronous Programming The server-side story Panagiotis Kanavos
  • 2. Parallel Extensions • Best kept .NET secret known by all • Since 2010 and .NET 4.0 • Coves Task, Data based processing models • Close cousins • TPL Dataflow • System.Reactive • Available through NuGet • SIMD through RyuJIT
  • 3. Use on the desktop • async/await for asynchronous events • Task.Run for background Processing • Parallel computation • Priorities • Avoid blocking the UI • Reduce execution time • Use all available cores
  • 4. What’s different on the Server • No UI thread • Multiple concurrent users/requests • Throughput/Scalability more important than request durattion • Then there are timeouts • Lots of services, lots of combined timeouts • And …
  • 6. Server Meltdown • Load Balanced farm • High load • First server stuck at 100% CPU • Timeouts, App pool Recycle • More requests redirected to other servers • Second server reaches 100% • Don’t drop the ball! • …
  • 7. Request Queuing in ASP.NET IIS Queue • Req 1 • Req 2 • Req 3 ASP.NET Worker Threads • Thread 1 • Thread 3 App Domain • Thread 2
  • 8. Why … • Lots of threads  Worse Scalability • Can’t run more threads than cores • Thrashing • When everyone awaits everyone else, no-one finishes • HATES blocking calls! • Thread Pool starvation  Can’t serve • 500 Too Busy • Immediate actio • App Pool Recycle • Only possible action really …
  • 9. Asynchronous Execution • More important for server than speed • Stackless Python  No blocking calls • A view of the real world • No blocking I/O at the OS level • I/O = File, Database, WS/REST calls • On IIS – IO Threads • Since the IO Thread is doing the job, why am I waiting for it?
  • 10. Async on WebForms • Basic support through async event handlers • async void Button_Click μπρρρ… • <%@ Page Async=“true” %> • Use Page.RegisterAsyncTask for jobs longer than a single request • Raw Background threads can be killed at any time
  • 11. Async on MVC/Web API • Async actions! • Plays well with Database/EF async • HostingEnvironment.QueueBackgroundWorkItem • Notifies IIS • Gives 90 sec to task to finish in case of shutdown/recycle • HangFire, Azure Web Jobs for repeating tasks
  • 12. Parallel.For and PLINQ • Data Parallelism • Cuts the data in chunks • Task per chunk • Generally, 1 Task/Core • Also uses the calling thread • Looks like blocking (it’s not) • Not a concern on the server
  • 13. Dataflow DB call • I:Date • O:Sales Web call • I:Sale • O:Details • X3 • Bounded Analyze Store • Bulk Insert to DB
  • 15. The basics • Processing models • Agent and Pipeline • and MapReduce data processing • One task per block (configurable) • Input/Output Buffer • Block Types • Buffering • Execution • Grouping • Links between blocks
  • 16. Execution Blocks • Action Block • Classic Job Queue • Rudimentary Agent implementation • Transform Block • Transform one Input item to one Output • Call a web service for a Stock to get History as a package • TransformMany Block • For one input item multiple output items • Parallel Execution
  • 17. Throttling • BoundedCapacity • Maximum input size • Blocks sources • MaxMessagesPerTask • Recycle a Task after X messages • CPU Throttling
  • 19. Buffering Blocks • Buffer Block • Easy Publisher/Subscriber implementation • Broadcast Block • Last event sent to many subscribers • WriteOnce block
  • 21. Grouping Blocks • Batching Block • Join Block • 2-3 Inputs, 1 Tuple<T1,T2,T3> output • BatchedJoin Block • Batch  Join • T1, T2  Tuple< IList<T1>, IList<T2>>
  • 23. Links • LinkTo  IDisposable • Disconnect  Dispose • Settings • Filter messages through Lambda • Propagate Completion
  • 24. Custom combination with Encapsulate • New block • One Input, One Output
  • 25. Error Handling In case of error a block • Discards input messages • Refuses new messages • Propagate exception • Caught with await/.Wait()
  • 27. Reactive Extensions • Event monitoring and processing • Control Events • Records in Event Log • Calls to a Web Service • Time-based events • Handle as a stream • Works like LINQ • Foundation for Event Processing
  • 28. Sources • Anything implementing IObservable<> • Time • Observable.Interval • Events • Observable.FromEventPattern • BeginXXX/EndXXX • Observable.FromAsyncPattern • State machines / “Loops” • Observable.Generate • Tasks • Every IEnumerable<T> • Subject<T>  mySubject.OnNext(data)
  • 30. Useful Functions • Interval • Buffer • Sample • Throttle • Window • Skip • Take • TakeUntil
  • 31. Transformations • Anything goes • task ... AsObservable() • block … AsObservable() • await Observable • ToTask<T>
  • 32. SignalR • Real Time communication from Server to Client • Push Notifications • Long process progress • Eg. Search for tickets
  • 33. Multiple Techniques • Web sockets, falling back to … • Server Sent Events • Forever Frame • Ajax Long Polling
  • 34. Call function on Browser • Server-side: Clients.All.DoSomething(data) • Browser-side hub.client.updateProgress = function (data)
  • 38. Supported backplanes • Windows Azure Bus • SQL Server • Redis • Recommended Distributed memory cache for new Azure projects • Open Source Rulez!
  • 40. Useful links- Courses MVA Course • Lighting up Real-Time Web Communications with SignalR Pluralshight Course • Async and Parallel Programming: Application Design Books • The C# Concurrency Cookbook, Stephen Cleary, O’Reilly
  • 41. Useful Links - Sites Sites • Signal R • TPL Dataflow • Reactive Extensions • Using Asynchronous Methods in ASP.NET • 101 Rx Samples Blogs • Stephen Cleary • The Magic of using Acync in ASP.NET, Scott Hanselman • How to run background tasks in ASP.NET, Scott Hanselman
  • 42. Session Evaluation Your feedback is important and valuable Submit before the event’s close session to WIN prizes 2ways to access Go to m.itprodevconnections.gr Ask for an Evaluation Sheet from the registration desk
  • 43. Microsoft Virtual Academy for IT Pros Όλα τα trainings που χρειάζεσαι δωρεάν σε ένα site! /virtualization/ /server-infrastructure/ /private-cloud/ /hybrid-cloud/ /desktop-devices-management/ /licensing/ /και πολλά άλλα… www.microsoftvirtualacademy.com MVA
  • 44. Microsoft Virtual Academy LIVE Όλα τα trainings που χρειάζεσαι δωρεάν σε ένα site! www.microsoftvirtualacademy.com 4 ΜΕΡΕΣ Εκπαίδευση με το Mark Russinovich ΔΩΡΕΑΝ ΠΡΟΕΤΟΙΜΑΣΙΑ για την Εξέταση 70-533: Implementing Azure Infrastructure Solutions Και 50% ΕΚΠΤΩΤΙΚΟ VOUCHER ΣΤΟ ΚΟΣΤΟΣ ΤΗΣ ΕΞΕΤΑΣΗΣ 1 ως 4 Δεκεμβρίου MVA
  • 45. Microsoft Virtual Academy for Devs Όλα τα trainings που χρειάζεσαι δωρεάν σε ένα site! /game development / /web development / /cloud development / /mobile development / /C#-XAML-HTML/ /app development/ /visual studio και πολλά άλλα… www.microsoftvirtualacademy.com MVA