How does Redis resolve a directive set twice across `redis.conf` and `include` files, and how does `CONFIG REWRITE` treat those files?
answer
- read top to bottom
- the last processed line
- position of the include line
- the rewrite opens one file only
basics
~10 sRedis processes config lines in order, expanding each include where it appears, so the last line for a single-value directive wins. CONFIG REWRITE edits only the startup file, and never the included ones.
solid answer
~50 sRedis reads its config file top to bottom and expands each `include` at the line where it appears, so for a single-value directive the last processed line wins. An `include` at the top gives you shared defaults that the file's own lines override, and one at the bottom lets the shared file override everything. `CONFIG REWRITE` opens only the file the server was started with and does not edit the included files or the `include` lines. It sees a value that came from an include as absent from the file, so if that value is non-default it appends a copy at the end of the main file. That copy then beats the shared file at every later restart, which quietly pins the host. With `include` as the last line, a directive the main file also holds is rewritten above the include and loses to it at restart.
code
conf · 7 lines# /etc/redis/redis.conf
include /etc/redis/fleet-defaults.conf
include /etc/redis/conf.d/*.conf
# lines below override anything the includes set
port 6379
maxmemory 4gbgo deeper
Recall the line format, that a config file can include others, and that the last line read for a directive sets its value.
Explain how position decides precedence, how wildcard and plain includes differ when nothing is found, and why the rewrite opens only the startup file.
Diagnose the pinned host: a shared template edit that works everywhere except one node, caused by a past CONFIG REWRITE appending the template's values to that node's main file.
Set one layout for a fleet and say whether live rewrites are allowed on managed hosts, since the shared template and CONFIG REWRITE pull in opposite directions.
## The file format `redis.conf` is a list of **directives**, one per line: ``` keyword argument1 argument2 ... argumentN ``` Arguments with spaces go in single or double quotes, as in `requirepass "hello world"`. A line is a comment only when it starts with `#`, so a trailing comment after a value is read as more arguments and stops the server with a wrong-number-of-arguments error. Memory sizes accept units, and the units are case insensitive: - `1k` is 1000 bytes and `1kb` is 1024 bytes. - `1m` is 1000000 bytes and `1mb` is 1024*1024 bytes. - `1g` is 1000000000 bytes and `1gb` is 1024*1024*1024 bytes. A directive the binary does not recognise stops startup. With no argument it reports `Bad directive or wrong number of arguments`. With at least one argument it is held as a possible module setting, so a plain typo ends in `Module Configuration detected without loadmodule directive or no ApplyConfig call: aborting`, a message that sends people looking in the wrong place. ## How include works `include /path/to/file.conf` loads another file at that exact position, and included files can include others. Paths may contain wildcards: matching files load in alphabetical order, and a wildcard that matches nothing is ignored without an error, so an empty `conf.d` directory is safe. A plain path behaves differently, because a file that cannot be opened is a fatal error at startup. ## Which value wins The `redis.conf` shipped with Redis states the rule: Redis always uses the last processed line as the value of a directive. So the position of the `include` line is the decision. | `include` position | Who wins at startup | Fits when | |---|---|---| | First line | The host's own lines | A shared template with per-host overrides | | Last line | The included file | The shared file must be enforced on every host | The rule is about directives holding one value. Directives that legitimately repeat, such as `save`, `loadmodule` or `rename-command`, accumulate rather than replace each other. Command-line options come last of all. `redis-server /etc/redis/redis.conf --port 7000` appends the options to the file's contents before parsing, so `--port` beats any `port` line. ## What CONFIG REWRITE does with includes `CONFIG REWRITE` opens one file, the one the server was started with, and never follows `include` lines. The `redis.conf` comments note that `include` itself is not rewritten, by an admin or by Sentinel. Three things follow: - **Included files are never edited.** A change you made to a value from a shared file is written to the main file instead. - **Values from includes get copied.** To the rewrite, a directive that lives in an included file looks absent from the main file. If its effective value is non-default, it is appended to the main file, even if you never changed it. - **Values at the default are skipped.** If a shared file sets `maxmemory 4gb` and you set it back to `0`, the default, nothing is written, so the next restart reads `4gb` again. The second point is the expensive one. One `CONFIG REWRITE` on a host pins every non-default value from the template at the bottom of its `redis.conf`. Later edits to the shared template then appear to work on other hosts and do nothing on this one. The position trap runs the other way. If `include` is the last line and the main file already has `maxmemory`, the rewrite updates that line in place, above the include. The include wins at restart and the live change is lost. That is why the shipped comments advise putting includes at the beginning unless you want them to override. ## Which file is in play `INFO server` reports the startup file as `config_file`, and that is the one a rewrite edits. For Redis 8.0 to 8.8, `redis-full.conf` opens with `include redis.conf`, so a server started on it has its rewrites land in `redis-full.conf`. The documentation says Redis 8.10 uses one file, `redis.conf`, while a build from a git checkout still generates `redis-full.conf` for module settings. ## A workable convention 1. Put `include` lines first, then per-host lines. 2. Keep shared values in the template and host values in the main file. 3. Decide whether `CONFIG REWRITE` is allowed on managed hosts, because it moves values between the two.
- A teammate adds a trailing comment after a value, as in `maxmemory 4gb # was 2gb`. What happens at the next Redis startup?The server refuses to start. Only a line beginning with `#` is a comment, so the rest is split into extra arguments, and a single-value directive with more than one argument fails with `wrong number of arguments`. Put the comment on its own line.
- When is `include` as the last line of redis.conf the right choice?When a shared file must beat local edits on every host, such as a fleet-enforced policy. The policy still has a hole: a non-default value the main file lacks is appended below the include by `CONFIG REWRITE` and then beats it. A directive the main file holds stays above the include, so its live change is lost at restart.
- Redis rejects a config file when a single-value directive appears twice.
- Values in an included file always override the main file.
- CONFIG REWRITE updates the included file the value came from.
- A wildcard include that matches no files stops the server from starting.
- In redis.conf, 1g and 1gb mean the same number of bytes.