Sub topic 10
Blocking versus non-blocking delivery
This chapter assumes you can run containers with docker run, you know that a logging driver receives each container's stdout and stderr, and you have set --log-opt options before. It covers what happens when a driver is slower than the application, and how to trade log completeness for application speed.
How delivery works by default
Docker's default delivery mode is blocking. When the application writes a line to stdout or stderr, the Docker daemon reads it and passes it to the logging driver. In blocking mode, the application's write does not finish until the driver accepts the line.
With the json-file and local drivers this is rarely a problem, because the driver only appends to a file on local disk. The risk appears with drivers that send logs over a network, such as syslog over TCP, fluentd, or awslogs. If the destination is slow or unreachable, the driver waits. The application then waits too. A web server can stop answering requests because its log collector is down.
What non-blocking mode changes
In non-blocking mode, the daemon puts each line into an in-memory ring buffer (a fixed-size queue) and returns to the application right away. A separate thread drains the buffer into the driver. The application no longer waits on the log destination.
The cost is that the buffer can fill up. When it does, Docker drops the oldest message in the buffer to make room for the new one. You choose to lose some log lines instead of stalling the application.
Enabling non-blocking mode
Two options control it:
| Option | Meaning | Default |
|---|---|---|
| `mode` | `blocking` or `non-blocking` | `blocking` |
| `max-buffer-size` | Size of the ring buffer, used only in non-blocking mode | `1m` |
This command sends logs to a remote syslog server and uses a 4 MB buffer:
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:alpineTo make non-blocking mode the default for new containers, put it in daemon.json. All option values must be strings. In this example json-file is paired with rotation options, but the mode options apply to any driver:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3",
"mode": "non-blocking",
"max-buffer-size": "4m"
}
}Restart the daemon after editing the file. The new defaults apply only to containers created afterwards. Existing containers keep the configuration they were created with, so you must remove and recreate them.
Choosing a buffer size
A larger buffer survives longer outages and bursts, but it uses more daemon memory for each container that has it. The buffer lives only in memory, so anything still queued is lost if the daemon stops.
Use these rules of thumb:
- Keep the default
blockingmode for local drivers (json-file,local). They are fast, and you keep every line. - Use
non-blockingfor network drivers when application availability matters more than a complete log. - Keep
blockingwhen you need a complete audit trail. Dropped lines are not recorded anywhere, so you cannot tell what was lost. - Size the buffer for your burst rate. If an application writes 2 MB of logs in a spike and the buffer holds 1 MB, lines will be dropped even when the destination is healthy.
Verifying the mode
The effective settings are stored per container. Inspect them to confirm what a container is using:
docker inspect --format '{{json .HostConfig.LogConfig}}' workerThe output should list "mode":"non-blocking" and "max-buffer-size":"4m" under Config. If they are missing, the container was created before your change, or the option was misspelled. Check for stray typos in the option names, and check that the values are strings in daemon.json.
Exercise
A container uses the fluentd driver in the default mode. The fluentd server becomes unreachable, and the application starts responding slowly. What setting would you change, and what is the downside?
Show answer
Set mode to non-blocking, and choose a max-buffer-size that fits your burst rate. Application writes then return immediately because lines go into an in-memory ring buffer. The downside is that when the buffer fills, the oldest lines are dropped without any warning. Because the setting is stored per container, you must recreate the container for the change to apply.
Exercise
You added "mode": "non-blocking" to daemon.json and restarted the daemon, but docker inspect on a running container still shows blocking behavior. Why?
Show answer
The daemon-wide default applies only to containers created after the change. The running container keeps its original log configuration, so remove it and create it again.