This document discusses parallel and asynchronous programming on servers. It covers techniques like Task Parallel Library (TPL), Reactive Extensions (Rx), and Dataflow that can be used for parallel processing on servers. Unlike desktop applications where the focus is on reducing execution time, server applications prioritize throughput and scalability over individual request duration. Asynchronous programming is more important on servers to avoid blocking and improve throughput. The document demonstrates various asynchronous programming patterns on ASP.NET like async actions and background processing using libraries like SignalR. It also provides demos of parallel programming techniques like Parallel.For, TPL Dataflow, and Rx.
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
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
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