This is going to be a good day
Tags: concurrency, performance, database, clones
Published: 2022-05-09 11:59:19

You wake up with a smile. You unplug from your charger, walk over to the coffee machine and brew your morning cup.

You enjoy a cinnamon roll with your coffee while looking out the window into the immensity beyond.

You don your jet-black helmet*, your cape and boots and leave your quarters.

<sub>* with standard Ominous Voice Modulator™ included.</sub>

## This is going to be a good day

You stroll to the bridge waving hello to everyone you meet along the way*.

<sub>* as usual they fall to their knees, grabbing at their throats, choking.</sub>

This looks like a perfect day to train your army of white-armored clone people. You already know they are useless at shooting things with their blaster guns. They are also pretty lousy meeple workers. Surely they must be good for _something_.

You decide to push your luck and try the famous pick-up-and-deliver mechanic. Yes, this has to work, you briskly walk over to...

## The training grounds

You're standing in the middle of a big room. The room has 10 doors. Behind each door there's a table. In your hand you hold 10 coins, let the party begin...

## The one that works

You call in one of your clones and task it with placing one coin over each table.

The clone takes 1 minute to put the first one down so 10 minutes later the task is done.

But 10 minutes for this menial work feels excessive. What if...

## We throw more clones at the problem

You reset the experiment, summon 10 clones and task each with placing one coin over each table.

After roughly 1 minute of scrambling over the floor the task is done.

That was a 10x improvement, no small feat, we just needed to throw more clones at the problem.

This is getting boring, let's try something different for a change.

## The important task

You now summon back a clone and give it the 10 coins. You then task it to place all 10 coins in the table behind a single door. Sure enough, 1 minute later the task is complete.

Looks like we're not even trying.

## The Mr. Burns effect

What if we ask 10 clones to place the 10 coins on the _same table, at the same time_. Surely this is going to be amusing.

Clones are optimistic by default. That's the only way for them to go through life putting up with all the nonsense they are asked to do.

So when trying to perform this task they just assume things will work. This is called [optimistic locking](https://en.wikipedia.org/wiki/Optimistic_concurrency_control) and surely nothing can go wrong with it.

The clones know not to try your patience so they shuffle to pick-up their coin and rush to the door to place the coin on the table.

And, of course, only one clone gets through the door. The other nine clones hit their heads, stumble and fall to the floor, stunned. As they recover they rinse and repeat, one gets through, eight fall to the floor.

You just sit there laughing ominously, watching clones hit their head and fall. It doesn't get old but you take this time to run some quick math in your chest calculator:

- clone #1: 1 minute (one success)
- clone #2: 2 minutes (one failure, one success)
- clone #3: 3 minutes (two failures, one success)
- ...
- clone #10: 10 minutes (nine failures, one success)

So we should expect about 10 minutes to get this done, but a much more interesting result is the damage done to the clones.

If you count the number of times a clone fell to the floor you'll get a much funnier picture:

- clone #1: fell 0 times
- clone #2: fell 1 time
- clone #3: fell 2 times
- ...
- clone #10: fell 9 times

So in total you enjoyed `9 + 8 + 7 + ... + 1 = 45` slapstick moments in those 10 minutes. Fun aside, the amount of effort required to make this work was pretty intense.

But hold on a second, there's no room for optimism in your army and certainly not on _your_ watch.

So you use your famous mind tricks to squeeze all the optimism out of your clones and try again.

## Pessimistic boredom

This time the picture is different, as the clones rush to the door one reaches the door first and closes it behind it. It eventually comes out and another one goes in. This is all very civilized and boooring.

After 10 minutes of tedium the task is finally done.

## All is well

Your locking adventures left you invigorated. You observe that parallelism can be a great performance optimization (you managed a 10x improvement in your second experiment).

On the other hand, running stuff in parallel just for the sake of comedy is not a good performance strategy, your optimistic lock experiment was 10 times worse than the previous one (i.e. a single clone dropping the 10 coins in one go).

You find it interesting that both pessimistic and optimistic locking experiments took the same 10 minutes, although the optimistic one also produced 45 concussions.

You conclude that pessimistic locking is consistently bad, while optimistic locking is surprisingly (and comically) bad.

Since you're all about the _bad_, you feel good. This was indeed a good day.

Tomorrow you plan to try the optimistic lock again with 90 clones. That should net you a cool hour and a half of pure, unadulterated, slapstick fun and a whopping 4005 falls to the floor.

But all that will have to wait for now you have...

## More pressing matters

You walk back to the space port. On the way there you take a brief detour to commend Governor Cushing on the quality of his clones.

You then proceed to shuttle off of the station and into a much anticipated family reunion.

You know this was a good day because you find yourself humming to your favorite tune, the one that's always stuck in your head: _dum-dum-dum dum-de-dum dum-de-dum_...