why async matters

54
Why async matters Timur Babyuk timur.babyuk at gmail.com

Upload: timbc

Post on 11-Jul-2015

440 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Why async matters

Why async matters

Timur Babyuk

timur.babyuk at gmail.com

Page 2: Why async matters
Page 3: Why async matters

Intro

What is async?

Page 4: Why async matters

Intro

What is async?

Asynchrony VS Synchrony

Page 5: Why async matters

Intro

Sync

Page 6: Why async matters

Intro

Sync

Page 7: Why async matters

Intro

Sync

Page 8: Why async matters

Intro

Async

Page 9: Why async matters

Intro

Async

Page 10: Why async matters

Intro

Async

Page 11: Why async matters

Intro

Async

Page 12: Why async matters

Real world

How does this work on my computer?

Page 13: Why async matters

Usual case: obtain data from DB

client = clientRepository.GetClient(id);

Page 14: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Page 15: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

Page 16: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Page 17: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

Page 18: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Page 19: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Page 20: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Page 21: Why async matters

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Waste :(

Page 22: Why async matters

Resources

Thread is a very important and heavyweight

resource:

- Keeps memory (from 1 MB)

- Consumes CPU time

- .NET Garbage Collector travers all

threads’ roots

- …

Page 23: Why async matters

Resources

Too many threads leads to slowdown

Page 24: Why async matters

Resources

Too many threads leads to slowdown

…even if threads do nothing

Page 25: Why async matters

I/O Completion Ports

Page 26: Why async matters

I/O Completion Ports

- Work on OS level

Page 27: Why async matters

I/O Completion Ports

- Work on OS level

- Appeared in Windows NT 3.5

Page 28: Why async matters

I/O Completion Ports

- Work on OS level

- Appeared in Windows NT 3.5

- Link with I/O op: network socket, file

Page 29: Why async matters

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

Page 30: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Page 31: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

Page 32: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Page 33: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Page 34: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

Page 35: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Page 36: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Page 37: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

Page 38: Why async matters

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Go

t D

ata

En

d o

f

Ge

tClie

nt

No Waste :)

Page 39: Why async matters

Real life?

Page 40: Why async matters

Real life?

API matters!

- File I/O

- DB queries

- Network

- …

Page 41: Why async matters

API

Two approaches

Page 42: Why async matters

API

Two approaches

- BeginX / EndX

Page 43: Why async matters

API

Two approaches

- BeginX / EndX (BeginRead/EndRead)

- IAsyncResult interface

- AsyncCallback

Page 44: Why async matters

API

Two approaches

- BeginX / EndX (BeginRead/EndRead)

- IAsyncResult interface

- AsyncCallback

- XAsync

Page 45: Why async matters

API

Two approaches

- BeginX / EndX (BeginRead/EndRead)

- IAsyncResult interface

- AsyncCallback

- XAsync (ReadAsync)

- Task/Task<T>

- CancellationToken

Page 46: Why async matters

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){

...

}

Page 47: Why async matters

API

XAsync is much easier using C# 5

async void ReadFile(FileStream file){

result=new byte[fileLength];

await file.ReadAsync(result, 0, result.Length);

}

Page 48: Why async matters

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!

Page 49: Why async matters

API

Available .NET API:

- FileI/O – yes

- Network I/O – yes

- ADO.NET: SqlClient - yes

- EF6 in RC with Async support – yes

- EF5 – no

Page 50: Why async matters

API

Available 3dparty API (just a few):

- Redis client BookSleeve – Yes

- Solr.Net – No

- NHibernate – No

- …

Page 51: Why async matters

Platforms

Available platforms:

- MVC – Yes

- WebForms – Yes

- WCF – Yes

- …

Page 52: Why async matters

Platforms

Available platforms:

- MVC – Yes

- WebForms – Yes

- WCF – Yes

- …

- NodeJS – yes!

Page 54: Why async matters

Thank you!