In this lesson, you will learn how to design robust, scalable consumer patterns using systemd timers to pull and process large volumes of work from a central queue.
The Consumer-Timer Pattern
When dealing with massive batch job queues, running a long-lived process can be risky; if the process crashes or hangs due to a memory leak, the entire queue stalls. Instead of a single persistent daemon, we use systemd timers as "pulse-checkers" to trigger short-lived consumer services that pull a specific batch of work from your queue (e.g., a database table, an SQS queue, or a directory of files).
By decoupling the "scheduler" from the "processor," you gain high availability. If one job causes a segmentation fault, it only affects that single execution instance, not the entire pipeline.
Implementing the Consumer
To implement this, define a service that performs a "work-chunking" operation. It should pull a finite number of items, process them, and then exit.
# /etc/systemd/system/batch-consumer.service
[Unit]
Description=Pull and process next batch of jobs
[Service]
Type=oneshot
ExecStart=/usr/local/bin/process_queue.sh --limit 100
You then pair this with a timer that launches the service frequently. To handle massive queues without overloading the system, use OnUnitInactiveSec rather than OnCalendar. This ensures that if a batch takes longer to process than expected, the timer waits for the previous run to finish before starting the next one.
# /etc/systemd/system/batch-consumer.timer
[Unit]
Description=Trigger batch processing every minute
[Timer]
OnUnitInactiveSec=1min
AccuracySec=1s
[Install]
WantedBy=timers.target
Why this is Robust
- Backpressure: By setting
OnUnitInactiveSec, you naturally prevent the system from spawning an infinite number of overlapping processes if the jobs begin to take longer than your interval. - Resource Isolation: Each execution uses fresh memory, preventing "zombie" states common in long-running scripts.
- Observability: Because each batch is a distinct
systemdunit, you can easily query thejournalctllogs for specific job failures without filtering through weeks of continuous output.
Key Takeaway: Replace monolithic, long-lived background daemons with short-lived, timer-triggered consumer units to ensure your batch processing system is isolated, recoverable, and capable of natural backpressure.