3

I'm looking for a way to queue incoming HTTP Post requests.

Apex REST API handlers are executed in parallel on separate threads. This introduces race conditions if any new records are inserted by the incoming HTTP requests.[1]

I tried inserting the HTTP data into a custom object table for later processing by a trigger. Unfortunately, it looks like the trigger handler is spun out on independent threads, so I still have a race collision.

Does anyone know a way to ensure that a handler is executed only after all previous handlers have completed?

My current thought is to try to implement polling for completion of previous POSTs through the Queueable interface or with @future.

Does anyone have any guidance or thoughts? If you see an easier way, I'm eager to hear about it.

[1] If you don't see how unavoidable race conditions are created, post a comment and I will elaborate. [2] I'm already aware of the Known Issue Article "Race condition in Salesforce upsert operations may cause DUPLICATE_VALUE error" https://success.salesforce.com/issues_view?id=a1p300000008YBEAA2 The documented Workaround does not work for my situation.

6
  • The only locking mechanism I know of is for update on a query. Doesn't have to be on the table you are adding data to.
    – Keith C
    Jan 6, 2018 at 0:26
  • 2
    If you have decided to go for an asynchronous response, you may try creating a recursive batch (infinite recursion) class which picks up the top 5/10 or some records from the request storage object. Though this may not avoid race, you will have way to process records in series not in parallel. Jan 6, 2018 at 0:54
  • Are you getting DUPLICATE_VALUE errors? If not, then the workaround is irrelevant.
    – sfdcfox
    Jan 6, 2018 at 1:27
  • sfdcfox, I am getting DUPLICATE_VALUE errors.
    – Mark Moore
    Jan 17, 2018 at 21:44
  • Saumya, I could possibly re-architect to use asynchronous response an poll for completion, but that does not solve the underlying problem. As you kind of allude to, the batch handing is still executed on multiple independent execution threads. The collision still happens.
    – Mark Moore
    Jan 17, 2018 at 21:46

0

You must log in to answer this question.

Browse other questions tagged .