Why async matters

Post on 11-Jul-2015

427 views 0 download

Tags:

Transcript of Why async matters

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

My thre

ad

Their t

hre

ad(s

)

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

Sync

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

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

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

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

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

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 :(

Resources

Thread is a very important and heavyweight

resource:

- Keeps memory (from 1 MB)

- Consumes CPU time

- .NET Garbage Collector travers all

threads’ roots

- …

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 completed, notification is sent to

the port

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

Async

Timeline

My thre

ad

Their t

hre

ad(s

)

Sta

rt o

f

Ge

tClie

nt

SQ

L Q

ue

ry

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

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

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

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

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

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)

- Task/Task<T>

- CancellationToken

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

...

}

API

XAsync is much easier using C# 5

async void ReadFile(FileStream file){

result=new byte[fileLength];

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

}

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!

API

Available .NET API:

- FileI/O – yes

- Network I/O – yes

- ADO.NET: SqlClient - yes

- EF6 in RC with Async support – yes

- EF5 – no

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!

Thank you!