Software Development, Web Design, Training

ColdFusion Summit Notes: Sync or Async the Aha Moment - Shirak Avakian

understanding sync/async

Synchronous - the operation blocks a process until the operation completes
runs “line by line”

validate()
sendEmail()
saveToDatabase()

Asynchronous - non-blocking
only INITIATE the operation
validate()
 — > sendEmail()
saveToDatabase()

sendEmail runs in “parallel mode” at same time as the other code

problem w/long running operations
blocking - when the thread is busy doing its stuff
blocking the processor of your application
when you dispatch an event, there’s a running process
between the running process, until it comes back to whatever initiated the request
that’s the “blocking” part
by the time you get the reply back, that’s the “unblocked” version

in async programming
you want to eliminate the blocking as much as possible

turn 1 chunk into multiple chunks to eliminate blocks

2 types of blocking

1. CPU bound blocking operations (math ops, games, etc)
90% of games rely on async programming
hardware system needs to support async programming

2. IO-bound (in/out) blocking operations. When your code is relying on external resources:
-hard drive / folder / file system / API calls
-network access

Demo: do a call to an API/query in-line and do one 1 runAsync with the “please wait” on the screen

async - run and forget

need to use judgement - when to use “wait”

any logging systems, really we’re not waiting for those results. put them in async mode.
sending email - async
saving stuff to a DB that the UI is NOT relying on - use async

do not use async:
-for simple and basic computations or short running operations
-when you have a single db server not utilizing connection pooling
if you run multiple async procs that talk to the database, updating, deleting, etc, that’s a bottleneck - db will have locking issues. so make sure your database supports “clustering” to get around this
for sequential tasks, where stopping the entire program to wait for a network request or disk i/o makes sense
-if your page is relying on results from a query before it can complete, you don’t gain anything with async

use async:
to make your client application really responsive
5 seconds is “late”
mobile apps, if it takes more than 5 seconds to load, people think the app isn’t working
if you want to make your web app responsive, async
when you can run multiple code blocks in parallel
when you’re interacting w/ an external service provider (API’s, etc)
when you have tasks independent from each other

2 types of async programming
1. client
2. server

client side is visible
you can “feel” it happen
$.ajax( url, async:true );
“async” and “await” keywords in JavaScript
async by itself = run and forget
async / await = let’s you do things when the request is finished

improves performance

data dependency is assured by using the “await” keyword

Asnc for server applications
new in CF2018

server applications , normally happened in blind box
interacting w/ database, file manager, emails, etc
CF provides RunAsync()

cfthread
event gateway
both also async
so why runAsync()?

cfthread - not always easy to manage, or to catch errors, confusing to join threads w/ cfthread
thread scope and attributes scopes are confusing, not very well documented, not many REAL examples out there!

runAsync returns a “future object”
“futures”
an eventual result of an async operation
can run as async mode or as async/await to get results back
methods for ruynAsync:

cancel
error
error( callback, timeout )
error( callback )
get
get( timeout )
is canceled
isdone()
then( callback)
then( callback, timeout )

* any time you use future object, your page thread will wait for the async result

future = runAsync()
creates a new thread separate from the main thread

runAsync( thing ).then( second thing ).then( third thing ).error( onError );
chained functions
they fire in order
error() runs if ANY of the threads throw an error
what if you want to run the error() also asynchronously? so you don’t see the error but it gets logged for later
onError() is not yet non-blocking, this is coming in an future (ha!) update

Cancel()
IsCanceled()
IsDone()

in try/catch, use “continue” to indicate “i will take care of this error, the rest of the app should just continue on as normal”.