Why the same CSV can produce a different gzip archive on every run
A retry can produce a different archive even when every order is unchanged. A small Python experiment isolates the timestamp and filename and checks both the compressed bytes and the restored data.
In this article
A retry in the overnight export
A shop exports orders to accounting every night. Two rows appear in the export, with the same values as the first attempt. The sender did not receive a confirmation, so the job runs again. Its storage key includes a SHA-256 digest of the compressed file. Instead of finding the same key, the job stores a second object. Both archives open normally. Three notions of equality are involved. The original CSV may contain the same bytes. The gzip archive may contain the same bytes including its header. The recipient may interpret the rows as the same business records. Each statement requires a different test. Our aim is reproducible archive bytes from a fixed input within a known environment.
Two fields outside the payload
A gzip member carries a header before the compressed data. RFC 1952 defines a four-byte MTIME field. A zero value indicates that a timestamp is unavailable. The header can also carry an original filename. Depending on how a file object is constructed, a useful local filename can become part of an archive that was meant to have a stable identity. The example therefore supplies a byte buffer as the destination and chooses the header filename explicitly. Raising the compression level cannot reconcile two different timestamps. A digest over the complete file covers the header as well as the payload, regardless of whether a recipient cares about the original filename or time.
Five checks on two orders
The program builds two fictional orders with integer cent amounts. It compresses them first with two adjacent, explicit timestamps. No sleep or wall-clock assumption is involved. It then builds two archives with an empty filename and a zero timestamp. Another variant changes only the original filename, while a final variant increases the second order total by one cent. Every comparison answers a separate question. Our run printed True on all five lines. The first comparison checks the restored bytes from the two dated archives. The final assertion independently checks the restored bytes from the reproducible variant. Changing one input at a time makes each result interpretable.
from gzip import GzipFile, decompress
from hashlib import sha256
from io import BytesIO
def packed(payload, *, stamp, label=""):
target = BytesIO()
with GzipFile(filename=label, mode="wb", fileobj=target,
mtime=stamp, compresslevel=6) as stream:
stream.write(payload)
return target.getvalue()
data = b"order_id,total_cent\n17,1250\n18,399\n"
first = packed(data, stamp=1700000000)
later = packed(data, stamp=1700000001)
stable = packed(data, stamp=0)
retry = packed(data, stamp=0)
renamed = packed(data, stamp=0, label="orders.csv")
changed = packed(data.replace(b"399", b"400"), stamp=0)
print("same_input:", decompress(first) == decompress(later) == data)
print("clock_changes_bytes:", first != later)
print("same_digest:", sha256(stable).digest() == sha256(retry).digest())
print("name_changes_bytes:", renamed != stable)
print("new_value_changes_bytes:", changed != stable)
assert decompress(stable) == data
# Expected output
# same_input: True
# clock_changes_bytes: True
# same_digest: True
# name_changes_bytes: True
# new_value_changes_bytes: True
Choose the identity that matters
The equal digest measures reproducibility of this compressed object in this experiment. A recipient concerned with business content might instead compare restored CSV bytes or an agreed representation of the records. An extra trailing newline can change the file without changing the number of orders. A different row order also changes the bytes. Sorting is appropriate only when the parties agree that order has no business meaning. Silently discarding repeated order identifiers can lose information. Encoding, line endings, column order and monetary units are useful parts of a written format agreement. Successful decompression alone does not resolve any of those choices.
Finish writing before hashing
The code reads the output buffer after the compressor’s context has closed. Before that point, pending bytes and the gzip trailer may still be missing. A larger exporter can write chunks to a temporary file, close the compressor and then read the completed file to calculate its digest. The existence of a temporary file after an error does not prove that the export finished. Two workers writing the same storage key may also need conditional writes and a job receipt. Stable bytes can prevent a redundant upload. Evidence that the recipient actually processed the order batch still has to come from the receiving side. For example, a receipt can keep the archive digest and the accounting system’s processing identifier in separate fields. Each is evidence for a different step.
Preserve the right regression test
When changing the compressor or runtime, keep a fixture that can be decompressed and checked against its expected records. Separately generate two new outputs from known input in the new environment and compare them. If a contract also promises byte-for-byte compatibility with the old compressor, compare against an archived golden digest as a third test. Two repeatable runs do not establish stability across every implementation or version. That is why the example prints comparisons instead of promising a universal digest. Keep the actual export time in a job receipt when it matters to the business. Recording the runtime, compression settings and input format makes later investigations considerably easier.
Reader discussion
Which bottleneck would you expect as this solution scales?
No comments yet. Be the first to contribute.
Write a comment