{"id":"7c7ac291b4973b95","repo":"apache/kafka","slug":"record-is-corrupt-stored-crc-computed-crc-7c7ac2","errorCode":null,"errorMessage":"Record is corrupt (stored crc = {}, computed crc = {})","messagePattern":"Record is corrupt \\(stored crc = (.+?), computed crc = (.+?)\\)","errorType":"exception","errorClass":"CorruptRecordException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/record/internal/LegacyRecord.java","lineNumber":136,"sourceCode":"    }\n\n    /**\n     * Returns true if the crc stored with the record matches the crc computed off the record contents\n     */\n    public boolean isValid() {\n        return sizeInBytes() >= RECORD_OVERHEAD_V0 && checksum() == computeChecksum();\n    }\n\n    /**\n     * Throw an CorruptRecordException if isValid is false for this record\n     */\n    public void ensureValid() {\n        if (sizeInBytes() < RECORD_OVERHEAD_V0)\n            throw new CorruptRecordException(\"Record is corrupt (crc could not be retrieved as the record is too \"\n                    + \"small, size = \" + sizeInBytes() + \")\");\n\n        if (!isValid())\n            throw new CorruptRecordException(\"Record is corrupt (stored crc = \" + checksum()\n                    + \", computed crc = \" + computeChecksum() + \")\");\n    }\n\n    /**\n     * The complete serialized size of this record in bytes (including crc, header attributes, etc), but\n     * excluding the log overhead (offset and record size).\n     * @return the size in bytes\n     */\n    public int sizeInBytes() {\n        return buffer.limit();\n    }\n\n    /**\n     * The length of the key in bytes\n     * @return the size in bytes of the key (0 if the key is null)\n     */\n    public int keySize() {\n        if (magic() == RecordBatch.MAGIC_VALUE_V0)","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/record/internal/LegacyRecord.java#L118-L154","documentation":"Thrown by LegacyRecord.ensureValid when the record is large enough to hold a CRC but the stored CRC does not match the value recomputed from the record body. This is the canonical on-disk corruption signal for message-format v0/v1 records; the message text prints both CRC values to aid diagnosis. It is a CorruptRecordException.","triggerScenarios":"Invoking LegacyRecord.ensureValid() (or isValid()) on a v0/v1 record whose bytes were altered after the CRC was written. Reached during log recovery, fetch validation, and produce-side validation of legacy-format batches.","commonSituations":"Disk bit-rot or controller/firmware bugs, silent data corruption from a faulty NIC or RAM, partial writes from unclean shutdown, or a producer that hand-rolled records without recomputing the CRC. Also seen after restoring from a backup that did not fsync.","solutions":["Run log recovery with the recovery-point checkpoint deleted so the broker truncates back to the last validated offset.","Restore the affected partition from a healthy in-sync replica; do not attempt to patch CRCs by hand.","Run filesystem-level checks (fsck/xfs_repair) and smartctl on the underlying device to find the source of corruption.","Move to message.format.version 2 (or higher) where batches have both CRC and length checks, and enable log.message.format.version interop carefully during upgrade."],"exampleFix":"// before: every consumed legacy record must pass strict CRC validation\nfor (LegacyRecord r : legacyRecords) r.ensureValid();\n\n// after: tolerate known-corrupt tail records during recovery, fail loud on the rest\nfor (LegacyRecord r : legacyRecords) {\n    try { r.ensureValid(); }\n    catch (CorruptRecordException e) {\n        if (recovery) log.warn(\"Skipping corrupt record at offset {} during recovery\", offset);\n        else throw e;\n    }\n}","handlingStrategy":"try-catch","validationCode":"if (!record.isValid()) {\n    log.warn(\"CRC mismatch on record: stored {}, computed {}; skipping\",\n             record.checksum(), record.computeChecksum());\n    continue;\n}","typeGuard":null,"tryCatchPattern":"try {\n    record.ensureValid();\n} catch (CorruptRecordException e) {\n    // disk or network bit-rot; quarantine the batch and advance past it\n    log.warn(\"Corrupt record (CRC mismatch)\", e);\n    consumer.seekToEnd(); // or quarantine segment for repair\n}","preventionTips":["Call record.isValid() before trusting record contents rather than relying solely on ensureValid().","Investigate recurring CRC failures: failing disk, bad NIC/cable, or a misbehaving producer.","Keep CRC validation enabled in the consumer; never disable it for throughput.","Replicate corrupt segments off the hot path for forensic comparison."],"tags":["legacy-record","crc","corruption","recovery","data-integrity"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}