Skip to the solution
MediumLow-Level Design6 min read

What is the purpose of the 'zlib' module in Node.js?

297 views

The problem

Question presented to candidate: "You want to compress an HTTP response body, or a file you are about to write to disk. Does Node need an external package for this, and how would you actually verify the compression is lossless?"

What a strong answer should cover:

  • zlib is a built-in Node module β€” no external dependency required β€” providing gzip, deflate, and modern Brotli compression/decompression, both as synchronous functions (gzipSync/gunzipSync) and as streams (zlib.createGzip()/createGunzip()), fitting directly into the .pipe()/stream.pipeline() patterns covered in their own dedicated questions.
  • πŸ“Œ Verified, not assumed: compressing 100,000 bytes of highly repetitive data produced a 132-byte result (a 99.9% reduction), and decompressing that result reproduced the exact original bytes, confirmed via a byte-for-byte buffer comparison β€” real, measured evidence that the round trip is genuinely lossless, not merely described as such.
  • The stream-based API (zlib.createGzip()) is the natural fit for the large-file/HTTP-response use cases covered in the dedicated large-files-with-streams question β€” compressing data as it flows through a pipe chain, with the same flat memory profile, rather than requiring the entire payload in memory first to compress it in one synchronous call.
  • gzip/deflate and Brotli (brotliCompressSync/brotliDecompressSync, or their streaming equivalents) are genuinely different algorithms with different trade-offs β€” Brotli often achieves a smaller output for the same input at the cost of somewhat higher compression time, which is why HTTP content negotiation (the Accept-Encoding/Content-Encoding headers) lets a client and server agree on which one to actually use for a given exchange.
  • A precise answer names the common real use case beyond raw file/response compression: many HTTP frameworks and reverse proxies use zlib (directly or via a middleware) to implement response compression transparently, which is why an application developer often does not call zlib directly at all β€” it operates one layer below, inside the framework or infrastructure.
  • zlib's compression is not a substitute for encryption β€” compressed data is still fully readable/reversible by anyone with the compressed bytes; confidentiality is a separate concern addressed by the crypto module, covered in its own dedicated question.

Clarifying questions expected:

  • "Is this for compressing an HTTP response, a file being written to disk, or an arbitrary in-memory buffer?" β€” decides between the streaming API and the synchronous one-shot functions.
  • "Does the consuming client support Brotli, or should this stick to the more universally-supported gzip?" β€” a real, practical HTTP compatibility question.

Code / implementation expected: Yes β€” a real gzip round trip with the actual compressed size and a verified byte-for-byte match after decompression is the concrete, convincing proof of both the compression ratio and its losslessness.

  • zlib
  • compression
  • performance

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 10 sections

Read the code

JavaScriptA real gzip round trip: 100,000 bytes compressed to 132, decompressed back byte-for-byte identical
const zlib = require("zlib");

const original = Buffer.from("x".repeat(100000)); // 100,000 bytes
const compressed = zlib.gzipSync(original);
console.log("original:", original.length, "compressed:", compressed.length);
// original: 100000 compressed: 132

const decompressed = zlib.gunzipSync(compressed);
console.log("round-trip correctness:", decompressed.equals(original)); // true

// The streaming equivalent, fitting directly into a pipe chain:
// fs.createReadStream(src).pipe(zlib.createGzip()).pipe(fs.createWriteStream(dest + ".gz"));

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 100 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

How do you consume a readable stream with `for await...of` versus the `data` event?

Continue