Redis

From Christoph's Personal Wiki
Revision as of 22:06, 26 July 2016 by Christoph (Talk | contribs) (Created page with "'''Redis''' is a data structure server. It is open-source, networked, in-memory, and stores keys with optional durability. More simply, Redis is an in-memory key-value store....")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Redis is a data structure server. It is open-source, networked, in-memory, and stores keys with optional durability. More simply, Redis is an in-memory key-value store.

Install and configure Redis

Note: This setup will be for Ubuntu 16.04 (LTS; 64-bit).

  • Install Redis dependencies:
$ sudo apt-get update
$ sudo apt-get install build-essential tcl
  • Download and extract the latest stable version of Redis:
$ sudo wget http://download.redis.io/redis-stable.tar.gz -P /usr/local/src
$ cd /usr/local/src
$ sudo xzvf redis-stable.tar.gz
$ cd redis-stable
  • Compile and install Redis:
$ make
$ make test
$ sudo make install

By default, the above will install the Redis binaries in /usr/local/bin

  • Configure Redis:
$ sudo mkdir /etc/redis
$ sudo cp redis.conf /etc/redis

By default, the Redis supervision tree is set to "no" (i.e., no supervision interaction with upstart or systemd). Since Ubuntu 16.04 uses systemd, we will set the supervision to "systemd" like so:

$ sudo sed -i 's/^supervised no/supervised systemd/' /etc/redis/redis.conf
  • Change Redis working directory (i.e., the directory that Redis will use to dump persistent data):
$ sudo sed -i 's,^dir ./,dir /var/lib/redis,' /etc/redis/redis.conf
  • Create a Redis systemd unit file:
$ sudo vi /etc/systemd/system/redis.service  # add the following lines to this file:
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
  • Create the Redis user, group, directory, and permissions:
$ sudo adduser --system --group --no-create-home redis
$ sudo mkdir /var/lib/redis
$ sudo chown redis:redis /var/lib/redis
$ sudo chmod 0770 /var/lib/redis
  • Start the Redis service:
$ sudo systemctl start redis
$ sudo systemctl status redis.service

If Redis was installed and configured properly, you should see something that resembles the following:

● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2016-04-16 14:55:22 PDT; 1min 1s ago
 Main PID: 8545 (redis-server)
    Tasks: 3
   Memory: 880.0K
      CPU: 38ms
   CGroup: /system.slice/redis.service
           └─8545 /usr/local/bin/redis-server 127.0.0.1:6379
  • Test that the Redis service is functioning properly:
$ redis-cli ping  # You should see "PONG" as the reply
  • Test that you can set and retrieve key/values:
$ redis-cli
127.0.0.1:6379> set test "Hello, world!"
OK
127.0.0.1:6379> get test
"Hello, world!"
127.0.0.1:6379> exit
  • Make sure Redis key-values survive service restart and/or machine reboots (i.e., make sure your Redis data are persistent):
$ sudo systemctl restart redis
$ redis-cli get test
"Hello, world!"
  • Finally, enable the Redis service so that it automatically starts up on machine boot:
$ sudo systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.

Et voilà! You now have a fully functioning Redis key-value store!

External links