From Christoph's Personal Wiki
Jump to: navigation, search

Redis (aka Remote Dictionary Server) 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 -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:
Description=Redis In-Memory Data Store

ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown

  • 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
  • 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> set test "Hello, world!"
OK> get test
"Hello, world!"> 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/ to /etc/systemd/system/redis.service.

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

Redis with Python

  • First, install the Python Redis module:
$ sudo pip install redis -y
  • Simple Redis with Python commands:
$ cat
#!/usr/bin/env python
import redis

redis_endpoint = "localhost"
redis_port = 6379
r = redis.StrictRedis(host=redis_endpoint, port=redis_port, db=0)

r.set('test', "Hello, world!")
r.get('test') #=> "Hello, world!"

AWS ElastiCache with Redis

In order to connect to my ElastiCache cluster running Redis on AWS, I had to spin up an EC2 instance (Amazon AMI) and run the following commands:

$ sudo su -
$ yum install gcc -y
$ wget -P /usr/local/src
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make distclean
$ cd deps
$ make hiredis jemalloc linenoise lua
$ cd ../
$ make
  • Test that you can connect to your AWS ElastiCache cluster read endpoint(s):
$ export # Use your actual endpoint
$ export # Use your actual endpoint
$ src/redis-cli -h ${READ_ENDPOINT_A} -p 6379 ping

Redis on Kubernetes

  • Test Redis connection from a Busybox Pod:
$ kubectl -n gateway-service get svc redis
NAME    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)             AGE
redis   ClusterIP   <none>        6379/TCP,9121/TCP   107m

$ kubectl run -n gateway-service -i --rm --tty busybox --image=busybox -- sh
If you don't see a command prompt, try pressing enter.
/ # telnet 6379
Connected to
AUTH <redacted_redis_password>

$ kubectl run -i --rm --tty redisbox -- sh
# redis-cli -h -a <redacted_redis_password> info
# redis-benchmark -c 100 -n 100000 -d 1024 -r 100000 -t PING,SET,GET,INCR,LPUSH,RPUSH,LPOP,RPOP,SADD,SPOP,MSET -h -a <redacted_redis_password> -q

See: How fast is Redis? for details.

  • Using Init Containers:
    - name: wait-for-redis
      image: busybox
      - /bin/sh
      - -c
      - >
        set -x;
        until ((printf "AUTH <password>\r\n"; sleep 1) | nc <redis-host> 6379);
        do echo waiting for redis;
        sleep 2; done

External links