Why async matters
Timur Babyuk
timur.babyuk at gmail.com
Intro
What is async?
Intro
What is async?
Asynchrony VS Synchrony
Intro
Sync
Intro
Sync
Intro
Sync
Intro
Async
Intro
Async
Intro
Async
Intro
Async
Real world
How does this work on my computer?
Usual case: obtain data from DB
client = clientRepository.GetClient(id);
Sync
Timeline
MythreadTheirthread(s)
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Sync
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Waste :(
Resources
Thread is a very important and heavyweight
resource:
- Keeps memory (from 1 MB)
- Consumes CPU time
- .NET Garba...
Resources
Too many threads leads to slowdown
Resources
Too many threads leads to slowdown
…even if threads do nothing
I/O Completion Ports
I/O Completion Ports
- Work on OS level
I/O Completion Ports
- Work on OS level
- Appeared in Windows NT 3.5
I/O Completion Ports
- Work on OS level
- Appeared in Windows NT 3.5
- Link with I/O op: network socket, file
I/O Completion Ports
- Work on OS level
- Appeared in Windows NT 3.5
- Link with I/O op: network socket, file
- Once I/O c...
Async
Timeline
MythreadTheirthread(s)
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
Async
Timeline
MythreadTheirthread(s)
Startof
GetClient
SQLQuery
GotData
Endof
GetClient
No Waste :)
Real life?
Real life?
API matters!
- File I/O
- DB queries
- Network
- …
API
Two approaches
API
Two approaches
- BeginX / EndX
API
Two approaches
- BeginX / EndX (BeginRead/EndRead)
- IAsyncResult interface
- AsyncCallback
API
Two approaches
- BeginX / EndX (BeginRead/EndRead)
- IAsyncResult interface
- AsyncCallback
- XAsync
API
Two approaches
- BeginX / EndX (BeginRead/EndRead)
- IAsyncResult interface
- AsyncCallback
- XAsync (ReadAsync)
- Tas...
API
BeginX/EndX code is often too complex to maintain
asyncResult = fStream.BeginRead(
tempState.ReadArray, 0 , tempState....
API
XAsync is much easier using C# 5
async void ReadFile(FileStream file){
result=new byte[fileLength];
await file.ReadAsy...
API
XAsync is much easier using C# 5
async void ReadFile(FileStream file){
result=new byte[fileLength];
await file.ReadAsy...
API
Available .NET API:
- FileI/O – yes
- Network I/O – yes
- ADO.NET: SqlClient - yes
- EF6 in RC with Async support – ye...
API
Available 3dparty API (just a few):
- Redis client BookSleeve – Yes
- Solr.Net – No
- NHibernate – No
- …
Platforms
Available platforms:
- MVC – Yes
- WebForms – Yes
- WCF – Yes
- …
Platforms
Available platforms:
- MVC – Yes
- WebForms – Yes
- WCF – Yes
- …
- NodeJS – yes!
Practice
1. Simple ASP.NET MVC application
https://github.com/timba/async-web
2. Self-hosted WCF services application, use...
Thank you!
Why async matters
Upcoming SlideShare
Loading in …5
×

Why async matters

695 views
560 views

Published on

A short overview of the topic: why asynchronous I/O operations important.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
695
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Why async matters

  1. 1. Why async matters Timur Babyuk timur.babyuk at gmail.com
  2. 2. Intro What is async?
  3. 3. Intro What is async? Asynchrony VS Synchrony
  4. 4. Intro Sync
  5. 5. Intro Sync
  6. 6. Intro Sync
  7. 7. Intro Async
  8. 8. Intro Async
  9. 9. Intro Async
  10. 10. Intro Async
  11. 11. Real world How does this work on my computer?
  12. 12. Usual case: obtain data from DB client = clientRepository.GetClient(id);
  13. 13. Sync Timeline MythreadTheirthread(s)
  14. 14. Sync Timeline MythreadTheirthread(s) Startof GetClient
  15. 15. Sync Timeline MythreadTheirthread(s) Startof GetClient SQLQuery
  16. 16. Sync Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData
  17. 17. Sync Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient
  18. 18. Sync Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient
  19. 19. Sync Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient
  20. 20. Sync Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient Waste :(
  21. 21. Resources Thread is a very important and heavyweight resource: - Keeps memory (from 1 MB) - Consumes CPU time - .NET Garbage Collector travers all threads’ roots - …
  22. 22. Resources Too many threads leads to slowdown
  23. 23. Resources Too many threads leads to slowdown …even if threads do nothing
  24. 24. I/O Completion Ports
  25. 25. I/O Completion Ports - Work on OS level
  26. 26. I/O Completion Ports - Work on OS level - Appeared in Windows NT 3.5
  27. 27. I/O Completion Ports - Work on OS level - Appeared in Windows NT 3.5 - Link with I/O op: network socket, file
  28. 28. I/O Completion Ports - Work on OS level - Appeared in Windows NT 3.5 - Link with I/O op: network socket, file - Once I/O completed, notification is sent to the port
  29. 29. Async Timeline MythreadTheirthread(s)
  30. 30. Async Timeline MythreadTheirthread(s) Startof GetClient
  31. 31. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery
  32. 32. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery
  33. 33. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData
  34. 34. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient
  35. 35. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient
  36. 36. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient
  37. 37. Async Timeline MythreadTheirthread(s) Startof GetClient SQLQuery GotData Endof GetClient No Waste :)
  38. 38. Real life?
  39. 39. Real life? API matters! - File I/O - DB queries - Network - …
  40. 40. API Two approaches
  41. 41. API Two approaches - BeginX / EndX
  42. 42. API Two approaches - BeginX / EndX (BeginRead/EndRead) - IAsyncResult interface - AsyncCallback
  43. 43. API Two approaches - BeginX / EndX (BeginRead/EndRead) - IAsyncResult interface - AsyncCallback - XAsync
  44. 44. API Two approaches - BeginX / EndX (BeginRead/EndRead) - IAsyncResult interface - AsyncCallback - XAsync (ReadAsync) - Task/Task<T> - CancellationToken
  45. 45. API BeginX/EndX code is often too complex to maintain asyncResult = fStream.BeginRead( tempState.ReadArray, 0 , tempState.ReadArray.Length, new AsyncCallback(EndReadCallback), tempState); void EndReadCallback(IAsyncResult result){ ... }
  46. 46. API XAsync is much easier using C# 5 async void ReadFile(FileStream file){ result=new byte[fileLength]; await file.ReadAsync(result, 0, result.Length); }
  47. 47. API XAsync is much easier using C# 5 async void ReadFile(FileStream file){ result=new byte[fileLength]; await file.ReadAsync(result, 0, result.Length); } Note: Mind the overhead!
  48. 48. API Available .NET API: - FileI/O – yes - Network I/O – yes - ADO.NET: SqlClient - yes - EF6 in RC with Async support – yes - EF5 – no
  49. 49. API Available 3dparty API (just a few): - Redis client BookSleeve – Yes - Solr.Net – No - NHibernate – No - …
  50. 50. Platforms Available platforms: - MVC – Yes - WebForms – Yes - WCF – Yes - …
  51. 51. Platforms Available platforms: - MVC – Yes - WebForms – Yes - WCF – Yes - … - NodeJS – yes!
  52. 52. Practice 1. Simple ASP.NET MVC application https://github.com/timba/async-web 2. Self-hosted WCF services application, uses a bit of outdated stuff https://github.com/timba/async-ef-performance-test
  53. 53. Thank you!

×