Multiple queued foreground actions are batched in a single request (XHR) to minimize network traffic. The batching of actions is also known as boxcar’ing, similar to a train that couples boxcars together.
The server sends the XHR response to the client when all actions have been processed on the server. If a long-running action is in the boxcar, the XHR response is held until that long-running action completes. Marking an action as background results in that action being sent separately from any foreground actions. The separate transmission ensures that the background action doesn’t impact the response time of the foreground actions.
When the server-side actions in the queue are executed, the foreground actions execute first and then the background actions execute. Background actions run in parallel with foreground actions and responses of foreground and background actions may come back in either order.
We don’t make any guarantees for the order of execution of action callbacks. XHR responses may return in a different order than the order in which the XHR requests were sent due to server processing time.
If two actions must be executed sequentially, the component must orchestrate the ordering. The component can enqueue the first action. In the first action’s callback, the component can then enqueue the second action.
The framework throttles foreground and background requests separately. This means that the framework can control the number of foreground requests and the number of background actions running at any time. The framework automatically throttles requests and it’s not user controlled. The framework manages the number of foreground and background XHRs, which varies depending on available resources.
Even with separate throttling, background actions might affect performance in some conditions, such as an excessive number of requests to the server.
To set an action as a background action, call the setBackground() method on the action object in JavaScript.
// set up the server-action action var action = cmp.get("c.serverEcho"); // optionally set actions params //action.setParams({ firstName : cmp.get("v.firstName") }); // set as a background action action.setBackground();