Build Reliable Asynchronous Code with Queueable Apex

Preview:

Citation preview

Build Reliable Asynchronous Code

with Queueable Apex

Dan Appleman

CTO – Full Circle Insights, Author of Advanced Apex Programming

dan@fullcircleinsights.com

@danappleman

A Typical Future Call

@future

public static void updateTherecords(Set<ID> recordIDs)

{

// Perform some operation on the records

}

Will the future call work?

Yes

No

Maybe – I’m not sure, or just afraid of commitment

Leave me alone, I’m just glad to have a seat

Will the future call work?

Yes

No

Maybe

Leave me alone, I’m just glad to have a seat

How could it possibly fail?

Too many future calls (other apps/triggers)

System exceeds max # of future calls in 24 hour period

Some other code inserts/updates record in a future call

and future can’t call future

Validation rules, other logic, or other limits

System flakes (it happens)

And when it fails...

What do you mean

you don’t think my

future call is

working?

And when it fails...

@future

public static void updateTherecords(Set<ID> recordIDs)

{

// Perform some operation on the records

}

Maybe you don’t care?

40% of our data is

obsolete or messed

up anyway.. What’s

another few records?

... Or maybe you do?We’ve lost

a penny

It’s the end of

the world

First Rule of Reliable Asynchronous Programming

Store the async request in the database!

• Unique field on a record

• Status that defines the request

• Separate async request object

Reliable Async Architecture

Take a request

Process the request

Success?

Delete the request

Mark the request as

a failure?

Schedule request for

retry?

Log the error

No

Yes

Demos

Always include an

on/off switch with

Queueable Apex

(if you’re chaining)

Async Comparison

Future Scheduled Batch Queueable

Manageable No Yes Yes Yes

Associate complex data No Yes Yes Yes

Blocks class updates No Configurable ??? ???

Chaining No Yes Finish only YES

Speed Fast Slow Slow Faster

Questions?

Recommended