Clocks, ticks and synchronization: How CueTimer stays on time

CueTimer helps people stay on schedule. You should expect this app to show the time precisely and reliably. In this text I want to discuss how we approach this issue from a technical point of view, and show some lessons that we have learned along the way:
We can’t do everything ourselves inside the software. We have to rely on other timing-devices that keeps the world in sync.

Part 1: How to create an accurate software countdown

An oscillator inside the computer creates ticks. These are electronical pulses that happen at a fixed interval. The concept of time inside a computer comes from counting these ticks, and converting them to a readable time-format.

If the computer is a musical orchestra, then the Oscillator is the drummer. It creates the rhythm that keeps the rest of the computer in sync: without the oscillator, the CPU would be lost.

Counting the ticks

When we tried to make our first countdown-timer, we used the simplest approach we could think of: a script counted the ticks created by the internal oscillator, and this was subtracted from the total duration. The result of this calculation was the countdown.
It seemed like an easy task to solve for our software. But to our surprise the countdowns created didn’t follow the accurate time displayed by our reference clocks: Our countdown would always run a little bit slower. Not much, maybe on an average 1 second offset every 10 minutes, but still unacceptable for a software aimed at accurate timings. And the longer the countdown lasted, the worse was the offset.
So we tried writing better code and run the program from different machines, with the same results. It was a little bit better with faster computers, but the offset was always there.
Then we realized that a software will never be able to count all the ticks generated from the oscillator inside the computer.

It’s a bit like asking your friend if he can count all the cars passing by the street: He can do it with one eye closed. But at some point the phone will ring, a cute girl passes by, or something else distracts his attention. Sooner or later he will miss some of the cars.

With a software there are a lot of distractions too. Even with a very simple countdown-application, the OS will sometimes ask the software to do something else, and take the eyes off the ticks for a little while. Or maybe the computer is run so hard that Windows decides that the CPU-power must be used for more important stuff. Whatever the reason, it is impossible for a software to count all the ticks, forever. When a tick is missed, the timer is delayed. And the longer the timer lasts, the more delay is accumulated. A clock that runs inside the software can never be completely accurate because there are too many things that disturbs the process.

Count on the System Clock

Luckily for us we don’t have to count ticks from the software itself. Instead we can use the System Clock. This is a device inside the computer that runs independently from the operating-system, it even runs when the computer is turned off! When you turn on the computer after it has been off for a while, it still shows the correct time of the day. That’s because the System Clock has its own battery-supply to keep it self-preserved while the computer is off.
The system-clock has one job only, and it does this extremely well: It counts the ticks created by the computer-oscillator and turns this into values that we can read as time. And although it hates disturbances, the clock can answer one simple question: “What time is it?”
The answer to this is all we need to create precise countdown-timers for Mac and Windows.

Countdowns in CueTimer

In CueTimer, when we create a countdown of 10 minutes, what happens is this:

  • When we start the countdown, we ask the system-clock what time it is. From this answer we add 10 minutes, and get the End Time. Then we repeatedly ask the system-clock the same question again, and we can call this value the Current Time. The countdown that is displayed is then: End Time – Current Time
  • When the End Time equals Current Time, the countdown has reached its end.

This approach gives the most accurate and robust countdown: Just like with counting ticks, we can’t completely trust that our software always remembers to do its job. But if CueTimer fails to ask the system-clock what time it is, then the countdown is only delayed until the next time the process succeeds. So, for example, if you want to use the computer for Bitcoin-mining while running CueTimer (not a very good idea) and the computer is almost running out of processing-power, CueTimer might respond slowly, and countdown might appear choppy on the output-windows, but the synchronization is not lost: When you turn off the mining and the computer can breathe again, the countdown continues as if nothing has happened. This is because it still uses the same End Time that we use to calculate the countdown.

Sending a countdown over the Internet

With CueTimer you can send the countdown to a remote computer over the Internet. Then a speaker in Australia can view the countdown on an Internet browser, while the CueTimer operator sits in Norway and controls the timer.
Sending the countdown over the Internet, and creating a countdown just for one computer, is not that different: The most important data that we share is still the End Time, which we have in UTC-format so that the timezone is irrelevant.  This value is sent to all the remote machines, and used to generate the countdown. The countdown that is shown is a little script that runs inside the remote browser. This means that Internet can fail and the countdown will still run on the remote machine, as long as the browser has all the data it needs to generate the countdown.

The method with sharing End Time is very robust, and it doesn’t require much data. But it has one great challenge: The clocks on the CueTimer host-computer, and the remote computer viewing the countdown, must be synchronized: If the current time on these two systems are not the same, then the countdown won’t show the correct time.

In CueTimer we take care of the synchronization ourselves. CueTimer asks our own server what time it is, works out how far off your computer’s clock is, and quietly corrects for the difference. Every machine viewing the countdown does the same thing against the same server, so they all line up on the exact same time — even if one of them has a clock that’s a few seconds out — and CueTimer keeps checking back now and then (and whenever a computer reconnects or wakes up) to stay in step. The nice part is you don’t have to think about any of this: whether your own clock is set perfectly or not, CueTimer sorts out the syncing for you.

In practice

Put together, CueTimer is quietly doing a fair bit of bookkeeping behind that single number on screen: it leans on your computer’s own clock to keep each countdown ticking smoothly, and checks against our server so every screen shows the same time. The result is that you can just trust the countdown — no fiddling with computer clocks or time settings required.

And the timing itself is rarely the hard part anyway. The trickier bit is usually the human side — like remembering to actually hit start when the speaker walks on. (I still manage to forget that one now and then.) And if a countdown ever drifts from where you want it, you can always nudge it faster or slower on the fly to bring it back — not the most scientific method, but often the easiest way to stay on schedule with CueTimer.

Explore Cuetimer
Download CueTimer