Redis

From Christoph's Personal Wiki
Revision as of 01:05, 24 September 2021 by Christoph (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 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!

Redis with Python

  • First, install the Python Redis module:
$ sudo pip install redis -y
  • Simple Redis with Python commands:
$ cat redis_test.py
#!/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 http://download.redis.io/redis-stable.tar.gz -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 READ_ENDPOINT_A=foo-001.rdwty2.0001.usw2.cache.amazonaws.com # Use your actual endpoint
$ export READ_ENDPOINT_B=foo-002.rdwty2.0001.usw2.cache.amazonaws.com # Use your actual endpoint
$ src/redis-cli -h ${READ_ENDPOINT_A} -p 6379 ping
# PONG

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   10.231.86.56   <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 10.231.86.56 6379
Connected to 10.231.86.56
AUTH <redacted_redis_password>
+OK
PING
+PONG

$ kubectl run -i --rm --tty redisbox --image=gcr.io/google_containers/redis:v1 -- sh
# redis-cli -h 10.231.86.56 -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 10.231.86.56 -a <redacted_redis_password> -q

See: How fast is Redis? for details.

  • Using Init Containers:
  initContainers:
    - name: wait-for-redis
      image: busybox
      command:
      - /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