You may have encountered these two concepts, when dealing with database, or generally CS problem about accessing a single resources from more than 1 processes/threads. So basically they are respectively correspond to two different assumptions.
I assume you already know what a database transaction is, it’s basically a unit of work that either completed in its entirety or have no effect. See Also ACID properties.
Let’s think optimistically!
In this assumption, we believed that the chances of conflict is small. So the validation is only performed at the end (commit time). It the thread detects the transaction fails the validation, it will simply be aborted. This reduces the time where the resource is blocked from other threads and thus improve performance in general.
But I’m Pessimistic…
This can be thought of as a “strict” assumption. You believe that the chance of conflict is high, so you always acquire the lock at the beginning of the transaction, blocking any other operations that may violate the ACID properties.
Which one should I choose in my code?
That depends. Pessimistic concurrency control is always the fail-safe approach, but it will hurts the performance. Optimistic strategy allows some performance optimization, and is the best choice generally. However, if the chances of transaction conflict is high, pessimistic strategy may provide better performance and fail-proof.
This is my notes on this topic, hope it helps your understanding. Feel free to leave your comments here!