build reliable asynchronous code with queueable apex
TRANSCRIPT
![Page 1: Build Reliable Asynchronous Code with Queueable Apex](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/1.jpg)
Build Reliable Asynchronous Code
with Queueable Apex
Dan Appleman
CTO – Full Circle Insights, Author of Advanced Apex Programming
@danappleman
![Page 2: Build Reliable Asynchronous Code with Queueable Apex](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/9.jpg)
... 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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/12.jpg)
Demos
![Page 13: Build Reliable Asynchronous Code with Queueable Apex](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/13.jpg)
Always include an
on/off switch with
Queueable Apex
(if you’re chaining)
![Page 14: Build Reliable Asynchronous Code with Queueable Apex](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051318/58a4564d1a28ab55068b606d/html5/thumbnails/15.jpg)
Questions?