Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Background I have 3 identical HTTP servers, holding the same data: api1, api2 and api3.

I want to query this data as reliable and quick as possible. In Java, I would have done this by spawning 3 threads -- each requesting one of my servers, and used the response that I got first (only waiting for the single fastest reply).

Question Can something similar be done efficiently in Apex (for example with @future methods)? There are two things that makes me worried that it won't be efficient:

  1. The docs says that "Each future method is queued and executes when system resources become available". This implies that it's slower than regular methods.

  2. Since @future methods has to return void, I have to write the result to the database(?) and setup some trigger logic that detects when the first response has arrived(?). This sounds slower than regular methods as well.

Am I right in my assumptions?

share|improve this question
If you're interested in load balancing, why not just host a load balancer (apiload) that selects the least busiest of the three servers? –  sfdcfox Aug 14 '14 at 16:06

2 Answers 2

up vote 4 down vote accepted

You have no control over when the future method gets executed after it is queued. there is no priority mechanism that caller can add to future method. You are also correct about your assumption related to future method returning void.

Please note, just for reliability if you are making multiple calls from salesforce to your servers you are adding up to limits on callouts.

I would suggest doing this still in java and pushing the result to salesforce.

share|improve this answer

Apex is single threaded, so you can't throw out 3 requests and see which is fastest.

@future is exactly what it sounds like: something that happens in the future. That is, it's asynchronous and will occur when the platform gets around to it. Usually in less than a couple of seconds, but definitely outside the execution scope of the method that called the @future method.

Best thing to do in Apex it just hit one server and wait for a response.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.