Your SlideShare is downloading. ×
Redis 맛보기
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Redis 맛보기

316
views

Published on

Redis 101

Redis 101


0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
316
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Redis 맛보기 jhoh@encoredtech.com
  • 2. Redis ● REmote DIctionary Server ● In-memory, but can be persisted ● NoSQL Key-value store ● Written in ANSI C ● No threading, just one process
  • 3. The Most Popular Key-Value Store
  • 4. 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
  • 5. Running & Connecting ● redis-server ● redis-cli ● http://try.redis.io/
  • 6. Redis Protocol redis[“key”] = “value”
  • 7. 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
  • 8. 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”
  • 9. Value types ● String ● List of strings ● Sets of strings ● Sorted sets of strings ● Hashes where keys and values are strings
  • 10. String ● The simplest Redis type ● A value can’t be bigger than 512 MB ● SET ● GET ● INCR/INCRBY, DECR/DECRBY ● GETSET
  • 11. 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
  • 12. 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
  • 13. 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:
  • 14. Sets ● Unordered collections of unique strings ● SADD ● SMEMBERS ● SISMEMBER ● SINTER ○ the intersection between different sets
  • 15. Sorted Sets ● Collections of unique strings with associated score ● ZADD ○ O(log(N)) ● ZRANGE/ZREVRANGE ● ZRANGEBYSCORE
  • 16. ZADD Add 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
  • 17. ZRANGE/ZREVRANGE Ask 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"
  • 18. 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.
  • 19. Updating the scores ● Just calling again ZADD
  • 20. Hashes ● HSET ● HGET ● HGETALL ● HKEYS
  • 21. 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
  • 22. Redis Clients http://redis.io/clients
  • 23. Demo: realtime-info AWS ElastiCache realtime-info EDM1 EDM1 EDM1 EDM1 EDM1 EDM1 API Server Log Server
  • 24. Demo: Realtime-Info (1) SET realtime-info by Poby
  • 25. Demo: Realtime-Info (2) MGET realtime-info by API
  • 26. 참고 자료 ● Redis - Wikipedia ● Redis - Fast and Furious ● 15 minutes introduction to Redis data types ● The Little Redis Book ● redis.io
  • 27.

×