Top Banner
Redis 맛보기 [email protected]
27

Redis 맛보기

May 06, 2015

Download

Software

Ki-Sung Bae

Redis 101
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Redis 맛보기

Redis 맛보기[email protected]

Page 2: Redis 맛보기

Redis● REmote DIctionary Server● In-memory, but can be persisted● NoSQL Key-value store● Written in ANSI C● No threading, just one process

Page 3: Redis 맛보기

The Most Popular Key-Value Store

Page 4: Redis 맛보기

Why Redis?● Fast● Easy to learn● Various data types● Solve a specific set of problems

○ Good: Caching, Statistical data, Recoverable state, Worker Queue

○ Bad: 100% consistent dataset required, Data is larger than memory

Page 5: Redis 맛보기

Running & Connecting

● redis-server● redis-cli● http://try.redis.io/

Page 6: Redis 맛보기

Redis Protocol

redis[“key”] = “value”

Page 7: Redis 맛보기

Key● Redis key is Binary safe, this means that you can use

any binary sequence as a key:○ string○ content of a JPEG file○ also, empty string

Page 8: Redis 맛보기

Rules about Keys● Too long keys are not a good idea

○ memory-wise, costly key-comparisons● Too short keys are often not a good idea

○ more readable○ id:10:pass => id:10:password

● Try to stick with a schema○ “object-type:id:field” => “comment:1234:reply.to”

Page 9: Redis 맛보기

Value types

● String ● List of strings● Sets of strings● Sorted sets of strings● Hashes where keys and values are strings

Page 10: Redis 맛보기

String

● The simplest Redis type● A value can’t be bigger than 512 MB● SET ● GET ● INCR/INCRBY, DECR/DECRBY ● GETSET

Page 11: Redis 맛보기

127.0.0.1:6379> set mykey 100

OK

127.0.0.1:6379> get mykey

"100"

127.0.0.1:6379> incr mykey

(integer) 101

127.0.0.1:6379> incrby mykey 5

(integer) 106

127.0.0.1:6379> decr mykey

(integer) 105

127.0.0.1:6379> decrby mykey 2

(integer) 103

127.0.0.1:6379> getset mykey 1

"103"

127.0.0.1:6379> get mykey

"1"

Example Of String

Page 12: Redis 맛보기

List

● Implemented via Linked Lists● LPUSH

○ add a new element into a list at the head● RPUSH

○ add a new element into a list at the tail● LRANGE

Page 13: Redis 맛보기

127.0.0.1:6379> incr next.news.id

(integer) 1

127.0.0.1:6379> set news:1:title "Redis is simple"

OK

127.0.0.1:6379> set news:1:url "http://code.google.com/redis"

OK

127.0.0.1:6379> lpush submitted.news 1

(integer) 1

Push IDs instead of the actual data

reddit.com example:

Page 14: Redis 맛보기

Sets

● Unordered collections of unique strings● SADD ● SMEMBERS

● SISMEMBER

● SINTER○ the intersection between different sets

Page 15: Redis 맛보기

Sorted Sets

● Collections of unique strings with associated score

● ZADD○ O(log(N))

● ZRANGE/ZREVRANGE● ZRANGEBYSCORE

Page 16: Redis 맛보기

ZADDAdd a few selected hackers with their year of birth as score

127.0.0.1:6379> zadd hackers 1940 "Alan Kay"

(integer) 1

127.0.0.1:6379> zadd hackers 1953 "Richard Stallman"

(integer) 1

127.0.0.1:6379> zadd hackers 1965 "Yukihiro Matsumoto"

(integer) 1

127.0.0.1:6379> zadd hackers 1916 "Claude Shannon"

(integer) 1

127.0.0.1:6379> zadd hackers 1969 "Linus Torvalds"

(integer) 1

127.0.0.1:6379> zadd hackers 1912 "Alan Turing"

(integer) 1

Page 17: Redis 맛보기

ZRANGE/ZREVRANGEAsk for sorted hackers

127.0.0.1:6379> zrange hackers 0 -1

1) "Alan Turing"

2) "Claude Shannon"

3) "Alan Kay"

4) "Richard Stallman"

5) "Yukihiro Matsumoto"

6) "Linus Torvalds"

127.0.0.1:6379> zrevrange hackers 0 -1

1) "Linus Torvalds"

2) "Yukihiro Matsumoto"

3) "Richard Stallman"

4) "Alan Kay"

5) "Claude Shannon"

6) "Alan Turing"

Page 18: Redis 맛보기

Sorted Set by different ordering

● Use SORT command ○ Server will waste CPU

● Make another sorted set○ An alternative for having multiple orders is to add

every element in multiple sorted sets at the same time.

Page 19: Redis 맛보기

Updating the scores

● Just calling again ZADD

Page 20: Redis 맛보기

Hashes

● HSET ● HGET ● HGETALL ● HKEYS

Page 21: Redis 맛보기

127.0.0.1:6379> hset users:1 username poby

(integer) 1

127.0.0.1:6379> hget users:1 username

"poby"

127.0.0.1:6379> hmset users:1 age 10 bestfriend pororo

OK

127.0.0.1:6379> hgetall users:1

1) "username"

2) "poby"

3) "age"

4) "10"

5) "bestfriend"

6) "pororo"

127.0.0.1:6379> hkeys users:1

1) "username"

2) "age"

3) "bestfriend"

127.0.0.1:6379> hdel users:1 age

(integer) 1

Example Of Hash

Page 22: Redis 맛보기

Redis Clients

http://redis.io/clients

Page 23: Redis 맛보기

Demo: realtime-info

AWS ElastiCacherealtime-info

EDM1EDM1

EDM1EDM1

EDM1EDM1

API Server

Log Server

Page 24: Redis 맛보기

Demo: Realtime-Info (1)

SET realtime-info by Poby

Page 25: Redis 맛보기

Demo: Realtime-Info (2)

MGET realtime-info by API

Page 27: Redis 맛보기