(Photo by Flickr user Ted Major, used under a Creative Commons license)
A few days ago I came across a puzzle. We had a list of updates that we needed to make to our database where we would: grab the data from our database, run some operations on those data, then update the data in the database. An issue arose when some of those updates were using shared resources, and overwriting the changes from another update. We needed a way that we could run our updates in a sequence, while making sure that Update 2 wouldn’t start until Update 1 had completed.
This is the heroic story of how we achieved that.
So, imagine that we have a function that executes a step in a cooking recipe, like so:
And a recipe to make some mediocre cookies that will suffice for this article, but really won’t satisfy anyone:
Now we can carry out steps in our recipe by calling performRecipeStep(), but because some steps are easier than others, we’re not quite sure how long each will take. This raises an issue: if our chef is particularly astute and plopping dough balls (3), and hopelessly incompetent at mixing dough (2), we could end up with a sheet full of unmixed batter.
The first thing we tried was to deliver the instructions in a simple for loop, with the shorthand equivalent, forEach():
But as we can see, our chef simply couldn’t handle being bombarded with all of the instructions at once:
We needed to find a way to deliver the instructions to our chef one by one, while only delivering the next step once he’s done with the previous one.
With this method, we know exactly when the previous step in our process is complete, and only then do we begin the next step.
I’m convinced that there’s likely dozens of ways to solve this problem, many of which may be more performant than this solution. How would you have done it?
Is it easier to solve this problem in your language of choice, or does it ever even pop up?
Which programming workshop will you choose at the #DIW18 Dev Conference?
Ground Up is teaching middle-schoolers computer science, and it’s working
Hear from the privacy pros at Security by the Schuylkill
School kids have the day off Sept. 6 but here’s a coding event you can take them to
Zip Code’s next AMA is Sept. 19
Jocelyn Harper is the new chapter lead for Girl Develop It Wilmington
Learn to lead digital transformation at Phorum 2019
Sign-up for daily news updates from Technical.ly Delaware