Tom Oliver's profile picture
Tom Oliver

Optimistic VS Pessimistic Locking

Imagine you are on a plane and you need to pee. You're a stubborn guy. When you leave your seat, you are determined to pee one way or another. You must assume your fellow passengers are as least as stubborn.

Optimistic Locking

There is a single toilet located in the middle of the plane that can be accessed from multiple directions. You are sitting at the back and can only see the entrance to the toilet that is in your direct line of sight. You want to know if its ok to go to the toilet so you check the overhead light which says "vacant". Assuming that it will remain vacant for the entirety of the time it will take you to reach your destination, you get out of your seat and commit to peeing. Now one of two things are going to happen:

  1. The toilet is vacant by the time you get to it - Success
  2. Someone else has slipped into the toilet via an entrance you couldn't see during the time it took you to walk there. Since you have committed to peeing, you have no choice but to do so in a cup - Failure.

Pessimistic Locking

Due to your toilet paranoia you reserve a seat on a specially designed plane. It is designed such that each seat is directly adjacent to a toilet and has excellent visibility of all its entrances. Because of this, you are able to in a single instant, both check that a toilet is free and enter it. It is therefore guaranteed that you nor any other passenger will be forced to pee in a cup.

So obviously there is a dilemma here.

The specially designed plane will carry less passengers due to its emphasis on providing the stubborn peeing passenger peace of mind. The normal plane will be more efficient but has to account for the occasional cup of pee getting knocked over.

What have we learned?

There is no "correct" way to design a plane, only trade-offs.

No Webmentions for this note yet!