Technology

Idempotence Now Prevents Pain Later by Eric Lathrop

Idempotence Now Prevents Pain Later by Eric Lathrop

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.

  • 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


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.

Related Articles

Back to top button