Find your content:

Search form

You are here

What are the implications of implementing Database.Stateful?

 
Share

I've recently implemented a batch apex class that initially did not implement Database.Stateful. After using it for a while as-is I decided to implemente better error handling via a simple Map to store any error messages for records being processed and to email them to me in the finish(Database.BatchableContext) method.

All well and good, now on to the meat of my question: once this was in place I noticed a serious performance hit on my job's total runtime. (each execute seems to be roughly the same though). Why is this, and are there any other side effects to implementing or not implementing Database.Stateful?


Attribution to: ca_peterson

Possible Suggestion/Solution #1

Daniel Ballinger: No, batches do not ever run simultaneously. You are correct, however, that serialization is the culprit here.

grigriforce: what's your batch size? If you're doing a million records, and your batch size is 1, then you will serialize/deserialize your state 1M times. Even with a small serialized object, that's gonna hurt.


Attribution to: Rich Unger

Possible Suggestion/Solution #2

Commenting on a long-dead question:

  1. The difference between a Batchable batch and a Batchable and Stateful batch is that the Batchable's state is updated (serialized and stored) at the end of each start and execute call when Stateful, in addition to it being (serialized and) stored on Database.executeBatch (or System.scheduleBatch) and deserialized before each call to start, execute and finish whether the batchable is stateful or not.
  2. Up to 5 different batches' operations (execute and I assume finish) can be executed in parallel on a given org, but for a given batch instance only one async operation is performed at a time (and always in the order start -> execute* -> finish).
  3. Only one batch instance's start method will be executed at a time on a given org (which gives a sort of "synchronized block").

See the Salesforce docs for details on points 2 and 3.

Clearly the additional serialization and storage actions after the start and execute calls have an overhead. Note that a batchable that is not stateful still does hold its initial state in the database; it just doesn't change from async operation to async operation without being stateful. If that initial state is large then the batch still has that deserialization overhead even when not stateful.


Attribution to: Phil W
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/689

My Block Status

My Block Content