Redis can start an append-only-file rewrite on its own. Explain how the auto-aof-rewrite-percentage and auto-aof-rewrite-min-size settings decide when that happens, and what baseline size the growth is measured against.
answer
- Both conditions: min-size AND percentage over baseline
- Baseline = size after last rewrite, or size at startup
- Defaults 100% and 64mb, i.e. doubled and at least 64 MB
- percentage 0 disables auto rewrite
- Scheduled, not started, if another child is running
basics
~20 sRedis remembers the AOF size after the last rewrite (or at startup). When the current size exceeds that baseline by auto-aof-rewrite-percentage (default 100, meaning doubled) and is at least auto-aof-rewrite-min-size (default 64mb), it starts a rewrite. Percentage 0 disables it.
solid answer
~40 sBoth conditions must hold. Redis records the AOF size at the end of the last successful rewrite as the baseline (`aof_base_size` in `INFO persistence`); if no rewrite has happened since startup, the size at startup is used. When `aof_current_size` has grown over that baseline by at least `auto-aof-rewrite-percentage` (default `100`, i.e. the file doubled) **and** `aof_current_size` is at least `auto-aof-rewrite-min-size` (default `64mb`), Redis triggers a `BGREWRITEAOF` itself. The min-size guard exists because on a small file doubling is meaningless: a 2 MB file reaching 4 MB is not worth a fork. Setting the percentage to `0` disables automatic rewrites entirely, which is what you do when you want to schedule them yourself off-peak. If a persistence child is already running, the rewrite is not started but marked `aof_rewrite_scheduled:1` and begins once that child exits.
code
text · 12 linesredis-cli CONFIG GET auto-aof-rewrite-percentage auto-aof-rewrite-min-size
# 1) "auto-aof-rewrite-percentage" 2) "100"
# 3) "auto-aof-rewrite-min-size" 4) "67108864"
redis-cli INFO persistence | grep -E 'aof_base_size|aof_current_size|aof_rewrite_scheduled'
# aof_base_size:1048576000
# aof_current_size:1992294400 -> ~90% growth, not yet at 100%
# aof_rewrite_scheduled:0
# Hand control to a scheduler instead of the automatic trigger
redis-cli CONFIG SET auto-aof-rewrite-percentage 0
redis-cli CONFIG REWRITEgo deeper
Recall the two directives and their defaults: at least 64 MB and doubled since the last rewrite.
Explain the baseline explicitly, including the startup-size case, and that both conditions must hold.
Discuss tuning on large instances, disabling the automatic trigger in favour of scheduled rewrites, and the scheduled-versus-started distinction in INFO.
Position it as a frequency-versus-file-size tradeoff tied to restart-time and fork-cost budgets, with explicit alerting so a manual schedule cannot fail silently.
## The two knobs ``` auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb ``` These are the only inputs to Redis's automatic rewrite decision, and they are evaluated together in the server's periodic housekeeping cycle (`serverCron`), not on every command. ## The baseline The percentage is not measured against the dataset size, and not against a fixed number. It is measured against a remembered **baseline**: the size of the AOF immediately after the last successful rewrite. `INFO persistence` exposes it as `aof_base_size`, next to `aof_current_size`. Two details matter: - If no rewrite has occurred since the server started, the baseline is the AOF size **at startup**. So a server restarted with an already-large AOF starts from that large baseline and will not rewrite until it has grown by the configured percentage over it. - The baseline is updated only on a *successful* rewrite. A rewrite that fails (disk full, child killed) leaves the old baseline in place, and Redis will attempt again later. ## The rule A rewrite is triggered when **both** are true: 1. `aof_current_size >= auto-aof-rewrite-min-size` 2. `(aof_current_size - aof_base_size) / aof_base_size * 100 >= auto-aof-rewrite-percentage` With the defaults: the file must be at least 64 MB, and it must have doubled since the last rewrite. Set the percentage to `200` and it must triple. Set it to `0` and automatic rewriting is switched off completely; `min-size` then has no effect. ## Why the minimum size exists Percentages are unstable at small magnitudes. A freshly seeded AOF of a few hundred kilobytes doubles within seconds of normal traffic, and you would fork constantly for no benefit. The minimum turns the percentage rule off until the file is large enough that halving it is actually worth a fork. `64mb` is the default; on a machine with a 50 GB dataset it is far too low, because you will hit it during the first minutes of writes and then rewrite repeatedly against a small baseline. ## Interaction with other persistence children Redis runs at most one persistence child. If the automatic condition fires while a `BGSAVE` (or a replica full-sync fork) is running, Redis does not fork; it sets an internal flag visible as `aof_rewrite_scheduled:1` and starts the rewrite when the current child exits. The same applies to a manual `BGREWRITEAOF`: the command returns success, but the work may not have begun. Monitoring that assumes "command returned, therefore rewriting" will misread this. ## Runtime changes Both settings are `CONFIG SET`-able, so you can raise them during an incident without restarting: ``` CONFIG SET auto-aof-rewrite-percentage 0 # stop automatic rewrites now CONFIG SET auto-aof-rewrite-percentage 200 # rewrite only when the file triples CONFIG REWRITE # persist to the config file ``` Remember `CONFIG REWRITE`, or the change vanishes on restart, which is a classic post-incident surprise. ## Choosing values The tradeoff is straightforward once you name both sides: - **Rewriting more often** keeps the file small, which shortens restart time and disk usage, but pays fork latency and copy-on-write memory more frequently. - **Rewriting less often** amortizes the fork cost, but lets the file grow, which lengthens recovery and risks filling the disk. A practical approach on a large instance: raise `auto-aof-rewrite-min-size` to something meaningful relative to the dataset (for example a multiple of the expected base size), keep or raise the percentage, and measure the resulting rewrite frequency and `latest_fork_usec`. If rewrites still land during peak traffic, disable the automatic trigger and drive `BGREWRITEAOF` from a scheduler during a quiet window, with an alert on `aof_current_size` so you notice if the schedule stops working. ## What these settings do not do They do not affect durability: the loss window is governed by `appendfsync`, not by rewrite frequency. They do not affect how much memory the dataset uses. And they do not throttle a running rewrite; once the child is forked, it runs to completion regardless of the thresholds.
- You restart a Redis server whose AOF is already 20 GB. When will the automatic trigger fire?The baseline becomes the size at startup, 20 GB, because no rewrite has happened in this process yet. With the default 100 percent the file must reach roughly 40 GB before an automatic rewrite starts. If that is unacceptable, issue a manual `BGREWRITEAOF` after startup to reset the baseline to a sane value.
- Why would you set auto-aof-rewrite-percentage to 0 in production?To take the fork off the server's own schedule. With automatic rewrites disabled you trigger `BGREWRITEAOF` from cron or an operator runbook during a low-traffic window, so the fork stall and copy-on-write memory spike land when they are cheapest. The cost is that you now own the risk: if the schedule silently stops, the AOF grows unbounded, so alert on `aof_current_size`.
saying these in an interview costs you the question
- Thinking the percentage is measured against the dataset size or against the RDB size
- Believing either condition alone is enough to trigger a rewrite
- Assuming the baseline is fixed forever rather than reset after each successful rewrite
- Changing the settings with CONFIG SET and forgetting CONFIG REWRITE
- Claiming rewrite frequency changes the durability window instead of appendfsync