Log rotation in Nginx - Time & Space Complexity
Log rotation in nginx helps manage log file sizes by periodically creating new files.
We want to understand how the work needed grows as logs get bigger or more frequent.
Analyze the time complexity of this nginx log rotation snippet.
/var/log/nginx/*.log {
rotate 5;
size 100M;
compress;
delaycompress;
notifempty;
create 0640 www-data adm;
}
This config rotates logs when they reach 100MB, keeps 5 backups, compresses old logs, and creates new files with set permissions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking log file size periodically and compressing rotated logs.
- How many times: Size check happens continuously; compression runs once per rotated file (up to 5 files).
As logs grow larger, nginx checks size often but compression only happens on rotated files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10MB | Many size checks, no rotation yet |
| 100MB | Size check triggers 1 rotation and compression |
| 500MB | Multiple rotations, compressions up to 5 files |
Pattern observation: Size checks happen often but compression work grows linearly with number of rotated files.
Time Complexity: O(n)
This means the work to compress rotated logs grows linearly with the number of rotated files.
[X] Wrong: "Log rotation time grows with total log size continuously."
[OK] Correct: Size checks are frequent but cheap; heavy work happens only on rotated files, which are limited.
Understanding how log rotation scales helps you manage system resources well and shows you grasp real-world server maintenance.
"What if we increased the number of rotated files kept from 5 to 50? How would the time complexity change?"