Successfully reported this slideshow.

Redis begins

2,625 views

Published on

Published in: Technology
  • Be the first to comment

Redis begins

  1. 1. Redis Begins charsyam@naver.com
  2. 2. Redis?
  3. 3. Redis? Redis is an open source. http://redis.io
  4. 4. Redis? Redis is an open source. BSD licensed. http://redis.io
  5. 5. Redis? Redis is an open source. BSD licensed. advanced key-value store. http://redis.io
  6. 6. Redis? Redis.io http://redis.io
  7. 7. Why Redis?
  8. 8. Why Redis? Simple. Performance.
  9. 9. Build $ make
  10. 10. But You can meet some errors in Linux Solution: $> make distclean; make;
  11. 11. Why? Redis saves settings to .make-* files And they disturb normal building.
  12. 12. Argenda Single Thread. Collections. Persistent. Replication.
  13. 13. The one thing you should know.
  14. 14. Redis is Single Threaded!
  15. 15. Single Thread Means!
  16. 16. Don’t execute long task.
  17. 17. Example: Keys command Flushall/flushdb command
  18. 18. O(n)
  19. 19. Keys * di = dictGetSafeIterator(c->db->dict); allkeys = (pattern[0] == '*' && pattern[1] == '0'); while((de = dictNext(di)) != NULL) { …… stringmatchlen(pattern,plen,key,sdslen(key),0) }
  20. 20. FlushAll Cache Item Count Time Memcache 1,000,000 1~2ms Redis 1,000,000 1000ms(1 second)
  21. 21. Memcache’s flush is faster than Redis’s flush.
  22. 22. Memcache’s flush is faster than Redis’s flush?
  23. 23. FlushAll-Redis for (i = 0; i < ht->size && ht->used > 0; i++) { dictEntry *he, *nextHe; if ((he = ht->table[i]) == NULL) continue; while(he) { nextHe = he->next; dictFreeKey(d, he); dictFreeVal(d, he); zfree(he); ht->used--; he = nextHe; } }
  24. 24. FlushAll-Memcache if (exptime > 0) settings.oldest_live = realtime(exptime) - 1; else /* exptime == 0 */ settings.oldest_live = current_time - 1;
  25. 25. FlushAll-Memcache if (settings.oldest_live != 0 && settings.oldest_live <= current_time && it->time <= settings.oldest_live) { do_item_unlink(it, hv); do_item_remove(it); it = NULL; }
  26. 26. Argenda Single Thread. Collections. Persistent. Replication.
  27. 27. Collections Memcached supports just K-V List Set Sorted Set Hash
  28. 28. Key:Value $> set key value $> get key
  29. 29. Key:Value sql> insert into userinfo (name, email) values(‘charsyam’, ‘test@abc.com’);
  30. 30. Key:Value $> set id:name “charsyam” $> set id:email test@abc.com $> mget id:name id:email 1) “charsyam” 2) test@abc.com”
  31. 31. List $> rpush listname a --- (a) $> rpush listname b --- (a, b) $> lpush listname c --- (c, a, b) $> rpop listname(or lpop listname)
  32. 32. List - when When you need job queue or stack.
  33. 33. Set $> sadd setname id1 $> sadd setname id2 $> smember setname 1) “id2” 2) “id1”
  34. 34. set - when When you need follwers ids or group members id
  35. 35. Sorted Set $> zadd zsetname 1 “one” $> zadd zsetname 2 “two” $> zadd zsetname 3 “three”
  36. 36. Sorted Set $> zrange zsetname 0 -1 1) “one” 2) “two” 3) “three”
  37. 37. zset - when When you need ranking
  38. 38. Sorted Set $> zrange zsetname 1 3 1) “two” 2) “three”
  39. 39. Hash sql> insert into userinfo (name, email) values(‘charsyam’, ‘test@abc.com’);
  40. 40. Hash$> hmset id name “charsyam” email test@abc.com
  41. 41. Hash$> hgetall 1) “name” 2) “charsyam” 3) “email” 4) “test@abc.com”
  42. 42. Hash$> hset id email abc@abc.com $> hgetall 1) “name” 2) “charsyam” 3) “email” 4) “abc@abc.com”
  43. 43. Remember!!!!!
  44. 44. The one thing you should know.
  45. 45. Redis is Single Threaded!
  46. 46. Don’t insert too many items into collections.
  47. 47. Delete collections Item Count Time list 1,000,000 1000ms(1 second) set Sorted set hash
  48. 48. Argenda Single Thread. Collections. Persistent. Replication.
  49. 49. Redis can store its memory snapshot.
  50. 50. RDB
  51. 51. RDB is not Relation DBMS.
  52. 52. RDB is redis snapshot name.
  53. 53. Fork()
  54. 54. RDB - BAD • Bad Performance in Large memory. • Twice memory problem. • Denying write problem when storing RDB fails. • If you just want Cache. Turn off RDB
  55. 55. RDB – Large Memory • Performance is relevant to Memory Size. 17.1 GB 34.2 GB 68.4 GB 117 GB
  56. 56. RDB – Twice Memory • fork() and COW(copy on write) Issue –In Write Heavy System:
  57. 57. RDB – Twice Memory
  58. 58. RDB – Twice Memory
  59. 59. RDB – Twice Memory
  60. 60. Real Case Study • Background –Can’t write to Redis Server –Sentinel doesn’t find Server’s failure.
  61. 61. Real Case Study • Reason –If redis fails to save RDB, Redis basically denies write operations from client. –“MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.”
  62. 62. Real Case Study • Reason if (server.stop_writes_on_bgsave_err && server.saveparamslen > 0 && server.lastbgsave_status == REDIS_ERR && c->cmd->flags & REDIS_CMD_WRITE) { flagTransaction(c); addReply(c, shared.bgsaveerr); return REDIS_OK; }
  63. 63. Real Case Study • Solution #1 • Solution #2 config set stop-writes-on-bgsave-error no Turn off RDB Setting 2.6.12 부터 conf 에서 stop-writes-on-bgsave-error 설정이 가능해짐.
  64. 64. AOF
  65. 65. AOF is append only file.
  66. 66. AOF memorizes all requests.
  67. 67. AOF*3rn$3rnsetrn$1rnA rn$3rn123rn*3rn$3rn setrn$1rnBrn$3rn123 rn*3rn$3rnsetrn$1r nCrn$3rn123rn
  68. 68. AOF*3 $3 Set $1 A ……
  69. 69. AOF Rewrite When size of AOF grows than redis rewrite memory state to AOF.
  70. 70. Argenda Single Thread. Collections. Persistent. Replication.
  71. 71. Redis support master/slave replication
  72. 72. Replication •Support Chained Replication Master 1st Slave 2nd Slave 1st slave is master of 2nd slave
  73. 73. Replication Master Slave replicationCron Health check
  74. 74. Replication Master Slave replicationCron Health check
  75. 75. Replication Master Slave replicationCron When master reruns, Resync with Master
  76. 76. Mistake: Replication Master Slave replicationCron Slave will has no data after resyncing If master has no data.
  77. 77. Initial Replication Step
  78. 78. Replication Don’t forget “slave of no one”
  79. 79. Sentinel
  80. 80. Sentinel • Sentinel is Failover Solution for Redis. Master Slave Sentinel Sentinel periodically checks Redis Master
  81. 81. Sentinel Master Slave Sentinel Send “slaveof no one” to slave node
  82. 82. Sentinel Master Slave Sentinel Notify to client about changing master Client
  83. 83. Sentinel redis 127.0.0.1:2003> psubscribe * Reading messages... (press Ctrl-C to quit) 1) "pmessage" 2) "*" 3) "+switch-master" 4) "resque 127.0.0.1 1999 127.0.0.1 2002"
  84. 84. Sentinel • Sentinel will connect correct master even if you set slave’s ip in conf Master Slave Sentinel Slave’s ip in sentinel.conf
  85. 85. Sentinel • INFO # Replication role:slave master_host:127.0.0.1 master_port:6379 ……
  86. 86. Sentinel • Reconnect to correct master using master_host and master_port. Master Slave Sentinel Reconnect to master of slave.
  87. 87. Sentinel.conf port 26379 sentinel monitor mymaster 127.0.0.1 6379 1 sentinel down-after-milliseconds mymaster 30000 sentinel can-failover mymaster yes sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 900000
  88. 88. Wrap Up
  89. 89. Redis is Single Threaded!
  90. 90. Q & A
  91. 91. Thank you!

×