Redis 맛보기[email protected]
Redis● REmote DIctionary Server● In-memory, but can be persisted● NoSQL Key-value store● Written in ANSI C● No threading, just one process
The Most Popular Key-Value Store
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
Running & Connecting
● redis-server● redis-cli● http://try.redis.io/
Redis Protocol
redis[“key”] = “value”
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
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”
Value types
● String ● List of strings● Sets of strings● Sorted sets of strings● Hashes where keys and values are strings
String
● The simplest Redis type● A value can’t be bigger than 512 MB● SET ● GET ● INCR/INCRBY, DECR/DECRBY ● GETSET
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
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
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:
Sets
● Unordered collections of unique strings● SADD ● SMEMBERS
● SISMEMBER
● SINTER○ the intersection between different sets
Sorted Sets
● Collections of unique strings with associated score
● ZADD○ O(log(N))
● ZRANGE/ZREVRANGE● ZRANGEBYSCORE
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
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"
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.
Updating the scores
● Just calling again ZADD
Hashes
● HSET ● HGET ● HGETALL ● HKEYS
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
Demo: realtime-info
AWS ElastiCacherealtime-info
EDM1EDM1
EDM1EDM1
EDM1EDM1
API Server
Log Server
Demo: Realtime-Info (1)
SET realtime-info by Poby
Demo: Realtime-Info (2)
MGET realtime-info by API
참고 자료
● Redis - Wikipedia● Redis - Fast and Furious● 15 minutes introduction to Redis data types● The Little Redis Book● redis.io
끝