Optimistic locking vs Pessimistic locking explained concisely

I’ve found this funny and interesting explanation of Optimistic vs Pessimistic locking through the analogy with the toilet door and could not stand not to share :)

Optimistic locking is like leaving the door to the toilet open: you have a number of toilets, select one, and you are optimistic that nobody will come to use the same toilet, too. If someome comes, it will of course be embarassing, an exception is raised, and he must abort his try, but you hope that these conflicts are the execption.

Pessimistic locking is like always locking the door to the toilet: although there are a number of different toilets, you are expecting every time that somebody else will come to use one, too, and that the only safe option is to lock all toilet doors (which corresponds to page locking). If someone comes and wants to use a toilet, he cannot enter and must wait before the locked door.

Found at Optimistic vs. Pessimistic Locking.

Tags: ,