Skip to main content

Redis CLI

  • redis-cli is the command-line interface for interacting with a Redis server.
  • It allows you to send commands, configure settings, and test data operations.
  • By default, it connects to:
    • Host: 127.0.0.1 (localhost)
    • Port: 6379 (default Redis port)

Redis CLI Configuration

You can connect with options:

  • Specify host & port
docker exec -it redis-server redis-cli -h 127.0.0.1 -p 6379
  • With authentication (if Redis has a password)
docker exec -it redis-server redis-cli -a mypassword
  • Run a single command without entering interactive mode
docker exec -it redis-server redis-cli PING

Redis redis.conf File

The redis.conf file is the main configuration file for Redis.

  • When Redis starts, it reads this file to load settings.
  • By default, it’s located in /etc/redis/redis.conf (Linux) or inside the Redis source folder.

Docker container does not include a redis.conf file by default.

  1. Create a folder on your host(project): mkdir redis-config

  2. Download a redis.conf template: curl -o redis-config/redis.conf https://raw.githubusercontent.com/redis/redis/7.0/redis.conf

  3. Use redis.conf in Docker container

    docker stop redis-server
    docker rm redis-server

    docker run -d ^
    --name redis-server ^
    -p 6379:6379 ^
    -v "%cd%\redis-config\redis.conf":/usr/local/etc/redis/redis.conf ^
    redis redis-server /usr/local/etc/redis/redis.conf

If no config is given, Redis starts with default settings

Main Sections of redis.conf

Here are the most important settings you’ll encounter in redis.conf:

  1. General Settings

    • Daemonize (background mode): daemonize yes

      • yes → Redis runs in the background.
      • no → Redis runs in the foreground (useful for debugging).
    • PID file (process ID storage): pidfile /var/run/redis/redis-server.pid

  2. Networking

    • Port Redis listens on port 6379, Default is 6379. You can change it if needed.
    • Bind address: bind 127.0.0.1, Redis will only accept connections from localhost.
    • If you want to allow external access: bind 0.0.0.0. But for security, you must add a password (see below).
  3. Security

    • Require password for clients: requirepass myStrongPassword
    • After setting this, every client must authenticate: redis-cli -a myStrongPassword
  4. Persistence (Saving Data)

    Redis stores data in memory, but persistence ensures data is saved to disk.

    • RDB snapshots (point-in-time saves):

      save 900 1   # Save if at least 1 key changed in 900 seconds (15 min)
      save 300 10 # Save if at least 10 keys changed in 300 seconds (5 min)
      save 60 10000 # Save if 10000 keys changed in 60 seconds
    • AOF (Append-Only File) logging:

      appendonly yes
      appendfsync everysec
      • Keeps a log of all write operations.
      • everysec → syncs data every second (good balance between performance and safety).
  5. Memory Management

    • Maximum memory limit:

      maxmemory 256mb
    • Redis will not use more than 256 MB RAM.

    • Eviction policy (when memory is full):

      maxmemory-policy allkeys-lru

      Options:

      • noeviction → return error when memory full.
      • allkeys-lru → remove least recently used keys.
      • volatile-lru → remove LRU keys with TTL set.
  6. Logging

    • Log level:

      loglevel notice

      Options: debug, verbose, notice, warning.

    • Log file path:

      logfile /var/log/redis/redis-server.log
  7. Replication (Master/Replica setup)

    Make this Redis a replica of another:

    replicaof 192.168.1.100 6379

    This makes Redis sync data from the given master.