REDIS intro and how to use redis

7,141 views

Published on

made by Kris.jeong

Published in: Technology
2 Comments
35 Likes
Statistics
Notes
No Downloads
Views
Total views
7,141
On SlideShare
0
From Embeds
0
Number of Embeds
180
Actions
Shares
0
Downloads
228
Comments
2
Likes
35
Embeds 0
No embeds

No notes for slide

REDIS intro and how to use redis

  1. 1. How to use redis 2012.05.31www.uajjang.com 미디어 검색팀 정경석(kris.j)
  2. 2.  What is When is the right time to use Advantages and disadvantages Install Run/Test Redis Feature Performance test Cluster Recommendation Summary
  3. 3.  REmote DIctionary Server(REDIS) One of open source NoSQL(key–value store) In memory database Supports persistence Redis is written in ANSI C(Portabel) Sponsored by VMware Using github, blizzard, digg, flicker, bump… Last stable version 2.4.14(2012.05.31) Supports a rich set of data types ◦ List, Set, Sorted Set, Hash
  4. 4.  Need a fast response Need a Atomic operations Need a transaction Need a Publish/Subscribe Support for persistence Use case ◦ Job Queue ◦ Session store ◦ Real time ranking(for all users) ◦ Local Address book(Multi client windows, )
  5. 5.  Advantages ◦ Speed(80000 TPS depends on System and Net) ◦ Command level Atomic Operation(tx operation) ◦ Lots of client Lib(Objective-C, C#, Java, node.js, Erlang, Ruby…) ◦ Supports a serverside locking Disadvantages ◦ Snapshot IO overhead/Memory overhead ◦ Source compile install ◦ Whole dataset be loaded into main memory
  6. 6.  Unix/Linux$ wget http://redis.googlecode.com/files/redis-2.4.14.tar.gz$ tar xzf redis-2.4.14.tar.gz$ cd redis-2.4.14$ make Windows 64/32 ◦ Unofficial support ◦ Binary http://github.com/dmajkic/redis/downloads ◦ Source export git from https://github.com/dmajkic/redis.git and build
  7. 7. // run redis server$ cd $REDIS_HOME/src$ ./redis-serverRedis log…// run redis client$ $REDIS_HOME/src$ ./redis-cliredis 127.0.0.1:6379> set firstkey sampleOKredis 127.0.0.1:6379> get firstkey"sample"redis 127.0.0.1:6379> ※ http://try.redis-db.com(ver 2.0.2) test site
  8. 8.  Supports a Expire  Supports a Multi getredis> set a 1 redis> set a 1ok OKredis> expire a 5 redis> set b 12ok OKredis> get a redis> set c 56"1" OK… redis> mget a b c(5 second later) 1) "1"redis> 2) "12"(nil) 3) "56"redis>※ Supports more then 100 commands.
  9. 9.  Supports a LIST  Supports a SETredis>del nickname redis>del nicknameredis> lpush nickname kris redis> sadd nickname kris(integer) 1 (integer) 1redis> lpush nickname peter redis> sadd nickname peter(integer) 2 (integer) 1redis> lpush nickname tom redis> sadd nickname tom(integer) 3 (integer) 1redis> lpush nickname jane redis> smembers nickname(integer) 4 1) "tom"redis> lrange nickname 0 1 2) "peter"1) "jane" 3) "kris"2) "tom" redis> sismember nickname tom (integer) 1※ Supports Range search ※ Non-redundant data processing
  10. 10.  Supports a HASH  Supports a Sorted SETredis>del nickname redis>del nicknameredis> hset user1 name kris redis> zadd nickname 10 kris(integer) 1 redis> zadd nickname 10 peterredis> hset user1 lastname redis> zadd nickname 10 tomjey redis> zadd nickname 10 jane(integer) 1 redis> zrevrange nickname 0 2redis> hset user2 name tom 1) "tom"(integer) 1 2) "peter"redis> hget user1 lastname 3) "kris""jey" redis> zincrby nickname 1 krisredis> hgetall user1 "11"1) "name" redis> zrevrange nickname 0 22) "kris" 1) "kris"3) "lastname" 2) "tom"4) "jey" 3) "peter" ※ Real time ranking
  11. 11.  Supports a WildCard KeySetredis> mset node Node.js node06 Node.js ruby18 Ruby1.8OKredis> mset ruby19 Ruby1.9 java Java6 mode9 tempOKredis> keys node*1) "node06"2) "node"redis> keys ?ode*1) "mode9"2) "node06"3) "node" ※ Most commands time complexity is O(1), except ‘keys’ : O(N) ※ On desktop environment : 1million keys in 40ms
  12. 12.  Supports a Blocking Queue※ Client 1 ※ Client 2redis> lpush job-list batch01 redis> brpop job-list 10 1) "job-list" 2) "batch01" redis> brpop job-list 10 0 (nil) (10.28s) redis> brpop job-list 0redis> lpush job-list batch02 1) "job-list" 2) "batch02" (5.82s)
  13. 13.  Supports a Publish/Subscribe(PSubscribe)※ Client 1 ※ Client 2redis> publish public-notice redis> psubscribe public*test message Reading messages... (press Ctrl-C(integer) 1 to quit) 1) "psubscribe" 2) "public*" 3) (integer) 1redis> publish public-alert 1) "pmessage"server will shutdon‘ 2) "public*"(integer) 1 3) "public-notice" 4) "test message" 1) "pmessage" 2) "public*" 3) "public-alert" 4) "server will shutdon"
  14. 14.  Test ENV ◦ Server : 10.x.x.xx/10.x.x.xx(dual node v2.4.14) ◦ Client : Local PC(Thread 100/100M Lan) ◦ Command : get/set Master/Slave Snapshot on Redis Server(Master) set Jedis Client Real time replication get 16,000~ Redis Server(Slave) 20,000 TPS Redis의 TPS는 대부분 Network/CPU 성능에 좌우되며 설정에 따라서 더 빨라질 수 있음. 공식 사이트의 측정치는 80000 TPS.
  15. 15. Master Snapshot offWrite Slave Slave Snapshot on Snapshot on Read Read
  16. 16.  모든 데이터가 Memory에 적재되므로 적절한 용량산정 필요.(천만 Key일때 약 500MB메모리 소요) Redis서버의 구현이 Single Thread로 구성되어 있으므로 물리 Core개수 만큼의 Instance 설치. 현재 release version 2.4.14에서는 Serverside Cluster(Sharding) 가 구현되어 있지 않음.(Sharding을 사용하기 위해서는 별도의 구현 필요) Persistence지원을 위하여 Snapshot 또는 AOP설정을 사용하여야 함.(Disk IO overhead 불가피) ◦ 실제 Field에서는 Master(Snapshot off) slave(on), slave(on)의 구성을 사용하고 있으며 Write는 Master, Read는 Slave에서 처리(성능 향상을 위함.) Redis Server 재 시작시 최종 Snapshot/AOF의 데이터가 로드됨. A common convention is to use “obj-type:id:field”
  17. 17.  Amazon EC2 Server에서 서비스중인 사례. ◦ 40000~100000 TPS처리중임. ◦ 20GB의 데이터 셋 ◦ Amazon EC2 Server의 Virtualize 특성 때문에 Snapshot 사용 문제  원인 fork() 시간이 너무 길게 걸림.) ◦ 위의 문제로 인하여 Snapshot을 off하고 AOF(Append Only File)기능을 사용함. ◦ 위와 같은 설정에서 서비스 이상없이 운영중임. 사용사례 Posting(1억2천만 사용자 대상 실시간 로그인 통계) ◦ http://blog.getspool.com/2011/11/29/fast-easy-realtime- metrics-using-redis-bitmaps/ ◦ Unique방문자수 집계  일간 : 50ms, 주간 : 392ms, 월간 : 1624ms

×