Tune for throughput
The defaults suit most connections: on a local network or a link with a short round trip, SshSharp keeps up with the server without any tuning (Benchmarks has the measurements). This page brings together the settings that matter when a transfer is slower than the link: what limits it, which option addresses that limit, and what it costs.
When to use
- A download, command output or tunnel is much slower than the link, and the link has a long round trip.
- An SFTP transfer of one large file, or of a tree of many small files, takes longer than the bandwidth explains.
- Copying into a remote file stream (
OpenWriteAsync) is slower thanUploadAsync. - The client's CPU, not the network, is the limit.
Find the limit first
flowchart TD
A[Transfer slower than the link] --> B{Long round trip?}
B -- yes --> C[Raise ChannelWindowSize<br/>SFTP: Segments, MaxRequestsInFlight]
B -- no --> D{Many small files?}
D -- yes --> E[SFTP: MaxConcurrentFiles]
D -- no --> F{Client CPU busy?}
F -- yes --> G[Cipher choice, thread pool spinning]
F -- no --> H[The server or the network is the limit]
Each option below helps against one limit and does nothing against the others. A larger window does not help when the bandwidth is the limit, and more segments do not help when the server's disk is.
Long round trips: the channel window
One channel (a command's session, an SFTP session, a tunnel) moves at most one flow-control window per round trip. With the default 2 MiB window that is about 20 MiB/s at 100 ms, whatever the bandwidth. Size the window from the bandwidth-delay product, and give the socket a receive buffer to match:
// A fast link with a long round trip (here 1 Gbit/s at 100 ms): bandwidth × round trip is about 12.5 MB, so each channel gets a
// 16 MiB window, and the socket a receive buffer to match, set before it connects so that TCP window scaling can use it.
await using var client = await SshClient.ConnectAsync("backup.example.com", 22, new SshClientOptions
{
UserName = "backup",
PrivateKeys = [await SshPrivateKey.LoadAsync("/etc/myapp/backup_ed25519", cancellationToken: cancellationToken)],
HostKeyVerifier = verifier,
ChannelWindowSize = 16 * 1024 * 1024,
ChannelMaxPacketSize = 255 * 1024,
ConfigureSocket = socket => socket.ReceiveBufferSize = 16 * 1024 * 1024,
}, cancellationToken);
| Option | Default | What it changes |
|---|---|---|
ChannelWindowSize |
2 MiB (1 KiB to 1 GiB) | How much the server may send on a channel before it waits for SshSharp to consume it. |
ChannelMaxPacketSize |
32 KiB (1 KiB to 255 KiB) | The most data the server may put in one packet: fewer, larger packets for bulk data. |
ConfigureSocket |
System defaults | Socket options before connecting, such as ReceiveBufferSize for TCP window scaling. |
Costs and limits:
- Memory: each channel may hold up to its window of data your code has not read yet.
- Uploads are limited by the window the server grants, which only the server can raise (OpenSSH's sshd uses 2 MiB).
ConfigureSocketapplies only to a direct connection made byConnectAsync(host, port, options): not through aProxy, over a stream you opened, or byssh_configalias.
Tune and inspect a connection has the details.
SFTP transfers
// One large file over a link with latency: more reads in flight, and four ranges over four sessions.
await sftp.DownloadFileAsync("images/vm.qcow2", "vm.qcow2",
new SftpTransferOptions { MaxRequestsInFlight = 128, Segments = 4 }, cancellationToken);
// A tree of many small files: eight files at a time, sharing the 64 requests in flight.
await sftp.UploadDirectoryAsync("site", "/srv/www/site", new SftpTransferOptions { MaxConcurrentFiles = 8 }, cancellationToken);
// Streaming into a remote file: large writes keep the 16 writes in flight full; tiny ones would each be a request.
await using var source = File.OpenRead("report.csv");
await using var target = await sftp.OpenWriteAsync("reports/report.csv", cancellationToken);
await source.CopyToAsync(target, 256 * 1024, cancellationToken);
| Option | Default | Helps with | Does not help with |
|---|---|---|---|
SftpTransferOptions.MaxRequestsInFlight |
64 (1 to 1,024) | Latency within one session's window: more reads or writes outstanding at once. | A transfer already limited by the channel window or the bandwidth. |
SftpTransferOptions.Segments |
1 (1 to 16) | One large download or upload (DownloadFileAsync, UploadFileAsync, and per file in directory transfers; uploads since 1.1) on a link with a long round trip: each segment is its own session, with its own window and (on OpenSSH) its own sftp-server process. An upload falls back to fewer sessions, or to in-order writes over one, on a server that refuses a second writer or out-of-order writes. |
Bandwidth: the sessions share one TCP connection. DownloadAsync and UploadAsync (streams) and Resume ignore it. A failed segmented upload is cut to its unbroken start, or deleted where the server cannot cut it (details). |
SftpTransferOptions.MaxConcurrentFiles |
1 (1 to 64) | Directory transfers of many small files, where each file's open, attributes and close round trips dominate. | A few large files, which already keep requests in flight. |
Write-behind on OpenWriteAsync |
Always on, 16 writes per stream | CopyToAsync into a remote file keeps the link busy, as UploadAsync does. |
Many tiny writes, each of which is a request: put a BufferedStream in front. |
Memory stays bounded: a transfer holds at most MaxRequestsInFlight reads or writes of the server's size (about 16 MiB with
OpenSSH's 255 KiB limit and the default 64), shared by all its files, and a file stream at most 16 writes. Segments and
ChannelWindowSize combine: both raise how much is in flight per round trip. The server may allow fewer sessions than Segments
asks for (sshd's MaxSessions, 10 by default).
In the benchmarks, CopyToAsync into OpenWriteAsync took 226 ms for 32 MiB against 244 ms for UploadAsync, on a Linux client
over loopback (Benchmarks). See
Transfer files with SFTP and Writes in flight.
Cipher choice
The cipher is the first one on the client's list that the server also offers. SshSharp's default list puts AES-GCM first when the CPU has AES instructions and ChaCha20-Poly1305 first when it does not:
| Cipher | Implementation | 16 KiB packet, encode (seal) |
|---|---|---|
aes256-gcm@openssh.com |
The platform's, on AES instructions | 1.96 µs |
chacha20-poly1305@openssh.com |
Managed, vectorized ChaCha20 | 14.6 µs |
aes256-ctr with hmac-sha2-256-etm@openssh.com |
The platform's | 13.7 µs |
These are single-packet micro-benchmarks on one machine (Intel Core i7-12800H, .NET 10; see Benchmarks). In a 2 GiB stream over loopback, throughput was the same with either AEAD cipher (157 against 156 MiB/s), but the client spent 7.0 s of CPU per GiB with AES-256-GCM and 10.8 s with ChaCha20-Poly1305. So the default is right for throughput and CPU, and changing it matters only for a server that offers no AES-GCM:
// The client's order decides which cipher is used. The default already puts AES-GCM first on CPUs with AES instructions and
// ChaCha20-Poly1305 first on CPUs without; this pins AES-GCM first whatever the CPU, for example to measure the difference.
await using var client = await SshClient.ConnectAsync("example.com", 22, new SshClientOptions
{
UserName = "deploy",
PrivateKeys = [await SshPrivateKey.LoadAsync("/etc/myapp/deploy_ed25519", cancellationToken: cancellationToken)],
HostKeyVerifier = verifier,
Ciphers = SshAlgorithmList.Prepend("aes128-gcm@openssh.com", "aes256-gcm@openssh.com"),
}, cancellationToken);
Console.WriteLine($"cipher: {client.Algorithms.EncryptionClientToServer}");
client.Algorithms.EncryptionClientToServer shows what was negotiated. See
Choose algorithms and authentication methods.
Compression on slow links
Compression is off by default, and on a fast link it should stay off: it adds CPU work on both sides for every byte, and the link
was not the limit. On a slow link (a few Mbit/s, a branch office or a radio link) carrying text, Compression = true sends fewer
bytes and can make command output, logs and text files arrive sooner. Already compressed data (archives, images, TLS in a tunnel)
gains nothing. Measure both ways on the real link. See
Compress a slow link.
Socket options and Nagle's algorithm
SshSharp turns Nagle's algorithm off on the sockets it opens (Socket.NoDelay), so small writes (keystrokes, short commands) are
not delayed; there is nothing to set. Buffer sizes, TCP keep-alive and the type of service go through ConfigureSocket; see
Set socket options.
Client CPU
When the client's CPU is the limit:
- Run work in parallel on one connection. Commands, SFTP sessions and tunnels are separate channels; eight commands in parallel on one connection took 3.9 ms in the benchmarks, against 1.7 to 1.8 ms for one.
- Thread pool spinning. Much of the client CPU while streaming was the .NET thread pool spin-waiting between packets, not SSH
work. In the benchmarks,
DOTNET_ThreadPool_UnfairSemaphoreSpinLimit=0cut the CPU per GiB (from 5.57 s to 2.40 s on Linux, and from 4.4 s to 2.2 s on Windows) without costing throughput. It is a process-wide runtime setting: measure your own application before and after. - SFTP or SCP. Both reached similar times for a 32 MiB upload in the benchmarks (244 ms and 211 ms). SFTP has resume, segments and directory concurrency; see SFTP or SCP?.
Notes and pitfalls
- Measure before and after with your server and network: the numbers above are from the benchmark machines in Benchmarks, not guarantees.
- The server can set the pace on a fast link: in the benchmarks, OpenSSH's single encryption thread per connection did, and both libraries and OpenSSH's own client kept up with it.
- A server with a low
RekeyLimitrenews keys often, which stalls transfers (the benchmarks met 2 MiB/s withRekeyLimit 32K). MaxSessionson the server boundsSegmentsand parallel channels on one connection.