13

Do jobs placed in apex queue are actually executed the order they've been put in?

Or is this expected by developer to be implemented manually by using chaining?

1 Answer 1

13

No, I don't believe the order of execution for Queueable Apex is defined. They "will be processed when system resources become available" (Source). The AsyncApexJob records that represent the queued jobs don't have a concept of ordering (other than the created date).

Instead you should use chaining, which is defend in the docs as:

If you need to run a job after some other processing is done first by another job, you can chain queueable jobs.


The Apex Flex Queue for batch jobs is something different again. It does however have the concept of reordering.

6
  • 1
    The problem is, of course, we've all observed that older jobs go before newer jobs. We have no idea if this is intentional, or if it'll change. Future calls also seem to work the same way for now. It'd be interesting to see if we could get the Word on the matter.
    – sfdcfox
    Commented Jun 23, 2016 at 0:29
  • 1
    @sfdcfox My concern with relying on the observed ordering is that it could break. I could see the proposed pilot changes in Bigger Apex Limits with Enhanced Futures and Run Future Methods with Higher Limits shuffling when async jobs run. Commented Jun 23, 2016 at 0:48
  • Those pilot programs are ancient. Who knows if they'll ever see the light of day. I wasn't disagreeing with this answer, of course, just that we don't know what we don't know. One should definitely use chaining if the order is dependent.
    – sfdcfox
    Commented Jun 23, 2016 at 1:50
  • 1
    Had a look at data and seems around 10% of the transactions ran earlier than transaction created before it.
    – dzh
    Commented Jun 23, 2016 at 13:03
  • Apparently Platform events could be another mitigation for ensuring the sequence of events.
    – dzh
    Commented May 15, 2017 at 2:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .