# Nanosecond timestamp collisions are common

DevFeed: [Nanosecond timestamp collisions are common](<https://devfeed.tech/articles/nanosecond-timestamp-collisions-are-common-20754.md>)

Original publisher: [Read original article](<https://www.evanjones.ca/nanosecond-collisions.html>)

Published: 2023-07-20T21:39:42Z

Content type: article

Language: en

Sources: [Evan Jones](<https://devfeed.tech/sources/evan-jones.md>)

Topics: [DateTime](<https://devfeed.tech/topics/datetime.md>), [Go Language](<https://devfeed.tech/topics/go-language.md>)

Tags: [go](<https://devfeed.tech/tags/go.md>), [threads](<https://devfeed.tech/tags/threads.md>), [time](<https://devfeed.tech/tags/time.md>)

## AI overview

The article reports tests of timestamp collisions using Go's absolute and monotonic clocks. It finds that raw nanosecond timestamps can collide frequently across threads and that collision behavior varies between Linux and Mac OS X.

## Source excerpt

I was wondering: how often do nanosecond timestamps collide on modern systems? The answer is: very often, like 5% of all samples, when reading the clock on all 4 physical cores at the same time. As a result, I think it is unsafe to assume that a raw nanosecond timestamp is a unique identifier. I wrote a small test program to test this. I used Go, which records both the "absolute" time and the "monotonic clock" relative time on each call to time.Now(), so I compared both the relative difference between consecutive timestamps, as well as just the absolute timestamps. As expected, the behavior depends on the system, so I observe very different results on Mac OS X and Linux. On Linux, within a single thread, both the absolute and monotonic times always increase. On my system, the minimum increment was 32 ns. Between threads, approximately 5% of the absolute times were exactly the same as other threads. Even with 2 threads on a 4 core system, approximately 2% of timestamps collided. On Mac OS X: the absolute time has microsecond resolution, so there are an astronomical number of collisions when I repeat this same test. Even within a thread I often observe the monotonic clock not increment. See the test program on Github if you are curious.