ãRedisãããããRedisã(by typester)â¦â¦ã¨ããã»ã©èªå㯠Redis æã§ããªãã®ã§ãããæè¿ Redis ã使ã£ããµã¼ãã¹ã®é¢åãè¦ã¦ãã¦æã ã¬ã¹ãã³ã¹ãæªåããç¾è±¡ã«åºä¼ã£ãã®ã§èª¿ã¹ã¾ããã
åæ
使ç¨ãã¦ããã®ã¯ Redis 2.4.16 ã§ãã
redis.conf ã« "save [t] [n]" ãå®ç¾©ããã¨ãæå¾ã« save ããã¦ãã [t]ç§éã« [n]å以ä¸ã® key ãæ´æ°ãããå ´åã« background 㧠save (=bgsave) ãå®è¡ããã¾ãã
save 60 10000
ããã ã¨ã60ç§éã« 10,000 keys ã§ãã
bgsave ã§ã¯ redisã¯èªåèªèº«ã®ããã»ã¹ã fork() ããåããã»ã¹å´ã¯èªåã®ã¡ã¢ãªã«ä¹ã£ã¦ããå 容ããã¡ã¤ã«ã«ãã¹ã¦æ¸ãåºãã¾ãã
ãã®ä»çµã¿ã«ãã£ã¦ã1ããã»ã¹ 1ã¹ã¬ããã§åä½ãã¦ãã redis ã®æä½ã block ãããã¨ãªãã«å ¨ãã¼ã¿ã®ãã¡ã¤ã«ã¸ã®æ¸ãè¾¼ã¿ãå®ç¾ãã¦ãã¾ããè³¢ãã§ããã
çºçããåé¡
redisã«ã¢ã¯ã»ã¹ãã¦ãã webã¢ããªã±ã¼ã·ã§ã³ã®ã¬ã¹ãã³ã¹ã¿ã¤ã ãæªåããæ°ç§ block ãããããªæåãè¦ããã¾ããããã®æåã¯ãbgsave å®è¡ä¸ã«çºçãã¦ãã¾ãã
åå ã®ç¹å®
仮㫠redis ãæã£ã¦ãããã¼ã¿ã 4GB ã ã£ãã¨ããã¨ãbgsave ããã¨ãã«ã¯ 4GB ã¾ããã¨ã®ãã¡ã¤ã«æ¸ãè¾¼ã¿ãçºçãã¾ãã
親㮠redis ããã»ã¹ã¯ãã¡ã¤ã«æ¸ãè¾¼ã¿ãããªãã®ã§ DISK IO ãå¤ãã¦ãåé¡ãªãâ¦â¦ãã¨æããããè¨å®ã«ãã£ã¦ã¯ç´æ¥ãã°ãã¡ã¤ã«ãæ¸ãè¾¼ããã¨ãããã¾ãã
loglevel verbose logfile /var/log/redis.log
[3886] 05 Dec 14:13:42 - Accepted 127.0.0.1:36837 [3886] 05 Dec 14:13:42 - Client closed connection [3886] 05 Dec 14:13:43 - Accepted 127.0.0.1:36838 [3886] 05 Dec 14:13:43 - Client closed connection
ã©ããã bgsave ãèµ°ã£ã¦å¤§éã® DISK IO ãèµ·ãã¦ããã¿ã¤ãã³ã°ã§ãã°ãæ¸ãè¾¼ãã¨ãã㧠block ããããããã¨ãããã¨ãçªãæ¢ãã¾ããã
ç¹å®æ¹æ³
strace 㧠redis ã®ããã»ã¹ãçºè¡ãã syscall ãæ½åºãã以ä¸ã® Perl script ã§ã¿ã¤ã ã¹ã¿ã³ãã 0.2 ç§ä»¥ä¸ééã空ããåå¾ãåºåããã¨ãããwrite() ã®ãã¨ã® close() ã§æ°ç§æãããã¨ãããã®ãçºè¦ãã¡ãªã¿ã«ã½ã¼ã¹ãèªãã¨åããã¾ãããredis.c å ã® redisLog() ã§ããã°åºåãã¨ã«ãã¡ã¤ã«ã追è¨ã¢ã¼ã㧠fopen(), fprintf(), fclose() ãã¦ãã¾ãã
# strace -ttt -p `pgrep redis` 2>&1 | perl timegap.pl Wed Dec 5 10:10:29 2012 1354669829.089764 write(52, "[2255] 05 Dec 10:10:29 - Client "..., 50) = 50 Wed Dec 5 10:10:34 2012 1354669834.418856 close(52) = 0 Wed Dec 5 10:11:55 2012 1354669915.735166 write(55, "[2255] 05 Dec 10:11:55 - Accepte"..., 50) = 50 Wed Dec 5 10:11:57 2012 1354669917.761811 close(55) = 0
#!/usr/bin/env perl use strict; my $prev_ts = 0; my $prev_line = ""; $| = 1; while (my $line = <>) { chomp $line; my ($ts, $log) = split / /, $line, 2; if ($prev_ts && $ts - $prev_ts > 0.2) { printf "%s %s\n", scalar localtime($prev_ts), $prev_line; printf "%s %s\n", scalar localtime($ts), $line; } $prev_ts = $ts; $prev_line = $line; }
対å¿
loglevel verbose ã§ã¯ã¯ã©ã¤ã¢ã³ããæ¥ç¶ãåæãããã¨ã«ãã°ã«è¨é²ããã¾ããããã«ã¹ãã§ãã¯ã®ããã«æ¯ç§æ¥ç¶ã»åæããã¦ããã®ã§ãã°ãã¡ã¤ã«ã¸ã®æ¸ãè¾¼ã¿ãæ¯ç§çºçãã¦ãã¾ããã
loglevel notice ã«å¤æ´ãããã°åºåãæå¶ããã¨ãã bgsave æã«ãããã¯ãããç¾è±¡ããªããªãã¾ããã
æ ¹æ¬çã«ã¯ãredis ããã°ãç´æ¥ãã¡ã¤ã«ã«åºåããã®ã§ã¯ãªããæ¨æºåºåçµç±ã§ multilog ã«æ¸¡ããsyslogã«æãããªã©ã«å¤æ´ããã®ã better ã§ãããããç¥ã®æ¸ãã»ããã§ãã
(å¤æ´åå¾ã®ã¢ããªã±ã¼ã·ã§ã³ã®ã¬ã¹ãã³ã¹ã¿ã¤ã çµ±è¨)