Redis

Wenzhi Xu 

Redis is an in-memory data storage software. 

FAST

OPEN-SOURCE

SIMPLE

NOSQL

POPULAR

CROSS-PLATFORM

Where to use it?

1. Caching - Speed up your application

2. Leaderboard - Sort by weight

3. Database - Store anything you want

4. Message Queue - asynchronous program

5. Session Store - Centralized storage of SESSION data

...

 

1. Basic Usage

2. Redis Data Structure

3. Redis Network Model

...

Install

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
By source code
yum install redis
# or
apt-get install redis
By package installer

After installation we'll get

1. redis-server : Start redis server

2. redis-cli : To connect redis server

3. redis-benchmark : To test redis performance

4. redis-check-aof and redis-check-rdb : To check data persistence on disk

5. redis-sentinel : monitor

Start Redis

Connect to Redis

Store new value

Data Types in Redis

1.  String     - set, get, del, expire
2. List          - lpush, lpop, rpush,rpop, lrange, lindex
3. Hash       - hset, hget, hgetall,
hdel, hexists
4. Set          - sadd, smembers, srem
5. Sorted Set - zadd, zrem, zrange

Don't confuse about data types and data structure. Data types can be implemented by one or more data structures.

String Usage

redis> get school
City University of Seattle.
redis> set school "CityU Seattle"
OK
redis> get school
CityU Seattle
redis> set F324FSF@$FSFF@#$#@$#@FFS4324424324 '{userId:
1, first:"Wenzhi", last:"Xu"}'
OK
redis> get F324FSF@$FSFF@#$#@$#@FFS4324424324
{userId:1, first:"Wenzhi", last:"Xu"}

redis> expire school 10
1
redis> get school
CityU Seattle
# after 10 seconds
redis> get school
null

List Usage

redis> lpush task "CT"
1
redis> rpush task "DB"
2
redis> rpush task "KC"
3
redis> rpush task "PE"
4
redis> lrange 0 10
ERR wrong number of arguments for 'lrange' command
redis> lrange task 0 10
0 CT
1 DB
2 KC
3 PE
redis> lpush task "MP"
5
redis> lrange task 0 10
0 MP
1 CT
2 DB
3 KC
4 PE

Hash Usage

redis> hset state WA Washington
1
redis> hset state CA California
1
redis> hset state NY New York
ERR wrong number of arguments for 'hset' command
redis> hset state NY 'New York'
1
redis> hget state NY
New York

redis> hset session F324FSF@$FSFF@#$#@$#@FFS4324424324
'{userId:1, first:"Wenzhi", last:"Xu"}'
1
redis> hget session F324FSF@$FSFF@#$#@$#@FFS4324424324
{userId:1, first:"Wenzhi", last:"Xu"}

As you can see, we can use Hash to store SESSION as well.

Which one should we use?

Sorted Set Usage

redis> zadd billboard 0 "Drivers License"
1
redis> zadd billboard 1 "Mood"
1
redis> zadd billboard 3 "Blinding Lights"
1
redis> zadd billboard 2 "Positions"
1
redis> zadd billboard 6 "Holy"
1
redis> zadd billboard 5 "Levitating"
1
redis> zadd billboard 4 "Go Crazy"
1
redis> zrange billboard 0 10 
0 Drivers License
1 Mood
2 Positions
3 Blinding Lights
4 Go Crazy
5 Levitating
6 Holy

# get top 3
redis> zrange billboard 0 2
0 Drivers License
1 Mood
2 Positions

Caching?

Assume Blackboard cache our profile data for speed up.

get_user_profile(user_id)

   cached_user_profile = redis.get(user_id)
   
   if cached_user_profile != nil:
   		return cached_user_profile
   
   user_profile = db.get_user()
   
   return user_profile

Language Supporting

https://redis.io/clients

For Python

In [1]: import redis

In [2]: r = redis.Redis(host='localhost', port=6379, db=0, password="foobared")

In [3]: r.get('school')
Out[3]: 'CityU Seatlle'

QA

Reference

https://redis.io/commands
https://redis.io/commands/expire
https://aws.amazon.com/elasticache/redis/​

Thanks!

Redis

By xuwenzhi

Redis

  • 218