In Redis, what does `CONFIG SET` change, what happens to that change at restart, and what does `CONFIG REWRITE` add?
answer
- running server versus file on disk
- takes effect on the next command
- a restart reads the old file
- minimal edits, comments kept
basics
~10 sCONFIG SET changes a setting on the running Redis server from the next command onward but leaves redis.conf alone, so a restart reverts it. CONFIG REWRITE writes the running values back into that file.
solid answer
~50 s`CONFIG SET` changes a parameter on the running server, and the new value applies from the next command with no restart. It does not touch `redis.conf`, and it acts only on the server you sent it to, so the next restart loads the old value again. To keep the change, edit the file by hand or run `CONFIG REWRITE`, which updates the file the server was started with so that it matches the current values, keeping comments and changing as little as it can. An option already in the file is rewritten on its own line, a non-default option not in the file is appended, and an option at its default is not added. `CONFIG GET` reads the effective values and accepts glob patterns. A server started with no config file answers `CONFIG REWRITE` with an error.
code
plaintext · 7 lines127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"
127.0.0.1:6379> CONFIG SET loglevel verbose
OK
127.0.0.1:6379> CONFIG REWRITE
OKgo deeper
Recall the three commands and what each touches: GET reads, SET changes the running server, REWRITE writes the file. Say clearly that a restart loses an unsaved change.
Explain how the rewrite treats an option: rewritten in place when present, appended when non-default and absent, skipped when default and absent. Mention that the change does not reach replicas.
Show the operating habit: verify with CONFIG GET, persist on every node, and treat config files as drifting from live values unless something checks them after an incident.
Decide who owns the truth. If configuration management owns the file, a live change plus CONFIG REWRITE is overwritten at the next run, so a team needs a rule for feeding incident changes back.
## Two copies of every setting A Redis server has a **configuration file**, usually called `redis.conf`, and a set of **effective values** held by the running process. At startup the server reads the file into those values. After that the two can drift apart, and most of this topic is about managing that drift. Three commands work on the running values and the file: - `CONFIG GET` reads effective values. - `CONFIG SET` changes effective values. - `CONFIG REWRITE` copies effective values into the file. ## Reading with CONFIG GET `CONFIG GET` takes one or more parameter names or glob-style patterns and replies with name and value pairs. `CONFIG GET *` lists the settings at once, and `CONFIG GET *max-*-entries* maxmemory` mixes a pattern with an exact name. A name that matches nothing simply adds nothing to the reply; it is not an error. Because it reports what the server is really using, `CONFIG GET` is the check to run after any change. Reading the file tells you what the next restart will do, not what the server is doing now. ## Changing with CONFIG SET `CONFIG SET parameter value [parameter value ...]` replies `OK`, or an error if the value was not set. The Redis documentation says a value set this way is loaded immediately and takes effect starting with the next command. Most settings work this way, so a routine change needs no restart. Since Redis 7.0 one call can carry several pairs. Some settings do more than flip a value. `CONFIG SET appendonly yes` starts a background process that writes the initial append-only file. The command still replies at once, but the work it started takes time. Three limits matter in practice: - **It leaves the file alone.** The documentation says plainly that at the next restart the old configuration is used. - **It acts on one server.** `CONFIG SET` is not a write command and is not replicated, so a replica keeps its own value until you change it there too. - **Not every setting accepts it.** Some can only change through the file and a restart. ## Persisting with CONFIG REWRITE `CONFIG REWRITE` updates the file the server was started with. `INFO server` shows that path in its `config_file` field. The rewrite is deliberately conservative: 1. Comments and the overall structure of the file are preserved as far as possible. 2. An option already in the file is rewritten on the same line. 3. An option not in the file, at its default value, is not added. 4. An option not in the file, at a non-default value, is appended at the end. The first appended line follows a `# Generated by CONFIG REWRITE` marker. 5. Lines no longer in use are blanked, for example surplus `save` lines after snapshots were turned off. Rule 2 has a useful consequence: an option already in the file stays there even when set back to its default, so its line records the value you meant. A line the rewrite cannot parse, or one naming a setting this binary does not know, is kept as a comment starting `# ???`. ## When the rewrite fails A server started with no configuration file replies `The server is running without a config file`. If the file existed at startup and has since vanished, the command can rebuild it from scratch. The 8.10 source writes a temporary file beside the original, syncs it, then renames it over the original, so a crash leaves either the old file or the new one. That means the server needs write access to the directory, not just the file. Otherwise the reply is an error that begins `Rewriting config file:` followed by the reason. ## The usual habit 1. Check the current value with `CONFIG GET`. 2. Apply the change with `CONFIG SET` and watch the effect. 3. If it was right, run `CONFIG REWRITE` or update the source of truth for the file. 4. Repeat on every other node that should hold the value, replicas included. The failure this prevents is a quiet one. A setting tuned live during an incident works for months, then disappears at the next restart, and nobody connects the two events because the restart happened long after the change.
- You ran CONFIG SET on a Redis primary. Does its replica pick up the new value?No. `CONFIG SET` is not a write command, so it is not replicated, and each node keeps its own value. Run it, and `CONFIG REWRITE`, on every node. Cluster-aware client libraries may send it to all nodes because the command's routing tip says `all_nodes`, but that is the client's doing, not the server's.
- What can CONFIG REWRITE do to the config file that you might not expect?It persists every non-default effective value, not only the one you just changed, so an option passed as `--port 7000` on the command line lands in the file. In 8.10 it also replaces the file through a temp-and-rename and sets mode 0644 less the server's umask, so a tighter mode on a file holding a password is not kept.
A thermostat's manual override changes the temperature right now, but after a power cut it returns to its stored program. CONFIG SET is the override, redis.conf is the stored program, and CONFIG REWRITE saves the override into the program.
- CONFIG SET saves the new value into redis.conf automatically.
- CONFIG REWRITE dumps every setting into a new file and loses the comments.
- You must restart Redis before any CONFIG SET change takes effect.
- CONFIG SET on a primary is replicated to its replicas.
- A server started with no config file can create one with CONFIG REWRITE.