The symptom and three explanations#
The example is fictional. During an incident review, an engineer filtered the logs for one request and saw orders received request about 400 ms before gateway sent request, and orders replied before the gateway's call even began. Every request from 14:02 UTC onward looks the same. The services run on different hosts and ship logs through agents to one log store. Three explanations fit:
- Clock skew. The
ordershost's clock reads about 400 ms behind the gateway's, so every time it records is early. - A late timestamp. The view shows and sorts by a time assigned after the event: when the agent shipped a batch, or when the log store received it. The gateway's agent sends every five seconds, the orders agent every second, so orders lines arrive first.
- A text sort across formats. The view sorts timestamps as strings, and one service writes
+01:00offsets while the other writesZ. Sorting RFC 3339 strings as text gives time order only when every string uses the same zone notation and the same number of fractional digits. RFC 3339 ordering
Clock skew is a host problem that also breaks certificate checks and token expiry. The other two are problems in the log pipeline or the view. Choose the observation where explanations disagree
What each explanation predicts#
| Observation | Clock skew | Late timestamp | Text sort |
|---|---|---|---|
| Order in the view | impossible | impossible | impossible |
| Order of one request's lines within one host | correct | correct | correct |
| Same lines sorted by each process's event time, parsed as time | still impossible | correct | correct |
| Clock offset implied by every request and reply pair | one interval, excluding zero | an interval containing zero | an interval containing zero |
| The host's own clock offset from its time source | large | small | small |
| Gap between event time and receive time | small | varies with each batch | small |
The third row separates skew from both pipeline explanations. The fourth reads the skew from the logs themselves, without access to the hosts: a request cannot arrive before it was sent, and a reply cannot be received before it was sent, so each pair limits how far apart the two clocks can be.
The lab: two worlds, 200 requests#
The script simulates 200 requests from gateway to orders, each producing four log records with an event time, read from the host's clock, and an ingest time, when that host's agent shipped its batch. In the skew world, the orders clock reads 400 ms behind and the view sorts by event time. In the late-stamp world, both clocks are right and the view sorts by ingest time. For each world it counts the requests shown in an impossible order under each field, and computes the range of clock offsets that every request and reply pair allows. It needs Python 3.6 or later.
import math, random, sys
if len(sys.argv) < 2 or sys.argv[1] not in ("skew", "late-stamp"):
sys.exit("usage: python3 order.py skew|late-stamp [seed]")
world = sys.argv[1] # skew | late-stamp
rng = random.Random(int(sys.argv[2]) if len(sys.argv) > 2 else 4)
SKEW = -400 if world == "skew" else 0 # ms the orders host's clock reads behind true time
FLUSH = {"gateway": 5000, "orders": 1000} # each host's shipper sends a batch this often (ms)
records = [] # (request, host, event, event_time, ingest_time)
def emit(req, host, event, true_t):
clock = true_t + (SKEW if host == "orders" else 0)
ingest = math.ceil(true_t / FLUSH[host]) * FLUSH[host] + 30
records.append((req, host, event, clock, ingest))
for i in range(200):
sent = i * 50 + rng.uniform(0, 10)
recv = sent + rng.uniform(1, 3) # network
reply = recv + rng.uniform(20, 40) # orders does the work
got = reply + rng.uniform(1, 3)
emit(i, "gateway", "sent", sent); emit(i, "orders", "received", recv)
emit(i, "orders", "replied", reply); emit(i, "gateway", "got reply", got)
def view(ts_field): # timestamp per (request, event) as a log view sorts them
return {(r[0], r[2]): r[ts_field] for r in records}
def impossible(t): # orders saw it before the gateway sent it, or similar
return sum(t[i, "received"] < t[i, "sent"] or t[i, "got reply"] < t[i, "replied"] for i in range(200))
def offset_bounds(t): # orders' clock minus the gateway's, from each request/reply pair
low = max(t[i, "replied"] - t[i, "got reply"] for i in range(200))
high = min(t[i, "received"] - t[i, "sent"] for i in range(200))
return low, high
event, ingest = view(3), view(4)
print(f"world={world}")
print(f" impossible order sorted by event_time: {impossible(event):>3} of 200")
print(f" impossible order sorted by ingest_time: {impossible(ingest):>3} of 200")
low, high = offset_bounds(event)
print(f" orders' clock offset that fits every event_time pair: {low:.1f} to {high:.1f} ms")
Save it as order.py. Before running it, write down for each world how many requests will look impossible under event time, and whether the offset range will include zero. Then run python3 order.py skew; python3 order.py late-stamp. Python 3.11 printed:
world=skew
impossible order sorted by event_time: 200 of 200
impossible order sorted by ingest_time: 160 of 200
orders' clock offset that fits every event_time pair: -401.0 to -399.0 ms
world=late-stamp
impossible order sorted by event_time: 0 of 200
impossible order sorted by ingest_time: 160 of 200
orders' clock offset that fits every event_time pair: -1.0 to 1.0 ms
Both views showed impossible orders: 200 requests in the skew world's event-time view, 160 in the late-stamp world's ingest-time view. Sorting by event time removed every impossible order in the late-stamp world and none in the skew world. The pairs bounded the skew world's offset to between 401 and 399 ms behind, and the late-stamp world's to within 1 ms of zero. Ingest time misordered 160 requests in both worlds, because it depends on when each agent ships, not on the clocks.
The prediction fails if sorting by event time leaves impossible orders in the late-stamp world, or if the skew world's offset range includes zero. Change SKEW to -2 and run it again: 93 of 200 requests are still misordered, and the range, -3.0 to -1.0 ms, still excludes zero. At -0.5, below the simulated network's 1 ms minimum delay, nothing is misordered and the range, -1.5 to 0.5 ms, includes zero: a skew smaller than the gap between two related events cannot reverse them.
Read it on a real system#
Find both times on each line. These queries only read. In CloudWatch Logs Insights, @timestamp is the event's timestamp as the producer sent it, and @ingestionTime is when CloudWatch Logs received it. Logs Insights fields
fields @timestamp, @ingestionTime, @logStream, @message
| filter @message like /req-7c1f/
| sort @timestamp asc
In Cloud Logging, a LogEntry has timestamp, the time the event occurred, which Logging fills with the current time if the writer omits it, and receiveTimestamp, when Logging received the entry. A writer that omits timestamp gets a late one. LogEntry
gcloud logging read 'jsonPayload.request_id="req-7c1f" AND timestamp>="2026-11-20T14:00:00Z" AND timestamp<"2026-11-20T14:10:00Z"' \
--project PROJECT --order asc \
--format 'table(timestamp, receiveTimestamp, resource.labels.container_name, jsonPayload.message)'
The OpenTelemetry log data model carries the same two fields, Timestamp from the origin clock and ObservedTimestamp from the collection system, and recommends using Timestamp when present. A pipeline that drops Timestamp falls back to observation time. Logs data model
For the hosts' clocks, chronyc tracking shows System time, the current offset between chrony's NTP clock and the system clock, and timedatectl status shows whether the system clock is synchronised. Measure the host's clock Run them on both hosts. Traces give the same check as the lab's pairs: a child span on one host belongs inside its parent span on another, so a child that starts before its parent in the trace view means the two hosts' clocks disagree by at least that much. Traces
Limits#
Pairs bound the skew only as tightly as the fastest round trip. A skew smaller than the network delay leaves a range that includes zero, but it cannot produce the impossible order either.
Skew changes over time. The realtime clock that log timestamps read is affected by discontinuous jumps when it is set, and by the gradual adjustments a time daemon makes, so a host that stepped its clock at 14:02 shows a different offset before and after. Compute the bounds over short windows. clock_gettime
"Event time" can itself be late. A logger that stamps the record when it formats or writes it, after an asynchronous queue, records the write time; a service that logs with whole-second precision makes the order within one second arbitrary. Both look like skew in a single pair and like noise across many.
Two hosts can both be wrong by the same amount. The pairs measure the offset between them, not their distance from true time, which only the hosts' time sources can show.
Questions#
Which timestamp should I sort logs by during an incident?
Sort by the event time the producing process recorded, and treat order across hosts as reliable only to within their clock offset. For causality across services, follow request IDs and parent-child links instead of timestamps: a reply follows its request whatever the clocks say.
Can hosts synchronised by NTP still misorder logs?
Yes, when two events on different hosts are closer together than the hosts' clock offset. Read the offset on each host with chronyc tracking and compare it with the gap between the misordered events: a gap smaller than the offsets is explained by them, and a larger one needs another explanation.
Why does a trace show a child span starting before its parent?
The two spans were timed by different hosts' clocks, and the child's host is behind. The amount by which the child starts early is a lower bound on the skew. Fix the clock; the trace's structure, which comes from the parent and child IDs, is still correct.