Idempotence Now Prevents Pain Later

By Eric Lathrop on

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.

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.