Table of contents
1.
Introduction
2.
Passing arguments via the command line
3.
Edit Configuration
4.
Changing the Redis configuration while the server is running
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Redis Configuration

Author GAZAL ARORA
0 upvote

Introduction

Redis is an in-memory data structure used as a database, cache, message broker, and streaming engine. It offers built-in replication, Lua scripting, LRU eviction, transactions, several levels of on-disk persistence, Redis Sentinel for high availability, and Redis Cluster for automatic partitioning. You can use Redis from most programming languages.

Redis is open-source (BSD licence). Redis allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. 

You can install Redis from here.

Redis can also start without a configuration file using the built-in default configuration. However, this is only suggested for testing and development.

The right way to configure Redis is by providing a Redis configuration file called redis.conf.

The Redis configuration file (redis.conf) can be found in the root directory. It is located at installdir/redis/etc/redis.

The CONFIG command can be used to get and set all Redis configurations. 

CONFIG GET CONFIG_SETTING_NAME

 

Use * instead of CONFIG SETTING NAME to get all configuration options.

CONFIG GET *

 

The redis.conf file contains multiple directives that have a very simple format:

keyword argument1 argument2 ... argumentN

Passing arguments via the command line

You can also use the Redis command line to pass configuration options. This is very useful for testing. An example of starting a new Redis instance using port 6381 as the replica of the instance running at server 127.0.0.1 port 6379 is shown below.

./redis-server --port 6381 --replicaof 127.0.0.1 6379

 

The arguments passed via the command line follow the same format as those in the redis.conf file, with the difference that the keyword is preceded by –.

Edit Configuration

You can either modify the redis.conf file directly or use the CONFIG set command to update configurations.

Syntax

The basic syntax of the CONFIG SET command is as follows.

CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE

Changing the Redis configuration while the server is running

The commands like [CONFIG SET](/commands/config-set) and [CONFIG GET](/commands/config-get) can be used to modify Redis on the fly without pausing and restarting the service or to query the current configuration programmatically.

Note that changing the setting on the fly does not affect the redis.conf file. Thus the old configuration will be used when Redis is restarted.

Make sure to update the redis.conf file to match the settings you set with [CONFIG SET] (/commands/config-set). You can do it manually or use [CONFIG REWRITE] (/commands/config-rewrite) to search your redis.conf file and change the fields that don't match the current configuration value automatically. 

Fields that don't exist but have a default value aren't added. Your configuration file's comments are saved.

Click here to know about data types in Redis. 


You can also read about the memory hierarchy.

Frequently Asked Questions

1. How do I access the Redis config file?
Run the config get command to receive a list of a Redis server's current configuration parameters. config get only takes one argument, which can be an exact match of a Redis parameter.

2. What is Redis used for?
Redis is used as an in-memory data storage with streaming technologies like Apache Kafka and Amazon Kinesis to ingest, process, and analyse real-time data with sub-millisecond latency. For real-time analytics use cases like social media analytics, ad targeting, personalization, and IoT, Redis is an excellent solution.

3. What port does Redis use?
TCP port 6379 is the default port for the Redis server.

Conclusion

In this article, we learned to configure Redis. Redis configuration file is known as redis.conf. We looked at:

  1. Get the Redis config file.
  2. Passing arguments via the command line
  3. Edit configuration
  4. Changing the Redis configuration while the server is running

Recommended Reading:

wget command in linux

Redis also allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. Click here to know about data types in Redis.


Happy Coding!.

You can use Coding Ninjas Studio to practice various DSA questions asked in the interviews. It will help you in mastering effective coding techniques, and you will also get interview experiences with people working in big companies.

 

Live masterclass