Build Reliable Asynchronous Code with Queueable Apex

15
Build Reliable Asynchronous Code with Queueable Apex Dan Appleman CTO Full Circle Insights, Author of Advanced Apex Programming [email protected] @danappleman

Transcript of Build Reliable Asynchronous Code with Queueable Apex

Page 1: Build Reliable Asynchronous Code with Queueable Apex

Build Reliable Asynchronous Code

with Queueable Apex

Dan Appleman

CTO – Full Circle Insights, Author of Advanced Apex Programming

[email protected]

@danappleman

Page 2: Build Reliable Asynchronous Code with Queueable Apex

A Typical Future Call

@future

public static void updateTherecords(Set<ID> recordIDs)

{

// Perform some operation on the records

}

Page 3: Build Reliable Asynchronous Code with Queueable Apex

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

Page 4: Build Reliable Asynchronous Code with Queueable Apex

Will the future call work?

Yes

No

Maybe

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

Page 5: Build Reliable Asynchronous Code with Queueable Apex

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)

Page 6: Build Reliable Asynchronous Code with Queueable Apex

And when it fails...

What do you mean

you don’t think my

future call is

working?

Page 7: Build Reliable Asynchronous Code with Queueable Apex

And when it fails...

@future

public static void updateTherecords(Set<ID> recordIDs)

{

// Perform some operation on the records

}

Page 8: Build Reliable Asynchronous Code with Queueable Apex

Maybe you don’t care?

40% of our data is

obsolete or messed

up anyway.. What’s

another few records?

Page 9: Build Reliable Asynchronous Code with Queueable Apex

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

a penny

It’s the end of

the world

Page 10: Build Reliable Asynchronous Code with Queueable Apex

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

Page 11: Build Reliable Asynchronous Code with Queueable Apex

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

Page 12: Build Reliable Asynchronous Code with Queueable Apex

Demos

Page 13: Build Reliable Asynchronous Code with Queueable Apex

Always include an

on/off switch with

Queueable Apex

(if you’re chaining)

Page 14: Build Reliable Asynchronous Code with Queueable Apex

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

Page 15: Build Reliable Asynchronous Code with Queueable Apex

Questions?