Store the same data in less space, by squeezing it or by keeping only one copy of repeats.
Compression and deduplication are two ways to store the same information in less space.
Real data is full of repeats. A log file repeats the same words on every line. A backup copied every night repeats most of yesterday's backup. Compression and deduplication are tools that notice those repeats and stop paying for them twice.
Compression works inside one piece of data. A simple idea is run-length encoding: write AAAAAAAAAA as "10 Γ A". Real codecs are smarter. They find repeated patterns anywhere, point back to the earlier copy, and then use shorter codes for common symbols. Deduplication works across many pieces. It cuts files into chunks, computes a fingerprint (a hash) of each chunk, and stores a chunk only if that fingerprint is new.
| Kind | Examples | What you get | Use for |
|---|---|---|---|
| Lossless, fast | LZ4, Snappy | Moderate shrinking, very high speed, low CPU | Hot data, databases, message batches |
| Lossless, strong | gzip (deflate), zstd, bzip2, xz | Better shrinking, more CPU (zstd can be tuned between fast and strong) | Logs, backups, files sent over the network, cold data |
| Lossy for media | JPEG, MP3, video codecs | Large savings by dropping detail people barely notice | Images, audio and video |
input : AAAAAAAAAABBBBCC runs : 10 x A, 4 x B, 2 x C output: 10A 4B 2C (shorter, and fully reversible)
Real codecs such as gzip, zstd and LZ4 use a more general idea: when a pattern appeared earlier, replace it with a short pointer to the earlier copy ("go back 40 bytes and copy 12"). Some then also give common symbols shorter codes.
| Level | What is compared | Good and bad |
|---|---|---|
| File-level | Whole files by hash | Simple and cheap. Misses files that differ by one byte. |
| Fixed-size block | Equal-sized blocks | Fast to compute. An insertion near the start shifts every block. |
| Content-defined chunks | Chunks cut where the content says so (rolling hash) | Survives insertions well, but costs more CPU and has variable chunk sizes. |
Teaching toy: run-length encoding only spots the same character repeated. Real codecs (gzip, zstd, LZ4, Snappy) find repeated patterns anywhere in the data and then code the result compactly, so they do far better on text. Already-compressed or encrypted data looks random and barely shrinks at all, as the "Random-looking" example shows.
| Term | Meaning in easy words |
|---|---|
| Compression | Storing data in fewer bytes by removing repetition |
| Lossless | Compression that restores the exact original data |
| Lossy | Compression that drops some detail for a much smaller size |
| Compression ratio | How much smaller the data became, e.g. compressed size divided by original size |
| Run-length encoding | Replacing a run of the same symbol with a count and the symbol |
| Dictionary coding | Replacing repeated patterns with references to earlier copies |
| Deduplication | Keeping one copy of identical data and referencing it from every use |
| Chunk | A piece of a file used as the unit of deduplication |
| Fingerprint | A hash of a chunk used to detect whether it has been seen before |
| Content-defined chunking | Cutting chunks at points decided by the data, not by position |
| Reference counting | Counting how many files use a chunk, so it is removed only when none do |
| Choice | We gain | We lose |
|---|---|---|
| Stronger compression | Smaller data | More CPU and slower writes or reads |
| Faster compression | Low latency and CPU | Larger data |
| Lossy compression | Very small media files | Exact originals can never be restored |
| Deduplication | Big savings when data repeats | Index memory, CPU and a shared-chunk risk |
| Small chunks | More matches found | A bigger index and more references |
| Large chunks | A small index and fast processing | Fewer matches, as small changes break whole chunks |