Redis is an advanced key-value store or a data structure server. This presentation will cover the following topics:
* An overview of Redis
* Data Structures
* Basics of Setup and Installation
* Basics of Administration
* Programming with Redis
* Considerations of Running Redis in a Virtual Machine
* Redis Resources There will be a number of demonstrations to help explain some of the concepts being presented.
1. Getting Started with Redis
Toronto Pivotal User Group
Syed Faisal Akber
VMware, Inc.
2014-08-20
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 1 / 37
2. 1 Overview of Redis
2 Building and Installing Redis
3 Starting Redis
4 Data Structures
5 Programming with Redis
6 Basics of Administration
7 Advanced Topics
8 Conclusions
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 2 / 37
3. Overview of Redis
The word Redis means REmote DIctionary Server
It is an advanced key-value store or a data
structure store
Runs entirely in memory
All data is kept in memory
Quick data access since it is maintained in memory
Data can be backed up to disk periodically
Single threaded server
Extensible via Lua scripts
Able to replicate data between servers
Clustering also available
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 3 / 37
4. Building and Installing Redis
Install from source
Download from http://redis.io/download
Download the Stable release
# tar -xvzf redis-2.8.13.tar.gz
# cd redis-2.8.13
# make
# make test
# make PREFIX=/path/to/install/directory install
Packages for Mac OS X are available via Brew
Pivotal Redis repository has a prebuilt package for
RedHat
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 4 / 37
5. Starting Redis
Starting the Redis server use the redis-server
binary
Once the server starts, you can connect to it using
your programs or the command-line utility
redis-cli
The default port that Redis will use is 6379 and
listens on all interfaces
The Redis server will look for the configuration file
in the current directory
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 5 / 37
7. Strings I
For each key, the value is a binary safe string
Simplest data structure
127.0.0.1:6379> set key "value of possible values"
OK
127.0.0.1:6379> get key
"value of possible values"
127.0.0.1:6379> del key
(integer) 1
127.0.0.1:6379> get key
(nil)
Numeric operations
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 7 / 37
8. Strings II
Set the value to a number and perform numeric
operations
127.0.0.1:6379> set counter1 0
OK
127.0.0.1:6379> set counter2 4
OK
127.0.0.1:6379> INCRBY counter2 5
(integer) 9
127.0.0.1:6379> INCR counter1
(integer) 1
127.0.0.1:6379> decr counter2
(integer) 8
127.0.0.1:6379> mget counter1 counter2
1) "1"
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 8 / 37
9. Strings III
2) "8"
Bit fields
Set and perform bit-wise operations on strings
127.0.0.1:6379> setbit field0 5 1
(integer) 0
127.0.0.1:6379> get field0
"x04"
127.0.0.1:6379> setbit field1 4 1
(integer) 0
127.0.0.1:6379> setbit field1 5 1
(integer) 0
127.0.0.1:6379> bitop AND afields field0 field1
(integer) 1
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 9 / 37
10. Strings IV
127.0.0.1:6379> bitop OR ofields field0 field1
(integer) 1
127.0.0.1:6379> get afields
"x04"
127.0.0.1:6379> get ofields
"x0c"
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 10 / 37
11. Lists I
Lists keep track of a number of items under the
same key
Can be used as a stack
127.0.0.1:6379> lpush list 1 2 3
(integer) 3
127.0.0.1:6379> lpush list 4
(integer) 4
127.0.0.1:6379> llen list
(integer) 4
127.0.0.1:6379> lset list 2 0
OK
127.0.0.1:6379> lpop list
"4"
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 11 / 37
12. Lists II
127.0.0.1:6379> llen list
(integer) 3
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 12 / 37
13. Sets
A set is a collection of items
Does not allow duplicates
Mathematical set operations work here
127.0.0.1:6379> sadd set0 5 4 3 2 1
(integer) 5
127.0.0.1:6379> sadd set1 4 5 6 7 8
(integer) 5
127.0.0.1:6379> sinter set0 set1
1) "4"
2) "5"
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 13 / 37
14. Ordered Sets
Ordered sets are similar to normal sets but retain
sorting/order
Set members carry a score to indicate user-defined
sorting order
127.0.0.1:6379> zadd newset 6 a 2 b 4 c 4 d 1 e 6 f
(integer) 6
127.0.0.1:6379> zrange newset 0 6
1) "e"
2) "b"
3) "c"
4) "d"
5) "a"
6) "f"
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 14 / 37
15. Hashes I
Each key can have subkeys (called fields) with its
own values
Consider a structure in C
struct users{
char *name;
int uid;}
To store this in Redis use the HSET command
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 15 / 37
16. Hashes II
127.0.0.1:6379> HSET users:rose name "Rose Tyler"
(integer) 1
127.0.0.1:6379> HSET users:rose uid 1
(integer) 1
127.0.0.1:6379> HGETALL users:rose
1) "name"
2) "Rose Tyler"
3) "uid"
4) "1"
127.0.0.1:6379> HKEYS users:rose
1) "name"
2) "uid"
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 16 / 37
17. Message Passing
A publish/subscribe mechanism is present for
broadcasting messages
It is lightweight and easy to use
Servers broadcast messages to channels
Clients subscribe to channels of interest
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 17 / 37
18. Programming with Redis
There are many “clients” or drivers available
Most are very mature
They include:
C
Common Lisp
Java
Perl
Prolog
Python
Ruby
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 18 / 37
19. Perl
One of several Perl clients, the recommended client
is Redis from CPAN
my $redis = Redis->new(
server=’redis.example.com:8080’,
name=’my_connection_name’);
$redis->get(’key’);
$redis->set(’key’ => ’value’);
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 19 / 37
20. Python
Install the driver using pip
import redis
redis = redis.Redis(host=’redis.example.com’,
port=8080, db=0)
redis.sadd(’team1’, ’player:mike’, ’player:jane’,
’player:joe’)
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 20 / 37
21. Java
The recommended client is Jedis
Jedis jedis = new Jedis("redis.example.com");
jedis.set("foo", "bar");
String value = jedis.get("foo");
Use it as a Maven dependency or download the jar
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 21 / 37
22. Basics of Administration I
Ensure enough memory and other resources are
available for Redis
Setup the configuration file (start with sample)
Configure network bind addresses
Configure how often to write to disk
Configure Operating System
Increase the file descriptor limit if there’s a large number
of connections
Add vm.overcommit memory = 1 to
/etc/sysctl.conf to prevent failure of background
saves under low memory connections
Configure start-up scripts
Configure backup jobs
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 22 / 37
23. Running Redis in a Virtual Machine
Configure the Memory Reservation to be 100% of
the Allocated Memory
Ensure enough disk space is allocated to Virtual
Machine
Resource Pools should have adequate resources
allocated
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 23 / 37
24. Advanced Topics
Below are some topics to research afterwards
Replication
Backups
Sentinel
Extensibility via Lua scripts
Transactions
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 24 / 37
25. Replication
Automatically have all of the data replicated to
remote servers
Use replication for redundancy
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 25 / 37
26. Backups
Disable disk writing on primary servers to improve
performance
Take backups of secondary servers
Use either append-only files or the snapshot
mechanism
Employ third-party backup software to protect save
files
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 26 / 37
27. Sentinel
Sentinel utility to make Redis servers Highly
Available
Features include:
Monitoring
Notification
Automatic Failover
Configuration provider
Uses Master/Slave topology
Slaves are in read-only mode
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 27 / 37
28. Scripting with Lua
Small lightweight scripting platform
Lua is similar to BASIC
Allows for extensibility of Redis
Offload processing to Redis for performance
Analogous to RDBMS stored procedures
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 28 / 37
29. Transactions and Pipelining
Redis allows for transaction based operations for
atomicity
Commands can be pipelined to improve performance
Pipelining can be used within transactions
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 29 / 37
30. Conclusions
Redis is a memory based datastore
Redis is FAST
It can store data in different structures
Programming with Redis is easy
Flexible administration options
Redis has some neat features
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 30 / 37
31. Acknowledgements
Salvatore Sanfilippo – Pivotal
Pieter Noordhuis – Pivotal
Dan Buchko – Pivotal
Luke Shannon – Pivotal
Matt Stancliff – Pivotal
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 31 / 37
32. References I
Seguin, Karl. The Little Redis Book
http://openmymind.net/redis.pdf
Macedo, Tiago and Fred Oliveira. 2011. Redis
Cookbook O’Reilly Media, Sebastopol, U.S.A.
Redis Web-Site http://redis.io/
Redis Documentation
http://redis.io/documentation
Redis Download http://redis.io/download
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 32 / 37
33. References II
Redis Documentation
http://redis.io/documentation
Redis Commands http://redis.io/commands
redissentinel Redis Sentinel Documentation
http://redis.io/topics/sentinel
The Programming Language Lua
http://www.lua.org/
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 33 / 37
34. References III
Salihefendic, Amir. 2010. Redis – The Hacker’s
Database (Google Tech Talk Video)
http://youtu.be/1BS3UVSLX-I
Virtual Machine Memory http://vmw.re/1BBkstW
Redis CPAN
http://search.cpan.org/~melo/Redis-1.961/lib/
Jedis Repository
https://github.com/xetorthio/jedis
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 34 / 37
35. References IV
Redmond, Eric and Jim R. Wilson. 2012. Seven
Databases in Seven Weeks: A Guide to Modern
Databases and the NoSQL Movement Pragmatic
Bookshelf.
RestMQ – Redis based message queue
http://restmq.com/
S. F. Akber (VMware, Inc.) Getting Started with Redis 2014-08-20 35 / 37