Number 24
May 8, 2024

Deceptive simplicity

The most pedestrian thing you can do in the world of programming is a CRUD application. Those are the initials of “Create, Remove, Update, Delete,” referring to the four operations you can perform on the records of a database.

A gigantic steam engine surrounded by people in 19th century clothes, and a man climbing some stairs that take him to the top of the machine.As he walked up the stairs, he thought, “I am a computer engineer from the year 2022! Operating this primitive steam engine shouldn’t give me any trouble…”

There are countless CRUD applications. A customer management program is a clear example of a CRUD application, but so is a blog, a contact book, an online shop catalog, or Facebook.

You could think programming CRUD applications is a solved problem, and no secrets are left to discover, right? We are so used to CRUD applications that we don’t even expect to find anything complicated in them. It’s just four functions; how difficult can it be? We overlook CRUD applications so much that nobody teaches us how to write them correctly. Therefore, we always make the same mistakes.

The mistake I want to talk to you today is not avoiding simultaneous modifications in a register.

Rose and Joe own a bookstore, and they’ve finally received new copies of the book “The Best Coding Sheets,” by famous author Jacobo Tarrío. The books sell so well that they don’t last five minutes on the shelves. Due to a miscommunication, they both rush to update the store’s website. As Rose checks the “available for sale” box, Joe modifies the description to announce that THE NINTH EDITION IS HERE. Then, both of them click the “update” button at the same time.

In this utterly truthful story, Rose and Joe experience a data race. Both changes compete against each other, and the one that arrives second destroys the effects of the first change. For example, Rose’s change comes first, checking the “available” box; however, Joe’s change leaves that box unchecked, and that’s how the record is stored. After this mishap, their customers cannot buy the book, and Joe and Rose are surprised when they don’t sell any books after two hours.

The way to overcome this problem is to detect simultaneous changes in the same record. You can do it like Wikipedia does: each article has a “last modified timestamp,” updated every time someone changes it. When someone clicks the “update” button, Wikipedia checks if the article’s current version is the same as the one the user was editing; if not, it shows an error message. If Rose and Joe’s website had worked similarly, they could have avoided the problem. They wouldn’t have suffered losses because of a poorly designed CRUD application.

We’ve been making CRUD applications for decades. Still, many modern ones have this problem because most programmers have yet to realize there is a correct way to build them. Next time you have a “simple” project, don’t underestimate it.

The illustration for this Coding Sheet comes from “Appletons’ cyclopædia of applied mechanics.”