Redis

Installation

To install the latest version of Redis:

  • Type this in Terminal:
    brew install redis
    

Start/Restart/Stop Redis

  • To start the Redis, type this in Terminal:

    brew services start redis
    
  • To restart:

    brew services restart redis
    
  • To stop: (or prevent Redis from started again when the Mac is reboot)

    brew services stop redis
    
  • Notes: If brew services list showing the service status is started, the service will become started again when the Mac is reboot.

Redis Sentinel

  • Skip this part if you don't need to run Redis in Sentinel mode
  • Create LaunchAgent /usr/local/opt/redis/homebrew.mxcl.redis-sentinel.plist
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
      <key>KeepAlive</key>
      <dict>
        <key>SuccessfulExit</key>
        <false/>
      </dict>
      <key>Label</key>
      <string>homebrew.mxcl.redis-sentinel</string>
      <key>ProgramArguments</key>
      <array>
        <string>/usr/local/bin/redis-sentinel</string>
        <string>/usr/local/etc/redis-sentinel.conf</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>WorkingDirectory</key>
      <string>/usr/local/var</string>
      <key>StandardErrorPath</key>
      <string>/usr/local/var/log/redis-sentinel.log</string>
      <key>StandardOutPath</key>
      <string>/usr/local/var/log/redis-sentinel.log</string>
    </dict>
    </plist>
    
  • Enable the LaunchAgent
    cd ~/Library/LaunchAgents
    ln -s /usr/local/opt/redis/homebrew.mxcl.redis-sentinel.plist homebrew.mxcl.redis-sentinel.plist
    launchctl load homebrew.mxcl.redis-sentinel.plist
    
  • To verify:
    redis-cli -p 26379
    

Known issue

  • brew services list may return status: error on redis service once you load Redis Sentinel
  • But actually the Redis is running properly
  • If you unload the Redis Sentinel like this launchctl unload homebrew.mxcl.redis-sentinel.plist, brew services list will return status to started again
  • No solution found yet. If you know any workaround / fix, please let me know!

Multiple Redis servers

  • Skip this part if you don't need to run multiple Redis servers on the same Mac

  • This is to create another Redis server running port 7179 and Redis Sentinel on 27179

  • Create data folder

    mkdir -p /usr/local/var/db/redis_7179
    
  • Create config file for another Redis server /usr/local/etc/redis_7179.conf

    port 7179
    pidfile /var/run/redis_7179.pid
    logfile "/usr/local/var/log/redis_7179.log"
    dir /usr/local/var/db/redis_7179/
    databases 1000
    
    bind 127.0.0.1 ::1
    bind 127.0.0.1
    protected-mode yes
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    loglevel notice
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
    
  • All of the settings are default except the 5 lines at the beginning. For details, refer to /usr/local/etc/redis.conf

  • Create config file for another Redis Sentinel /usr/local/etc/redis-sentinel_27179.conf

    port 27179
    sentinel monitor mymaster 127.0.0.1 7179 2
    
    sentinel down-after-milliseconds mymaster 30000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    dir /tmp
    
  • All of the settings are default except the 2 lines at the beginning. For details, refer to /usr/local/etc/redis-sentinel.conf

  • Create LaunchAgent plist for Redis Server: /usr/local/opt/redis/homebrew.mxcl.redis_7179.plist

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>KeepAlive</key>
        <dict>
          <key>SuccessfulExit</key>
          <false/>
        </dict>
        <key>Label</key>
        <string>homebrew.mxcl.redis_7179</string>
        <key>ProgramArguments</key>
        <array>
          <string>/usr/local/bin/redis-server</string>
          <string>/usr/local/etc/redis_7179.conf</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>/usr/local/var</string>
        <key>StandardErrorPath</key>
        <string>/usr/local/var/log/redis_7179.log</string>
        <key>StandardOutPath</key>
        <string>/usr/local/var/log/redis_7179.log</string>
      </dict>
    </plist>
    
  • Create LaunchAgent plist for Redis Sentinel: /usr/local/opt/redis/homebrew.mxcl.redis-sentinel_27179.plist

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>KeepAlive</key>
        <dict>
          <key>SuccessfulExit</key>
          <false/>
        </dict>
        <key>Label</key>
        <string>homebrew.mxcl.redis-sentinel_27179</string>
        <key>ProgramArguments</key>
        <array>
          <string>/usr/local/bin/redis-sentinel</string>
          <string>/usr/local/etc/redis-sentinel_27179.conf</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>/usr/local/var</string>
        <key>StandardErrorPath</key>
        <string>/usr/local/var/log/redis-sentinel_27179.log</string>
        <key>StandardOutPath</key>
        <string>/usr/local/var/log/redis-sentinel_27179.log</string>
      </dict>
    </plist>
    
  • Enable the LaunchAgents:

    cd ~/Library/LaunchAgents
    ln -s /usr/local/opt/redis/homebrew.mxcl.redis_7179.plist homebrew.mxcl.redis_7179.plist
    ln -s /usr/local/opt/redis/homebrew.mxcl.redis-sentinel_27179.plist homebrew.mxcl.redis-sentinel_27179.plist
    launchctl load homebrew.mxcl.redis*7179.plist
    
  • To verify:

    redis-cli -p 7179
    redis-cli -p 27179
    

results matching ""

    No results matching ""