← Blog

Date:Thu, 19 Nov 2026 From:Thomas Gumz <thomas@lograker.app> To:You Subject:

Adding compression to swift-nio-ssh

The other day I sat down to figure out where LogRaker actually spends its time when a lot of log lines come in over SSH. LogRaker talks SSH through its own fork of swift-nio-ssh (the RSA and jump host posts explain why there’s a fork at all), so I went through that code first, fully expecting the encryption to show up as the expensive part.

It didn’t. The benchmark that ships with swift-nio-ssh pushed 65 MB of 32 KB messages through encryption and decryption in 0.058 seconds, which is about 1.1 GB a second. No log file I know of comes anywhere near that, so as far as CPU goes, the library is basically free.

What did show up was something it doesn’t do at all.

Every byte went out uncompressed

SSH can compress its traffic, and has been able to for decades. Both sides list the compression methods they support during the key exchange, right next to the ciphers, and agree on one. swift-nio-ssh only ever listed none (and never even looked at what the server offered), so every single log line went over the wire exactly as written.

That’s a shame, because logs are about as compressible as text gets. They repeat the same timestamps, host names, process names and messages over and over. I tried gzip on two real ones:

  • LogRaker’s own log, 3.4 MB, came down to 217 KB, about 16 times smaller
  • the last 2 MB of the Jetson’s syslog came down to 83 KB, about 24 times smaller

On a fast home network nobody cares, but I live on a boat, and when the internet is a cellular or satellite link, every megabyte counts (and so does every second spent waiting for history to load).

How SSH compression works

There are two flavors. Plain zlib compresses from the very first packet, before the client has even logged in, which means the server’s decompressor is exposed to anybody on the internet who can open a TCP connection. OpenSSH has long preferred zlib@openssh.com instead, which is the same zlib but only starts once the login has succeeded, so nothing from an unauthenticated peer is ever inflated. That’s the only one I added.

The mechanics are surprisingly simple. Each direction gets one zlib stream that lives for the whole connection, so the second packet benefits from everything the first one taught the compressor, and every packet ends with a partial flush, so the other side can decompress it right away instead of waiting for the next one. A rekey changes the keys but leaves the streams alone.

Where it plugs in

The changes ended up in five places:

  1. The key exchange offers zlib@openssh.com first and none second, and picks one per direction the way RFC 4253 says to (the first name on the client’s list that the server also has). A server that doesn’t allow compression simply picks none, and nothing else changes.
  2. The new keys carry that choice to the packet reader and writer, armed but not running yet.
  3. The login success message (SSH_MSG_USERAUTH_SUCCESS) switches both directions on, right after it.
  4. The packet writer compresses each message before padding and encrypting it.
  5. The packet reader decompresses each packet after decrypting it.

Two gotchas are worth knowing about if you ever wrap zlib in Swift yourself. First, zlib keeps a pointer back to its own z_stream and checks it on every call, so the stream must never move in memory. Keep it in a Swift property and you’re at the mercy of where Swift decides to put it, so I gave it memory of its own instead. Second, a few kilobytes of zeros can inflate to megabytes, so the reader refuses any packet that inflates past 256 KB (OpenSSH’s own packet limit), and a hostile server can’t make one packet fill up your memory.

The numbers

I tested it against the Jetson (OpenSSH 8.9) by counting the bytes the server actually sent, with compression off and then on:

  • 20,000 live syslog lines (2.2 MB of text) went from 2.96 MB to 1.04 MB, about 2.8 times less.
  • Reading 1,000 older lines from a 20-minute window went from 2.0 MB to 117 KB, about 17 times less, and the lines came back byte for byte as the server’s file holds them.

Live lines gain less because each one is sent the moment it’s written, in its own packet, and every packet carries about 40 bytes of SSH framing that no compression can touch.

Of course, that second number made me look twice: why did reading 1,000 lines (111 KB of text) download 2 MB in the first place? It turned out the search for the window’s edges read 16 KB at every point it looked at, when all it needs there is one line with a timestamp. Now it reads a few lines’ worth, sized from the lines it has already seen, and the server trims each read to the exact bytes instead of sending whole 4 KB blocks. That alone brought the same read down to 280 KB, and together with compression it’s now 32 KB on the wire instead of 2 MB, about 62 times less, with the same number of round trips as before.

On by default, but you can turn it off

OpenSSH’s own ssh client leaves compression off by default, and for good reason: on a fast local network it costs both ends some CPU and saves next to nothing. For a log viewer that’s often talking to devices somewhere far away, I think it’s the other way around, so in LogRaker it’s on by default, and there’s a switch for it in Settings ▸ Streams called ‘Compress SSH traffic’. Servers allow compression by default too (sshd’s Compression setting is yes unless someone changed it), and any server that doesn’t simply connects without it.

Keeping the fork honest

Like everything else in the fork, every edit is marked with a LOGRAKER FORK: comment, and FORK.md explains the change. Compression is off by default in the fork itself, so anyone using it without asking for compression gets exactly what upstream does. Seven new tests cover a compressed connection end to end, the fallback to none, rekeys in both directions, and the inflate limit, and all 365 tests pass.

It’s not a small change, though. The fork now touches 19 of upstream’s files instead of 10, which makes the next rebase a little more work, but I think it’s worth it. It’s published as 0.15.0-rsa.4, Apache 2.0 like upstream.

So…

Compressed SSH is in LogRaker 3.2, for Streams, the Terminal and everything else that talks SSH. If you watch logs over a slow or metered connection, give LogRaker a try, and let me know how it goes!

Thanks for reading!