Sub topic 12
Choosing TCP and Queueing Messages
This chapter covers how a client delivers log messages to the central host, and what happens to those messages when the network or the server fails. It assumes you can edit rsyslog configuration files, use systemctl, and read basic firewall rules. It also assumes you have already seen a forwarding action and a receiver like the ones in the earlier chapters. Here we look at the delivery guarantees behind them.
Why transport choice matters
A log pipeline is only useful if the messages you need arrive when something goes wrong. Outages are exactly when the network is least reliable. The two decisions below determine whether messages survive an outage.
- The transport protocol: UDP or TCP.
- The queue: what the client does with messages it cannot deliver right now.
UDP versus TCP
Classic syslog used UDP port 514. UDP sends each message as a single datagram. It does not confirm delivery, does not retransmit, and does not tell the sender when the receiver is down.
TCP opens a connection, acknowledges data, and retransmits lost segments. It also tells the sender when the connection breaks, so rsyslog can react.
| Property | UDP | TCP |
|---|---|---|
| Delivery confirmation | None | Acknowledged by the kernel |
| Loss during a short network fault | Silent | Retransmitted |
| Sender knows the receiver is down | No | Yes, the connection fails |
| Message size | Limited by datagram size | Stream, no per-message limit from the transport |
| Supports TLS | No (needs DTLS) | Yes |
| Overhead | Low | Higher, one connection per client |
Choose TCP for anything you need to keep. UDP is acceptable only for high-volume, low-value messages where occasional loss is fine.
TCP is not a complete guarantee. The kernel acknowledges data when it receives it, not when rsyslog has written it to disk. If the receiver process crashes after the kernel accepted the data, those messages can still be lost. Queues and, if needed, reliable protocols such as RELP reduce that risk. RELP is an rsyslog-specific protocol with application-level acknowledgements, and it is outside the scope of this chapter.
What a queue is
Every rsyslog action has a queue between the rule that selects a message and the output module that sends it. By default this queue is small and lives in memory. If the output cannot send, the queue fills. When it is full, rsyslog either blocks the main message flow or discards messages, depending on settings.
For forwarding you almost always want a bigger queue that can spill to disk. The example from the plan does exactly that:
# /etc/rsyslog.d/50-forward.conf on each client
action(type="omfwd"
target="loghost.example.com" port="514" protocol="tcp"
queue.type="LinkedList"
queue.filename="fwd_queue"
queue.maxDiskSpace="1g"
queue.saveOnShutdown="on"
action.resumeRetryCount="-1")Each setting does a specific job.
queue.type="LinkedList"selects an in-memory queue that grows as needed. Settingqueue.filenameturns it into a disk-assisted queue. Messages stay in memory normally and spill to files once a size threshold is passed.queue.filename="fwd_queue"names the on-disk queue files. Without a file name, rsyslog cannot use the disk and the queue stays memory-only.queue.maxDiskSpace="1g"caps the disk used by the queue. Without a cap, an outage could fill the client's/var.queue.saveOnShutdown="on"writes unsent in-memory messages to disk when rsyslog stops, so a restart or reboot does not lose them.action.resumeRetryCount="-1"means retry forever. The default is a small finite number, after which the action gives up and messages are dropped.
The queue files go to rsyslog's work directory, which is /var/spool/rsyslog on most distributions. You can set it with global(workDirectory="/var/spool/rsyslog"). Make sure that directory exists and is writable by the rsyslog user.
What happens during an outage
The diagram shows the client's behaviour over time as the server goes down and returns.
In the Queue full state, new messages are lost or the sending flow slows down, depending on queue.timeoutEnqueue and related settings. Size the disk limit so that it covers the longest outage you plan for.
A rough sizing method is:
- Estimate the client's log rate in bytes per second.
- Multiply by the outage length in seconds you want to survive.
- Add a margin, for example 50 percent.
A client producing 50 KB per second needs about 180 MB per hour of outage. A limit of 1 GB covers roughly five and a half hours.
Disk-assisted queue tuning
A few more parameters matter when you tune a real system.
queue.sizeis the number of messages kept in memory before the queue spills to disk. The disk-assist threshold is derived from it.queue.highWatermarkandqueue.lowWatermarkset when spilling to disk starts and stops.queue.maxFileSizesets the size of each queue file, 1 MB by default.queue.dequeueBatchSizecontrols how many messages are sent per batch, which affects drain speed after an outage.
For most servers, the defaults plus the five settings shown earlier are enough. Tune further only after you have measured a real problem.
Failure modes to expect
- Reconnect storms. After a long outage, many clients reconnect and drain their queues at once. The central server sees a burst. Check that its own queues and disks can absorb it.
- Half-open connections. If the server host loses power, the client may not notice for some time, and messages written to the dead connection are lost. TCP keepalive settings and RELP reduce this window.
- Queue disk fills
/var. Always setqueue.maxDiskSpaceand keep the work directory on a partition with free space. - Permission errors on the work directory. rsyslog then falls back to memory only, or fails to start the action. Check
journalctl -u rsyslogfor messages. - Duplicate messages. After a connection break, rsyslog may resend messages that the server already received. Occasional duplicates are normal with plain TCP.
Receiver side considerations
The receiver also has limits. The imtcp module accepts a limited number of simultaneous sessions by default. For a large fleet, raise it when loading the module:
module(load="imtcp" MaxSessions="500")Open TCP port 514 only to your client addresses. For example, with firewalld:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port port="514" protocol="tcp" accept'
sudo firewall-cmd --reloadPlain TCP is unencrypted and unauthenticated. Anyone who can reach the port can inject messages. The TLS chapter addresses that.
Checking that the queue works
Test the behaviour rather than assuming it.
- On the central server, stop the receiver:
sudo systemctl stop rsyslog. - On a client, send messages:
for i in $(seq 1 100); do logger -t qtest "message $i"; done. - On the client, look for queue files:
ls -l /var/spool/rsyslog/. For small volumes the messages may still sit in memory, so files may not appear until the threshold is passed or rsyslog is stopped withsaveOnShutdownenabled. - Start the receiver again:
sudo systemctl start rsyslog. - On the central server, confirm that all 100 messages arrived:
grep -c qtest /var/log/remote/*/qtest.log.
If the count is lower than 100, check the client's retry setting and the work directory permissions.
Exercise
A client forwards over TCP with a disk-assisted queue, but action.resumeRetryCount is left at its default. The central server is down for two hours, and afterwards the client's messages from that period are missing. Give two changes that would prevent the loss, and explain why UDP would not have helped.
Show answer
Set action.resumeRetryCount="-1" so the action retries forever instead of giving up and discarding messages. Make sure the queue has a file name and a large enough queue.maxDiskSpace to hold two hours of messages, with queue.saveOnShutdown="on" as protection against restarts. UDP would not help because it has no delivery confirmation or retransmission, so messages sent while the server is down would be lost silently, and rsyslog would have no failure signal to trigger queueing.
Exercise
A client produces 20 KB of logs per second. You want to survive a 6 hour outage with a 50 percent margin. What should queue.maxDiskSpace be at minimum?
Show answer
20 KB/s × 3600 s × 6 = 432,000 KB, about 432 MB. Adding 50 percent gives about 648 MB, so a limit of 700m or 1g is appropriate.