Notes

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.

Tom Oliver's profile picture
Tom Oliver

Some lesser known nice things about Japan 🗾

  • Ambulances are loud, but not too loud.
  • When Police cars do unexpected things like making a U-turn suddenly, they politely inform everyone nearby with a megaphone.
  • When large vehicles make a turn near a pedestrian they play a vocal warning "Watch out! I'm about to turn left!"
  • Dog owners carry a bottle of water to wash away any pee the dog might do on a walk.
  • There are lots of outside gyms.
  • There are lots of summer festivals which aren't exclusively an excuse to get drunk.
  • Its not okay to be ugly.
    • You better look your best if you're thinking about leaving your home.
    • Foreigners usually get the benefit of the doubt.
  • Lots of stuff made in Japan for Japanese people only. (Films, cars, appliances...)
  • There is one and only one right way to do everything.
    • If you order something in a cafe or a restaurant, chances are they'll tell you the right way to consume it.
    • e.g. You order an iced coffee creamy thing from a local cafe.
      1. First taste the top and bottom layers independently before mixing them both together with the straw...
  • There is a lot of wildlife everywhere.
    • Probably more wildlife in Tokyo than in any National park in the UK.
  • There are dry ice machines in some supermarkets.
  • Some cars have a horn? that says in a polite voice "Please watch out, a car is passing by!" to unsuspecting pedestrians that haven't noticed.
  • Relaxing background music automatically starts playing the instant your skin comes into contact with the toilet seat.
  • Safety first
    • At petrol stations there is an "anti static electricity pad" for you to touch before filling up your car.
  • The sky is big. Power cables on a hot sunny dayPower cables on a hot sunny day
Tom Oliver's profile picture
Tom Oliver

Its been a while since I did any data modelling in typescript.

TS
// An example of a user management system
// Define user status type, just an enum
type UserAccountStatus = "ACTIVE" | "DORMANT" | "DELETED"
// Define a base type
type BaseUser = { name: string; status: UserAccountStatus }
// Create concrete types using the base type
type ActiveUser = BaseUser & { status: "ACTIVE" }
type DormantUser = BaseUser & { status: "DORMANT" }
type DeletedUser = BaseUser & { status: "DELETED" }
type User = ActiveUser | DormantUser | DeletedUser
// Create the type of function we want to implement
// Make return type a promise because we talk to the DB
type DeleteUser = (user: ActiveUser | DormantUser) => Promise<DeletedUser>
// do a temporary implementation of the function
const deleteUser: DeleteUser = (user) => {
const deletedUser: DeletedUser = { ...user, status: "DELETED" }
// define a helper that we haven't got yet
return writeUserToDB(deletedUser)
}
// declare the type of the helper like so:
declare function writeUserToDB<A extends User>(user: A): Promise<A>