Redis is able to start without a configuration file using a built-in default configuration, however this setup is only recommanded for testing and development purposes.
The proper way to configure Redis is by providing a Redis configuration file,
redis.conf file contains a number of directives that have a very simple
keyword argument1 argument2 ... argumentN
This is an example of configuration directive:
slaveof 127.0.0.1 6380
It is possible to provide strings containing spaces as arguments using quotes, as in the following example:
requirepass "hello world"
The list of configuration directives, and their meaning and intended usage is available in the self documented example redis.conf shipped into the Redis distribution.
Passing arguments via command line
Since Redis 2.6 it is possible to also pass Redis configuration parameters using the command line directly. This is very useful for testing purposes. The following is an example that stats a new Redis instance using port 6380 as a slave of the instance running at 127.0.0.1 port 6379.
./redis-server --port 6380 --slaveof 127.0.0.1 6379
The format of the arguments passed via the command line is exactly the same
as the one used in the redis.conf file, with the exception that the keyword
is prefixed with
Note that internally this generates an in-memory temporary config file (possibly concatenating the config file passed by the user if any) where arguments are translated into the format of redis.conf.
Changing Redis configuration while the server is running
Note that modifying the configuration on the fly has no effects on the redis.conf file so at the next restart of Redis the old configuration will be used instead.
Make sure to also modify the
redis.conf file accordingly to the configuration
you set using CONFIG SET.
There are plans to provide a
command that will be able to run the
redis.conf file rewriting the
configuration accordingly to the current server configuration, without modifying
the comments and the structure of the current file.
Configuring Redis as a cache
If you plan to use Redis just as a cache where every key will have an expire set, you may consider using the following configuration instead (assuming a max memory limit of 2 megabytes as an example):
maxmemory 2mb maxmemory-policy allkeys-lru
In this configuration there is no need for the application to set a time to live for keys using the EXPIRE command (or equivalent) since all the keys will be evicted using an approximated LRU algorithm as long as we hit the 2 megabyte memory limit.
This is more memory effective as setting expires on keys uses additional memory. Also an LRU behavior is usually to prefer compared to a fixed expire for every key, so that the working set of your data (the keys that are used more frequently) will likely last more.
Basically in this configuration Redis acts in a similar way to memcached.
When Redis is used as a cache in this way, if the application also requires the use Redis as a store, it is strongly suggested to create two Redis instances, one as a cache, configured in this way, and one as a store, configured accordingly to your persistence needs and only holding keys that are not about cached data.
Note: The user is adviced to read the example redis.conf to check how the other maxmemory policies available work.