Difference between revisions of "Redis"
(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....") |
|||
Line 93: | Line 93: | ||
Et voilà! You now have a fully functioning Redis key-value store! | 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 | ||
+ | <pre> | ||
+ | #!/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!" | ||
+ | </pre> | ||
+ | |||
+ | ==AWS ElastiCache with Redis== | ||
+ | |||
+ | In order to connect to my ElastiCache cluster running Redis on [[:Category:AWS|AWS]], I had to spin up an EC2 instance (Amazon AMI) and run the following commands: | ||
+ | |||
+ | $ sudo su - | ||
+ | $ yum install gcc -y | ||
+ | $ wget <nowiki>http://download.redis.io/redis-stable.tar.gz</nowiki> -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 | ||
==External links== | ==External links== |
Revision as of 23:33, 26 July 2016
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.
Contents
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
External links
- Official website
- Redis commands — a full list of all Redis commands