Reducing Bandwidth with Message Compression
Cut WebSocket bandwidth using the permessage-deflate extension and smart payload design, and understand its CPU/memory trade-offs.
Reducing Bandwidth with Message Compression is a free WebSockets & Real-Time Systems with Spring lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebSockets & Real-Time Systems with Spring learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Bandwidth Is a Bottleneck
Real-time apps can push thousands of frames per second. On mobile and high-fan-out systems, raw bytes on the wire become a real cost in latency, data charges, and egress bills.
The permessage-deflate Extension
WebSocket defines an extension, permessage-deflate, that compresses each message with DEFLATE (the same algorithm as gzip) before it is sent and decompresses it on arrival.
Negotiated in the Handshake
Compression is negotiated via the Sec-WebSocket-Extensions header during the upgrade. If both peers agree, frames are compressed transparently afterward.
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bitsWhere Compression Wins
Compression helps most with text and JSON, which are highly repetitive. A 4 KB JSON payload can shrink to under 1 KB.
Already-compressed data (images, video, gzip) barely shrinks and just wastes CPU.
The CPU Trade-off
Compression is not free. Every message costs CPU on both sides. Under very high message rates the CPU spent compressing can outweigh the bandwidth saved.
The Context-Takeover Memory Cost
DEFLATE keeps a sliding-window dictionary per connection (context takeover). With many connections this is significant memory. Disabling takeover saves RAM but lowers the compression ratio.
Enabling in Spring (Tomcat)
Spring delegates to the servlet container. On Tomcat you tune the WebSocket compression via container properties or a customizer.
server.tomcat.use-relative-redirects=false
# Tomcat enables permessage-deflate when the client offers it;
# tune limits via WsServerContainer / @Bean customizerPer-Message Skip for Small Frames
Compressing tiny frames adds overhead with little gain. Many stacks skip compression below a threshold (e.g. messages under 256 bytes). Keep this in mind when designing payload sizes.
Application-Level Alternatives
Before reaching for transport compression, shrink the payload itself:
- Send deltas, not full snapshots
- Use short field names or a binary format (Protobuf, MessagePack)
- Batch many small updates into one frame
Measuring the Benefit
Always measure. Compare bytes-on-wire and CPU before and after enabling compression for your real traffic mix; do not assume it is always a win.
# Compare with monitoring metrics
websocket.bytes.sent (deflate on vs off)
process.cpu.usageChoosing a Strategy
Rules of thumb:
- High-volume JSON, bandwidth-bound → enable permessage-deflate
- Many idle connections, memory-bound → disable context takeover
- CPU-bound, small frames → prefer application-level payload shrinking
Quick Check
Test your compression knowledge.
Recap
You optimized bandwidth:
- permessage-deflate compresses each message, negotiated in the handshake
- Best for repetitive text/JSON; useless for already-compressed data
- It trades CPU and per-connection memory (context takeover) for fewer bytes
- Application-level deltas, binary formats, and batching are complementary
- Always measure before and after for your real traffic
Frequently asked questions
Is the “Reducing Bandwidth with Message Compression” lesson free?
Yes — the full text of “Reducing Bandwidth with Message Compression” is free to read here on the web, and the WebSockets & Real-Time Systems with Spring course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebSockets & Real-Time Systems with Spring course, upgrade to CoddyKit PRO.
What will I learn in “Reducing Bandwidth with Message Compression”?
Cut WebSocket bandwidth using the permessage-deflate extension and smart payload design, and understand its CPU/memory trade-offs. You practise WebSockets & Real-Time Systems with Spring with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebSockets & Real-Time Systems with Spring?
No prior experience is required. WebSockets & Real-Time Systems with Spring on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reducing Bandwidth with Message Compression” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebSockets & Real-Time Systems with Spring lesson?
Yes. Every WebSockets & Real-Time Systems with Spring lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Benchmarking WebSocket Performance
- Monitoring WebSocket Connections
- Tuning Spring WebSocket Settings
- Reducing Bandwidth with Message Compression