1

Idempotence Now Prevents Pain Later

 3 years ago
source link: https://ericlathrop.com/2021/04/idempotence-now-prevents-pain-later/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Idempotence Now Prevents Pain Later by Eric Lathrop

Idempotence Now Prevents Pain Later

By Eric Lathrop on April 6, 2021

Idempotence is the property of a software that when run 1 or more times, it only has the effect of being run once. I'll describe a process I'm making at work, and describe the problems that idempotence will help avoid.

At work we maintain customer accounts with money balances in them. We need to charge dormant customers a monthly fee so that we don't have to keep their money on our accounting books forever. The basic way to setup this process looks like this:

  1. Query the database to find all dormant accounts with a balance
  2. Charge each of these accounts a fee
  3. Setup a cron job to run this on the first of the month at midnight

Simple enough, but think of ways it can go wrong.

  • The computer running the job could reboot, be offline, or have its virtual machine killed, even for just a minute at midnight on the first of the month. In this case the job never gets run for this month. Hopefully someone notices.
  • Maybe an auto-scaling process, or botched deployment leaves you with 2 (or more) computers running the job. Maybe you just want multiple servers for redundancy. In this case customers will be charged multiple fees! This will be really annoying to clean up.

If we take a little more time to make the process idempotent, we eliminate these types of errors. Changes in bold.

  1. Query the database to find all dormant accounts with a balance, which haven't been charged the fee this month.
  2. Charge each of these accounts a fee
  3. Setup a cron job to run this every hour

As a bonus, since the process runs at all hours, there's more of a chance you'll notice any errors during normal business hours, and not need to wake up in the middle of the night to fix it.

About Me

e9495adcf20e29d401f7f2749aede801 I'm a software engineer living in Louisville, Kentucky. I make video games at Two Scoop Games, volunteer at Louisville Makes Games, and help organize the local tech scene at LouisvilleTech.org. I work primarily with JavaScript, React, Elixir, Node.js, and Ruby.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK