0
0
Nginxdevops~5 mins

Log rotation in Nginx - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Log rotation
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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).
How Execution Grows With Input

As logs grow larger, nginx checks size often but compression only happens on rotated files.

Input Size (n)Approx. Operations
10MBMany size checks, no rotation yet
100MBSize check triggers 1 rotation and compression
500MBMultiple rotations, compressions up to 5 files

Pattern observation: Size checks happen often but compression work grows linearly with number of rotated files.

Final Time Complexity

Time Complexity: O(n)

This means the work to compress rotated logs grows linearly with the number of rotated files.

Common Mistake

[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.

Interview Connect

Understanding how log rotation scales helps you manage system resources well and shows you grasp real-world server maintenance.

Self-Check

"What if we increased the number of rotated files kept from 5 to 50? How would the time complexity change?"