Definition
An event sequence number is a counter value that a producer of events, such as a device, attaches to each event it records, so that events can be put in order and gaps noticed. The counter starts again after a reboot and wraps around to 0 after its largest value, so the number identifies an event only within one run of the counter. An epoch is a value that names that run, such as a count of boots. An event’s identity is the triple of producer, epoch and sequence number.
The idea
A device numbers its events 0, 1, 2 and so on, keeping the counter in RAM. It records event 7, a temperature alarm, and then loses power. RAM loses its content without power, so after the restart the counter begins again at 0, and the device later records a new event 7, an installed update. A collector that files events by sequence number now holds two events 7. If it discards repeats, it throws away the update as a duplicate of the alarm. If it pairs each “start” event with the “end” event of the same number, it can pair a start from before the power loss with an end from after it, and report a duration that never happened.
The number 7 is the same in both cases; what differs is the run of the counter, and an epoch names that run. Page numbers in an encyclopedia of several volumes are a fair picture: page 7 exists in every volume, so a reference gives the volume and the page. The picture stops in one place. A publisher never prints two volumes with the same number, but a device that forgets its state can repeat an epoch unless the design rules this out.
How it works
State the scope of the counter: which producer owns it, and which events restart it, such as power-on, reset, firmware update, restore from a backup, factory reset, and wraparound.
Choose an epoch that changes whenever the counter can restart, from one of the sources listed below.
Attach the epoch to every event, or to a session header that every later event refers to.
Handle wraparound: make the counter wide enough that it cannot wrap within one epoch, or start a new epoch when it wraps.
Match on the full identity. Every consumer, such as a collector, a duplicate filter or an analysis script, compares the whole triple (producer, epoch, sequence number). Equal triples are copies of one event, for example an event that a radio link sent twice; different triples are different events.
Three sources of an epoch are common.
A persistent boot counter, kept in non-volatile memory such as flash memory §6.6, which keeps its content without power. At every start the device reads the count, adds 1, writes the new value back and waits for the write to complete, all before it records its first event; otherwise a power loss before the write lets the next start reuse the epoch. The boot counter is a monotonic counter §8.28, a value that only ever increases, so it also orders the runs. It costs one flash write per boot, and 32 bits allow boots.
A random epoch, drawn at each start from a good random number generator. It needs no stored state, but two runs can draw the same value by chance, and it does not order the runs.
A clock reading at start. This works only if the clock keeps running while the device is off, never moves backwards and never gives two starts the same reading. A clock with no battery to keep it running restarts from zero at every start, so every run gets the same epoch.
Worked example
Device uses a persistent boot counter as its epoch and an 8-bit sequence number, which holds the values 0 to 255. In its 41st boot it records an alarm as . It loses power, boots for the 42nd time and records an update as , which the radio link then delivers twice.
A collector keyed on the sequence number alone holds one record for the key 7. It keeps either the alarm or the update, and one real event is lost.
A collector keyed on the triple keeps and as two events, and drops only the second copy of , which is a true duplicate.
Wraparound. In boot 42 the device records 300 events, in positions 0 to 299. The 8-bit counter has values, so the event in position 256 gets the number , where is the remainder when is divided by . Positions 256 to 299 reuse the numbers 0 to 43, so triples each name two events. A 32-bit counter at 1000 events per second wraps after seconds, about 49.7 days; a 64-bit counter at that rate wraps after about 585 million years.
Random epochs. Let each boot draw a 32-bit random epoch, one of equally likely values. By the birthday bound §5.6, the probability that some two of boots draw the same value is about , where is the base of natural logarithms. For , and , so the exponent is about and the probability about 0.0116, a 1.2% chance. With 64-bit epochs it is about .
Stated precisely
Let an event carry the producer’s identifier , the epoch and the sequence number , and let .
Uniqueness: two different events of one producer have different identities. This holds if (a) the producer never uses one epoch for two runs of its counter, and (b) the counter never repeats a value within one epoch; a -bit counter satisfies (b) for epochs of at most events.
Order within an epoch: for two events with the same producer and epoch and sequence numbers and , if the counter did not wrap, exactly when the first event happened before the second.
Order across epochs exists only when epochs are ordered, as boot counts are: comes before when , or when and .
Duplicate detection follows from uniqueness: two received records describe the same event exactly when their triples are equal, provided the transport does not alter the fields. Condition (a) rests on named assumptions. For a boot counter: the new value is stored durably before the first event, and no restore or reset brings back an old value. For a random epoch: the random source is good, and the application accepts the collision probability given by the birthday bound.
Limits and common mistakes
A timestamp is not an identity. A coarse clock gives two events the same time, and a clock that restarts at zero, or that clock synchronization §9.36 sets backwards, gives different events equal or reversed times.
An epoch is not authentication §1.6: anyone who can inject records can write any epoch. When records cross a trust boundary §1.3, a message authentication code §5.7 (MAC), a short value computed from the record with a secret key, must cover producer, epoch and sequence number together. Nor do distinct identities stop a replay attack §1.26, which resends a recorded message with its valid triple; rejecting it needs state at the receiver, such as the newest triple accepted from each producer.
An epoch can come back. A factory reset that erases the boot counter, a restored copy of the flash contents, or a cloned device starts again from an old value. Adding a random value drawn afresh at every factory reset keeps epochs distinct across resets, up to the birthday-bound collision probability.
Without an epoch, two values of a wrapping counter can still be compared, but only when they are less than half the counter’s range apart: that is the rule of serial-number arithmetic §19.3.4, defined in RFC 1982.
An epoch for identity is not an epoch for authority. In epoch fencing §19.3.2, the protected resource stores the newest epoch it has seen and rejects requests that carry an older one; an identity scheme performs no such check.
Distinct identities say which records are different events, not which are independent observations. Events from one boot share its conditions, so in a statistical analysis the boot, not the event, may be the experimental unit §11.3: the thing to which a condition was independently applied.
Questions answered
What makes event 7 after reboot different from event 7 before reboot?
The epoch makes them different: a value that names the run of the event counter and changes at every reboot. The sequence number 7 is the same in both cases, because the counter starts again from 0 after the reboot. An event’s full identity is the triple (producer, epoch, sequence number); for a device whose epoch is its boot count, event 7 before the reboot is, for example, and event 7 after it is .
This works only if the epoch never repeats. A persistent boot counter must be incremented and stored before the first event of the boot, and a random epoch must be long enough that a repeat is negligible: with 64-bit random epochs, the probability of any repeat among 10,000 boots is about . Every consumer must compare the whole triple when it matches events or removes duplicates. A timestamp is no substitute, because a clock can restart or move backwards across a reboot.
Related entries: Counter wraparound (§6.21), Lease and exclusive ownership (§13.26), Experimental unit (§11.3), Exactly-once effect (§8.32), Monotonic counter (§8.28), Birthday bound (§5.6), Replay attack (§1.26), Epoch fencing and stale authority (§19.3.2), Serial-number arithmetic and the half-range ambiguity (§19.3.4). Further reading: [154].