DEV Community

Sergey Shinder
Sergey Shinder

Posted on

A supplier sent prices effective in twenty nine days and our Node.js timer applied them in one millisecond

Our pricing service is a Node.js application that holds the current trade price of every product we sell. Suppliers send new price lists with an effective date, and when a list is loaded the service sets a timer for that moment: setTimeout(activate, effectiveAt minus now). When the timer fires, the new prices replace the old. On startup the service reads pending lists from the database and sets their timers again, so a restart loses nothing.

On the third of October our largest timber supplier sent their winter prices, nine percent higher, effective on the first of November. They were live on the third of October, a few seconds after the file was loaded. For nine days our branches charged November prices, until a trade customer compared two invoices and asked why the price had gone up a month before the supplier's letter said it would.

The timer's delay is stored as a signed 32 bit integer of milliseconds. The largest value it can hold is 2,147,483,647, a little under twenty four days and twenty one hours. Node's documentation says what happens to anything bigger: the delay is set to one millisecond. Node prints a TimeoutOverflowWarning when it does, and ours went into the logs between two lines about the price file's size. Twenty nine days is about 2.5 billion milliseconds, so the timer fired as soon as it could.

It had worked for four years because suppliers had always given us two or three weeks' notice. This one gave four. Nothing in our code was new. The input had crossed a limit the code had always had.

We refunded the difference to every account that had paid it, which took finance most of a week. The pricing service no longer schedules anything far in advance. A job runs every minute and activates any price list whose effective time has passed, reading the time from the database, so a list due in a year and a list due in a minute go through the same code. Where we still use timers, for retries and timeouts, a helper refuses any delay over a day. And our tests now load a price list effective in ninety days and check that the old prices are still in force afterwards.

A timer has a range like any other number. Ours was twenty four days, and our suppliers finally gave us more notice than that.

– Sergey Shinder

Top comments (0)