Redis is a memory-based key-value data store used in various environments that require fast data processing. It is mainly utilized for caching, task queues, and session management, and can also be used as a persistent data store by adding data preservation features. In this first post, we will explore the basic concepts of Redis and the main configuration file, redis.conf.

1. Basic Concepts and Features of Redis

Redis is a NoSQL database that boasts high speed by storing data in memory (RAM). Due to this characteristic, it is widely used for the following purposes.

  • Cache: To enhance the data response speed of web applications, frequently accessed data is stored in Redis for quick retrieval.
  • Session Storage: It stores user session information for quick access and management of session data.
  • Queue: An asynchronous task queue that schedules and processes tasks rapidly in conjunction with libraries such as Celery.
Redis key concepts infographic

Main Advantages of Redis

  • Speed: By storing data in memory, it allows for rapid read and write access, minimizing response delays.
  • Support for Various Data Structures: It supports a range of structures beyond simple key-value storage, including lists, sets, and hashmaps.
  • Scalability: It can expand data storage capacity and processing performance through cluster configurations.

Volatility of Redis and Data Preservation Options

By default, Redis is designed to handle volatile data, meaning that data stored in memory may be lost during server restarts. To address this, Redis offers two data preservation options.

  • RDB (Redis Database) Snapshots: It saves the data in memory to disk in snapshot form at regular intervals.
  • AOF (Append-Only File): It records all write operations to disk to enable data recovery upon server restart.

If Redis is used solely for caching, there may not be much concern regarding data volatility; however, if persistent data preservation is needed, these options can be utilized.

2. Redis Configuration File (redis.conf)

The main configuration file of Redis, redis.conf, allows detailed configuration of how the Redis server operates. It is generally located at /etc/redis/redis.conf and is automatically created during Redis installation.

Main Configuration Items in redis.conf

Redis configuration file structure diagram
1) Network Settings
  • port: Sets the port used by the Redis server. The default port is 6379.
port 6379
  • bind: Specifies the IP address that the Redis server will listen to. The default is 127.0.0.1, allowing only local connections.
bind 127.0.0.1
2) Security Settings
  • requirepass: Sets a password for accessing the Redis server. If omitted, access is possible without a password.
requirepass yourpassword
  • protected-mode: A mode that protects Redis from external access, with the default set to yes allowing only local connections.
3) Memory Management
  • maxmemory: Sets the maximum memory limit that Redis can use. If there’s no limit, it will use up to the allowed system memory.
maxmemory 256mb
  • maxmemory-policy: A policy that determines which data to delete when memory is full. allkeys-lru removes the least recently used data.
maxmemory-policy allkeys-lru
4) Data Preservation Settings
  • save: Periodically creates RDB snapshots and saves them to disk. For example, the below setting creates a snapshot if more than 1000 keys change every 60 seconds.
save 60 1000
  • appendonly: Activates AOF mode to enhance data preservation. If set to yes, all write operations are recorded in the AOF file.
appendonly yes
5) Logging and Debugging Settings
  • loglevel: Sets the log level. notice records only general messages, while debug records all debug information.
loglevel notice
  • logfile: Specifies the path of the log file. The default is stdout, which does not write to a separate file.
logfile /var/log/redis/redis.log

3. How to Apply Changes After Modifying Redis Settings

Once you modify the desired options in the configuration file, you can apply the settings by restarting the Redis server.

sudo systemctl restart redis-server

After making changes, you can check if Redis is running properly using the following command.

redis-cli ping

If PONG is returned, it indicates that the Redis server is functioning normally.

Conclusion

We have learned about the basic concepts of Redis and important items in the configuration file. Since Redis allows detailed adjustments to performance and preservation through settings, it is important to optimize the configurations according to the intended use. In the next part, we will delve deeper into Redis's data preservation options, RDB and AOF.