Sub topic 10
Blocking versus non-blocking delivery
This chapter assumes you can run containers with docker run, you know what a logging driver is, and you have set driver options per container or in daemon.json. You should also know that the default json-file driver stores output on the host's disk.
By the end you will know what happens when a log destination is slow, how non-blocking mode changes that, and what you give up by turning it on.
The problem: log delivery sits in the application's write path
Docker captures a container's stdout and stderr through pipes. The daemon reads from those pipes and hands each line to the logging driver. The application is only loosely coupled to the driver, but not completely.
A pipe has a fixed capacity, typically 64 KB on Linux. If the daemon stops reading because the driver is stuck, the pipe fills up. The next write() from the application to stdout then blocks until there is room. A thread that is logging stops doing its real work.
With json-file or local, the driver writes to a local disk, so this rarely matters. Stalls are brief unless the disk itself is saturated. With a remote driver the destination can be slow or unreachable: a syslog server over TCP, a Fluentd collector, or CloudWatch Logs through awslogs. A slow destination can then slow your application, or freeze it.
Blocking mode (the default)
In blocking mode, which is the default for every driver, the daemon passes each message to the driver synchronously. The daemon does not read the next message until the driver has accepted the current one.
The properties of this mode:
- No messages are dropped by Docker. If the driver accepts a message, it is delivered or the driver reports an error.
- Back-pressure reaches the application. A slow destination slows log output. Once the pipe is full, the application's writes to stdout block.
- Ordering is strict. Messages arrive in the order written.
Blocking is a reasonable default for local drivers and for workloads where losing a log line is worse than a slowdown, such as audit-style logging.
Non-blocking mode
In non-blocking mode, the daemon puts each message into an in-memory ring buffer for that container. A separate worker reads from the buffer and calls the driver. The application's write completes as soon as the message is in the buffer, whatever the destination is doing.
The buffer has a fixed size, set by max-buffer-size. The default is 1m (1 MB). When the destination is slower than the application's output, the buffer fills. When a new message arrives and the buffer is full, Docker drops the oldest message in the buffer to make room.
This is the trade-off in one sentence. Non-blocking mode protects the application from slow logging at the cost of possibly losing log lines.
The two modes compared:
| Property | `blocking` (default) | `non-blocking` |
|---|---|---|
| Application write when destination is slow | Waits, and can stall | Returns immediately |
| Message loss under sustained overload | None from Docker | Oldest buffered messages are dropped |
| Memory use | Minimal | Up to `max-buffer-size` per container |
| Buffered messages if the daemon crashes | Not applicable | Lost |
| Best for | Local drivers, strict completeness | Remote drivers, latency-sensitive apps |
Enabling non-blocking mode
Two log options control the behavior. mode accepts blocking or non-blocking. max-buffer-size sets the buffer size, and it only applies in non-blocking mode. Both are driver-agnostic, so they work with any logging driver.
Per container:
docker run -d --name worker \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
--log-opt mode=non-blocking \
--log-opt max-buffer-size=4m \
nginx:alpineThis container sends logs to a remote syslog server. The 4 MB buffer holds a burst of lines if the server is briefly unreachable or slow.
As the daemon-wide default, in /etc/docker/daemon.json:
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://192.0.2.10:514",
"mode": "non-blocking",
"max-buffer-size": "4m"
}
}As with every log-opts value, these must be strings. Write "4m" and "non-blocking", not bare numbers or booleans. Restart the daemon for the change to apply.
In Compose, the same options go under the service's logging key:
services:
worker:
image: nginx:alpine
logging:
driver: syslog
options:
syslog-address: "udp://192.0.2.10:514"
mode: "non-blocking"
max-buffer-size: "4m"Existing containers keep their old settings
A container's logging configuration is fixed when it is created. Changing daemon.json and restarting the daemon affects only containers created afterward. To change an existing container, recreate it with docker run or docker compose up -d (Compose recreates the service when its configuration changed).
Check what a container is using:
docker inspect --format '{{json .HostConfig.LogConfig}}' workerFor the container above, the output looks like this (key order may differ):
{"Type":"syslog","Config":{"max-buffer-size":"4m","mode":"non-blocking","syslog-address":"udp://192.0.2.10:514"}}If mode is missing from Config, the container is in blocking mode.
Choosing buffer size
The buffer is a shock absorber. It should cover the longest outage or slowdown you want to ride out without losing lines. Estimate with this arithmetic:
- Measure your peak log rate, for example 200 lines per second at about 200 bytes each, which is 40 KB/s.
- Pick the stall duration to tolerate, for example 30 seconds.
- Multiply: 40 KB/s × 30 s is about 1.2 MB. The default 1 MB is borderline, so 4 MB gives headroom.
The memory cost is per container, held by the daemon. A host with 100 containers at 4m could use up to 400 MB in the worst case. Size the buffer deliberately and do not set a large value everywhere by default.
A larger buffer delays the point at which loss begins. It does not prevent loss when the destination stays down longer than the buffer can cover.
Which drivers benefit
Mode matters most where the destination is a network service.
json-fileandlocal: The write goes to local disk. Blocking is almost always fine. Non-blocking helps only if the log disk is saturated, and then you have a larger problem to solve.syslogover UDP: Sends are fire-and-forget, so slow destinations seldom stall the application. Non-blocking adds little, though it still smooths local bursts.syslogover TCP or TLS: The driver can wait on the network. Non-blocking mode protects the application.fluentd: Delivery depends on the collector. Docker's documentation also offersfluentd-async, which connects to the collector in the background. This is a separate option frommodeand concerns connecting at container start. Check the driver's own documentation when you need to survive a collector that is down when the container starts.awslogsand other cloud drivers: Calls go over the internet to an API with rate limits. Docker's documentation recommends non-blocking mode for these when losing the application's availability is worse than losing some logs.
Non-blocking mode does not make a failed driver connection at container start go away. For example, the syslog driver over TCP connects when the container starts, and an unreachable server can make the start fail. Mode governs delivery after the driver is up. It does not govern whether the driver initializes.
Failure modes and gotchas
Silent loss. When the buffer overflows, Docker drops lines without an application-visible error. The daemon may log a warning in its own log, so check the daemon log (journalctl -u docker on systemd hosts) if lines are missing from your log platform. Do not use non-blocking mode for logs that must be complete.
Lost buffer on crash. Lines in the buffer exist only in daemon memory. If the daemon crashes or the host loses power, buffered lines are gone. In blocking mode, there is no such window, apart from whatever the driver itself buffers.
Hidden stalls in blocking mode. A container that appears hung may be waiting on its log destination. If a process stops making progress and docker logs shows output ending abruptly, check the driver and destination before blaming the application. Switching one container to non-blocking mode and seeing the stall disappear confirms the diagnosis.
Misplaced expectations about max-buffer-size. The option does nothing in blocking mode. Setting it without mode=non-blocking gives a false sense of protection. Always verify with docker inspect.
Mixed rotation and mode settings. max-size and max-file apply to json-file and local and are independent of mode. You can combine them. A json-file container in non-blocking mode still rotates its files normally.
docker logs with remote drivers. With dual logging, Docker keeps a local copy of recent logs for remote drivers so docker logs works. The non-blocking buffer sits in front of the driver, so lines that were dropped from the buffer never reach the destination. Whether they reach the local copy depends on the Docker version, so do not rely on docker logs to recover them.
Decision criteria
Use these questions in order:
- Is the destination local disk? Keep
blocking. - Can the application tolerate losing log lines during an outage? If it cannot, keep
blockingand make the destination reliable, for example by running a local forwarding agent. - Would a logging stall hurt the application's availability or latency? If so, use
non-blockingand size the buffer from your peak rate and the outage length you accept. - Do you need to alert on dropped lines? Docker does not expose a counter for them, so monitor the delivery path itself, for example by comparing line counts at the source and destination.
A common production pattern avoids the problem. The container writes with a local driver (local or json-file with rotation), and an agent on the host ships files to the remote system. The application never depends on the network, and nothing is dropped unless the disk fills.
Summary of the options
| Option | Values | Default | Notes |
|---|---|---|---|
| `mode` | `blocking`, `non-blocking` | `blocking` | Works with every logging driver |
| `max-buffer-size` | Size string such as `1m`, `4m` | `1m` | Used only in non-blocking mode; memory is per container |
Exercise
A container uses the syslog driver over TCP in the default mode. The syslog server stops responding, and soon the application's request handling freezes. Explain why, and give the change you would make, including how it is applied to this container.
Show answer
In blocking mode the daemon waits for the driver to accept each line. With the server unresponsive, the driver stalls, the daemon stops reading the container's stdout pipe, the pipe fills, and the application's write() to stdout blocks, which freezes request handling. Set mode=non-blocking (and a suitable max-buffer-size, for example 4m) with --log-opt, or under logging.options in Compose. The container's logging configuration is fixed at creation, so recreate the container (docker run again, or docker compose up -d) and confirm with docker inspect --format '{{json .HostConfig.LogConfig}}'. Accept that the oldest buffered lines are dropped if the buffer fills.
Exercise
Your application logs about 100 lines per second at roughly 300 bytes per line. You want to survive a 60-second log destination outage without dropping lines. What is the minimum max-buffer-size, and what is the cost of using a larger value on a host with 50 such containers?
Show answer
100 lines/s × 300 bytes = 30 KB/s. Over 60 seconds that is about 1.8 MB, so the default 1m is too small and 2m is the minimum, with 4m giving headroom. The buffer is held in daemon memory per container, so 50 containers at 4m could use up to 200 MB in the worst case.
Exercise
You set "max-buffer-size": "4m" in daemon.json, restarted the daemon, and an existing container still blocks when its destination stalls. Give two reasons this can happen and how to check each.
Show answer
First, the container was created before the change, and logging configuration is fixed at creation, so the new default does not apply. Second, max-buffer-size has no effect unless mode is non-blocking, and the daemon.json may lack "mode": "non-blocking". Check both by running docker inspect --format '{{json .HostConfig.LogConfig}}' on the container. If mode is absent from Config, it is in blocking mode. Recreate the container after fixing the daemon defaults.