Zmniejszanie przepustowości dzięki kompresji wiadomości
Zmniejszą Państwo zużycie pasma WebSocket za pomocą rozszerzenia permessage-deflate i przemyślanego projektowania payloadów oraz poznają kompromisy dotyczące CPU i pamięci.
Zmniejszanie przepustowości dzięki kompresji wiadomości to bezpłatna lekcja WebSockets & Real-Time Systems with Spring na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej WebSockets & Real-Time Systems with Spring, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs WebSockets & Real-Time Systems with Spring zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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
Często zadawane pytania
Czy lekcja „Zmniejszanie przepustowości dzięki kompresji wiadomości” jest bezpłatna?
Tak — pełny tekst „Zmniejszanie przepustowości dzięki kompresji wiadomości” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu WebSockets & Real-Time Systems with Spring, przejdź na CoddyKit PRO. Kurs WebSockets & Real-Time Systems with Spring zawiera 4 lekcji w sumie.
Co nauczysz się w „Zmniejszanie przepustowości dzięki kompresji wiadomości”?
Zmniejszą Państwo zużycie pasma WebSocket za pomocą rozszerzenia permessage-deflate i przemyślanego projektowania payloadów oraz poznają kompromisy dotyczące CPU i pamięci. Ćwiczysz WebSockets & Real-Time Systems with Spring z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć WebSockets & Real-Time Systems with Spring?
Nie wymagamy żadnego doświadczenia. WebSockets & Real-Time Systems with Spring w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Zmniejszanie przepustowości dzięki kompresji wiadomości”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji WebSockets & Real-Time Systems with Spring?
Tak. Każda lekcja WebSockets & Real-Time Systems with Spring zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Testowanie wydajności WebSocket
- Monitorowanie połączeń WebSocket
- Dostrajanie ustawień Spring WebSocket
- Zmniejszanie przepustowości dzięki kompresji wiadomości