Section 15.36

Event epoch and sequence identity

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

  1. 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.

  2. Choose an epoch that changes whenever the counter can restart, from one of the sources listed below.

  3. Attach the epoch to every event, or to a session header that every later event refers to.

  4. Handle wraparound: make the counter wide enough that it cannot wrap within one epoch, or start a new epoch when it wraps.

  5. 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.

Worked example

Device DD 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 (D,41,7)(D, 41, 7). It loses power, boots for the 42nd time and records an update as (D,42,7)(D, 42, 7), which the radio link then delivers twice.

Wraparound. In boot 42 the device records 300 events, in positions 0 to 299. The 8-bit counter has 28=2562^8 = 256 values, so the event in position 256 gets the number 256mod256=0256 \bmod 256 = 0, where amodma \bmod m is the remainder when aa is divided by mm. Positions 256 to 299 reuse the numbers 0 to 43, so 300−256=44300 - 256 = 44 triples each name two events. A 32-bit counter at 1000 events per second wraps after 232/1000≈4,294,9672^{32} / 1000 \approx 4{,}294{,}967 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 N=232N = 2^{32} equally likely values. By the birthday bound §5.6, the probability that some two of kk boots draw the same value is about 1−e−k(k−1)/(2N)1 - e^{-k(k-1)/(2N)}, where e≈2.718e \approx 2.718 is the base of natural logarithms. For k=10,000k = 10{,}000, k(k−1)=99,990,000k(k - 1) = 99{,}990{,}000 and 2N=8,589,934,5922N = 8{,}589{,}934{,}592, so the exponent is about −0.01164-0.01164 and the probability about 0.0116, a 1.2% chance. With 64-bit epochs it is about 2.7×10−122.7 \times 10^{-12}.

Stated precisely

Let an event ee carry the producer’s identifier dd, the epoch ϵ\epsilon and the sequence number ss, and let id(e)=(d,ϵ,s)\mathrm{id}(e) = (d, \epsilon, s).

  1. 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 ww-bit counter satisfies (b) for epochs of at most 2w2^w events.

  2. Order within an epoch: for two events with the same producer and epoch and sequence numbers s1s_1 and s2s_2, if the counter did not wrap, s1<s2s_1 < s_2 exactly when the first event happened before the second.

  3. Order across epochs exists only when epochs are ordered, as boot counts are: (ϵ1,s1)(\epsilon_1, s_1) comes before (ϵ2,s2)(\epsilon_2, s_2) when ϵ1<ϵ2\epsilon_1 < \epsilon_2, or when ϵ1=ϵ2\epsilon_1 = \epsilon_2 and s1<s2s_1 < s_2.

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 DD whose epoch is its boot count, event 7 before the reboot is, for example, (D,41,7)(D, 41, 7) and event 7 after it is (D,42,7)(D, 42, 7).

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 2.7×10−122.7 \times 10^{-12}. 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].