4. What is Redis?
REmote DIctionary Server (Data Structure server)
Redis is an open source, advanced key-value store
Written in c
Solution for building high-performance, scalable web
applications.
In-memory storage,with persistence.
Rich set of data types
Redis can replicate data to any number of slaves.
6. Why Redis?
Simple to learn
Lightweight text-based tcp protocol
Extremely fast and versatile - in RAM
Rapidly growing
Widely supported (C, .NET, PHP, Java... more)
Disk-backed, Background writes!
Master-Slave configurations
Open source , awesome community
7. Advantages of Redis
Exceptionally Fast (110000 SETs per second, 81000 GETs per
second)
Supports Rich data types
Snapshots
Atomic Operations
Replication
Sharding
MultiUtility Tool (caching, messaging-queues, any short lived data
in application like web application sessions, web page hit counts,
etc)
8. Comparision with other key-value stores
Complex data types, with atomic operations
Redis is an in-memory but persistent on disk database
very high write and read speed
Memory representation of complex data structures is much
simpler
Little internal complexity.
9. Memcache Vs Redis
Memcached Redis
Description In-memory key-value store,
originally intended for caching
In-memory database with
configurable options performance
vs. persistency
Initial
release
2003 2009
Transaction
concepts
none optimistic locking
Partitioning
methods
none Sharding
Durability no yes
Replication
methods
none Master-slave replication
10. Memcache Vs Redis
Memcached Redis
Memory
Usage
For simple key-value pairs
memcached is more memory
efficient than Redis
If use Redis hashes, then Redis is
more memory efficient.
Persistence Data lost with a restart &
rebuilding cache is a costly
process
Persistent data. Redis syncs to disk
at every 2 seconds.
Storage
Type
Stores variables in
memory
Database that resides in
memory
Read/ Write
Speed
Heavy Read Heavy Read & Write
Key Length 250 bytes max 2 GB max
Data
Structure
String & integer
Object: serialize
stronger data structures
11. Install Redis on Ubuntu
$sudo apt-get install redis-server
Check if redis is working?
$redis-cli
Run commands on remote server
redis-cli -h host -p port -a password
12. Install Redis on Ubuntu
$ wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz
$ tar xzf redis-2.4.2.tar.gz
$ cd redis-2.4.2
$ make
Start Redis: redis-server
13. Install Redis Desktop Manager on Ubuntu
Will provide UI to manage your redis keys and data.
Download from : http://redisdesktop.com/download
Install the downloaded file
14. Ping – Pong Game
Install Redis on your systems
16. Redis Key Commands (Cont..)
PEXPIREAT key milliseconds-timestamp
KEYS pattern
MOVE key db (move key to another db)
PERSIST (Remove expiration)
TTL (Remaining time in key expiry)
PTTL (Remaining time in key expiry in milliseconds)
RANDOMKEY
RENAME key newkey (RENAMENX)
TYPE key
17. Key => { Data Structures }
KEYKEY
““I am a plain text string”I am a plain text string”
C A A B DC A A B D
A B C D EA B C D E
A : 0.1 B : 0.2 C : 50 D : 50A : 0.1 B : 0.2 C : 50 D : 50
KEY 1KEY 1
KEY 2KEY 2
Value 1Value 1
Value 2Value 2
Hash Tables
(objects)
Strings/Bitmaps
Lists
Sets
Sorted Sets
String
23. Redis – Hashes
Collection of key value pair
Maps b/w string fields and string values
Used to represent objects
Can store upto more thn 4 billion field-value pairs
25. Redis – Hashes Commands
HSET key field val /HMSET
HGET key field /HMGET key field1 field2
HGETALL key
HDEL key field1 field2
HEXISTS key field
HINCRBY key field inc / HINCRBYFLOAT
HKEYS key
HLEN key
HSETNX key field value
HVALS key
HSCAN
26. Redis – Hashes Example
redis 127.0.0.1:6379> HMSET session name "Intro to redis" description "redis basic
commands for caching" attendees 20 absent 5
OK
redis 127.0.0.1:6379> HGETALL session
1) "name"
2) "Intro to redis"
3) "description"
4) "redis basic commands for caching"
5) "attendees"
6) "20"
7) "absent"
8) "5"
27. Redis – Lists
List of strings, sorted by insertion order
Can add elements on head or on tail
Maximum length of a list is 232 - 1 elements (4294967295, more than
4 billion of elements per list)
28. Redis – Lists
Redis Key
HEAD
Value2
Value4 TAIL
Value3
Value1
Value : Redis List
32. Redis – Sets
Unordered collection of unique strings
Can add, remove and test for existence
Diff, intersect, merge
Maximum length of a list is 232 - 1 elements (4294967295, more than
4 billion of elements per set)
37. Redis – Sorted Sets
Similar to Set
Every member of a Sorted Set is associated with score, that is used in
order to take the sorted set ordered, from the smallest to the
greatest score.
Ordered by score
Members are unique, scores may be repeated.
Can add, remove and test for existence
Maximum length of a list is 232 - 1 elements (4294967295, more than
4 billion of elements per set)
42. Redis – Publish Subscribe
Messaging system
The link by which messages are transferred is called channel
A client can subscribe any number of channels.
redis 127.0.0.1:6379> SUBSCRIBE redisChat
redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching
technique"
44. Redis – Transactions
Allow the execution of a group of commands in a single step
Two properties:
− All commands are serialized and executed sequentially
− Either all commands or no commands are processed
Iinitiated by command MULTI
Executed by EXEC command
49. Redis – Security
Redis database can be make secured
Making connection needs to authenticate before executing
command.
127.0.0.1:6379> CONFIG get requirepass
50. Redis – Security Commands
127.0.0.1:6379> CONFIG set requirepass "intellimeet"
127.0.0.1:6379> CONFIG get requirepass
1) "requirepass"
2) "intellimeet"
NOAUTH Authentication required error
Client needs to use AUTH command to authenticate himself.
127.0.0.1:6379> AUTH "intellimeet"
127.0.0.1:6379> SET mykey "Test value"
127.0.0.1:6379> GET mykey
"Test value"
51. Redis – Backup
SAVE command is used to create back up of current redis database
This command will create dump.rdb file in your redis directory.
To restore redis data just move redis backup file (dump.rdb) into your
redis directory and start the server.
CONFIG get dir : to get redis directory
52. Redis – Backup (Cont..)
BGSAVE start the backup process and run in background
Default configuration:
− Save after 900 sec (15 min) if at least 1 key changed
− Save after 300 sec (5 min) if at least 10 keys changed
− Save after 60 sec if at least 10000 keys changed
53. Redis – Some other useful Commands
DBSIZE
SELECT
FLUSHALL
FLUSHDB
INFO
LASTSAVE (Last unsuccessful save)
MONITOR
SHUTDOWN
SLAVEOF
54. Redis – Benchmarks
Utility to check the performance of redis
Running n commands simultaneously
redis-benchmark [option] [option value]
redis-benchmark -n 100000
redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 100000 -q
SET: 146198.83 requests per second
LPUSH: 145560.41 requests per second
55. Why not Redis?
Single Threaded
Significant overhead for persistence
Pick your risk level Vs performance
If your db is too big - redis can handle swapping on its own.
Keys remain in memory and least used values are swapped to
disk.
Swapping IO happens in separate threads
But if you need this - don't use redis, or get a bigger machine ;)
57. Other use case ideas
Geo Resolving with geohashing
− Implemented and opened by yours truly
https://github.com/doat/geodis
Real time analytics
− use ZSET, SORT, INCR of values
API Key and rate management
− Very fast key lookup, rate control counters using INCR
Real time game data
− ZSETs for high scores, HASHES for online users, etc
Database Shard Index
− map key => database id. Count size with SETS