Use the Database

Kevin Burke

@derivativeburke

Shyp Logo

burke.services

Who is this talk for?

Node.js monolith serves:

Problem 1: items being reacquired

Items being reacquired

  1. Driver acquires item
  2. Item transferred to warehouse
  3. Item marked as shipped
  4. Driver reacquires item

Lots of confusion

iOS caching problem

Server side

Use a state machine

Naive state machine

Naive implementation

Figuring out the valid states can be really hard

Really hard

Naive implementation v2

Problem 2: duplicate submissions

Duplicate submissions

Duplicate submissions (cont'd)

Two drivers, same pickup

Two drivers

Better state machine

You can run that 1000 times
and only one will succeed*

*Your database may not support CAS

Kyle Kingsbury Aphyr

CAS: a fancy way of
saying UPDATE with WHERE

"jepsen compare and set [your database]"

Lesson: if you type "state machine" into NPM you are doing it wrong

Lesson: read-check-modify-save is incorrect/vulnerable

Just try the write

Just try the write (cont'd)

Problem 3: Partial Assignment

Partial assignment

Partial assignment (cont'd)

"Driver X is in ASSIGNED but doesn't have any pickups."

Our #1 cause of
oncall incidents

Use transactions*

Use transactions

*Your ORM may not support transactions
*Your database may not support transactions

github.com/Shyp/pg-transactions

Transactions can go wrong

Conclusion

Conclusions

Consistency is hard

Let's go shopping

burke.services

Thanks!

Kevin Burke

kev.inburke.com

kev@inburke.com

@derivativeburke


These slides are available at:

kev.inburke.com/slides/use-the-database

/

#