{"id":6766,"date":"2017-06-05T13:46:06","date_gmt":"2017-06-05T05:46:06","guid":{"rendered":"http:\/\/rmohan.com\/?p=6766"},"modified":"2017-06-05T13:46:06","modified_gmt":"2017-06-05T05:46:06","slug":"redis-is-a-shallow-introductory-tutorial","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6766","title":{"rendered":"Redis is a shallow introductory tutorial"},"content":{"rendered":"<p>Redis is a shallow introductory tutorial<\/p>\n<p>1.  Introduction to Redis<\/p>\n<p>Redis is an open source using ANSI C language-based, memory-based Key-Value database.<\/p>\n<p>It supports storing more value types, including string, list, set, zset (sorted collection), and hash (hash type).<\/p>\n<p>Redis supports master-slave synchronization, and data can be synchronized from the master server to any number of slave servers. Since the publish \/ subscribe mechanism is fully implemented so that the tree can be synchronized from anywhere in the database, a channel can be subscribed and a complete message is received by the master server Release record.<\/p>\n<p>Compared to memcached, Rdeis has the following advantages:<\/p>\n<p>1. redis native support for more data types.<br \/>\n2. redis has a persistence mechanism, you can regularly store the data in memory to the hard disk.<br \/>\n3. redis support master-slave mode of data backup.<\/p>\n<p>4. performance. Redis author&#8217;s argument is to average the performance of a single core, in a single data is not the case of Redis better.<\/p>\n<p>Why do you say that the reason is that Redis is a single-threaded operation. Because it is single-threaded operation, so and Memcached multi-threaded compared to the overall performance will be low. Because it is a single-threaded operation, IO is serialized, network IO and memory IO, so when a single piece of data is too large, due to the need to wait for a command all IO to complete the subsequent order, so the performance will be affected.<\/p>\n<p>2.  Install Redis<\/p>\n<p>2.  Install Redis<\/p>\n<p>2.1 Redis installation is very simple, with yum or apt-get can be installed directly<\/p>\n<p># yum install epel-release<br \/>\n# yum install redis<\/p>\n<p>2.2 start \/ stop Redis<\/p>\n<p># redis-server \/etc\/redis.conf<\/p>\n<p># systemctl start redis<br \/>\n# systemctl stop redis<\/p>\n<p>3.  use Redis<\/p>\n<p>3.1 Redis-cli Command Line Operation KV<\/p>\n<p>Connect to Redis<\/p>\n<p># redis-cli -p port<br \/>\n# redis-cli<br \/>\nPing<\/p>\n<p>127.0.0.1:6379> ping<br \/>PONG<br \/>\nSet key<\/p>\n<p>127.0.0.1:6379> set testkey &#8220;hello&#8221;<br \/>\nOK<br \/>\nQuery key<\/p>\n<p>127.0.0.1:6379> get testkey<br \/>\n&#8220;hello&#8221;<br \/>\nDelete key<\/p>\n<p>127.0.0.1:6379> del testkey<br \/>\n(integer) 1<br \/>\nSet the validity period<\/p>\n<p>127.0.0.1:6379> setex test 10 111<br \/>\nOK<br \/>\nUse EXPIRE key s to set the expiration time milliseconds with PEXPIRE<\/p>\n<p>127.0.0.1:6379> EXPIRE test11 300<br \/>\n(integer) 1<br \/>\nUse the TTL key to view the expiration time in milliseconds with PTTL<\/p>\n<p>127.0.0.1:6379> TTL test11<br \/>\n(integer) 288<br \/>\nCancel expiration time with PERSIST key<\/p>\n<p>127.0.0.1:6379> PERSIST test11<br \/>\n(integer) 1<\/p>\n<p>3.2 Advanced features<\/p>\n<p>3.2.1 self-increment, self-decreasing, INCR, DECR, INCRBY, SORT<\/p>\n<p>127.0.0.1:6379> set counter 100<br \/>\nOK<br \/>\n127.0.0.1:6379> incr counter<br \/>\n(integer) 101<br \/>\n127.0.0.1:6379> incr counter<br \/>\n(integer) 102<br \/>\n127.0.0.1:6379> decr counter<br \/>\n(integer) 101<\/p>\n<p>3.2.2 Transactions<\/p>\n<p>127.0.0.1:6379> MULTI<br \/>\nOK<br \/>\n127.0.0.1:6379> set test11 111111<br \/>\nQUEUED<br \/>\n127.0.0.1:6379> set test12 121212<br \/>\nQUEUED<br \/>\n127.0.0.1:6379> incr counter<br \/>\nQUEUED<br \/>\n127.0.0.1:6379> EXEC<br \/>\n1) OK<br \/>\n2) OK<br \/>\n3) (integer) 102<\/p>\n<p>3.2.3 HyperLogLogs<\/p>\n<p>Redis adds the HyperLogLog algorithm to version 2.8.9.<\/p>\n<p>3.2.4 publish \/ subscribe function<\/p>\n<p>Redis publish a subscription (pub \/ sub) is a message communication mode: the sender sends a message and the subscriber receives the message. Redis clients can subscribe to any number of channels.<\/p>\n<p>Subscribe to the channel redisChat on a client<\/p>\n<p>127.0.0.1:6379> SUBSCRIBE redisChat<br \/>\nReading messages&#8230; (press Ctrl-C to quit)<br \/>\n1) &#8220;subscribe&#8221;<br \/>\n2) &#8220;redisChat&#8221;<br \/>\n3) (integer) 1<br \/>\nOn another client, send a message to the channel redisChat, the subscriber will be able to receive the message<\/p>\n<p>Release:<\/p>\n<p>127.0.0.1:6379> PUBLISH redisChat &#8220;redis haha&#8221;<br \/>\n(integer) 1<br \/>\nSubscriptions:<\/p>\n<p>127.0.0.1:6379> SUBSCRIBE redisChat<br \/>\nReading messages&#8230; (press Ctrl-C to quit)<br \/>\n1) &#8220;subscribe&#8221;<br \/>\n2) &#8220;redisChat&#8221;<br \/>\n3) (integer) 1<br \/>\n1) &#8220;message&#8221;<br \/>\n2) &#8220;redisChat&#8221;<br \/>\n3) &#8220;redis haha&#8221;<br \/>\n3. 3 View the Redis status<\/p>\n<p>Info output a lot of information, you can specify the output part<\/p>\n<p>1<br \/>\n127.0.0.1:6379> info stats<\/p>\n<p>1<br \/>\n127.0.0.1:6379> info memory<\/p>\n<p>Used_memory: The total amount of memory allocated by the Redis allocator, in bytes.<\/p>\n<p>Used_memory_rss: Returns the amount of memory that Redis has allocated from the operating system (commonly known as resident set size). This value and top, ps and other command output consistent.<\/p>\n<p>Rss> used, and the difference between the two values ??is large, that there is (internal or external) memory fragmentation.<\/p>\n<p>The ratio of memory fragmentation can be seen by the value of mem_fragmentation_ratio.<\/p>\n<p>Used> rss, that part of the memory of Redis is replaced by the operating system swap space, and in this case, the operation may produce significant delay.<\/p>\n<p>Used_memory_peak: peak, set the maximum memory to be greater than the peak<\/p>\n<p>3. 4 other orders<\/p>\n<p>View the number of records<\/p>\n<p>127.0.0.1:6379> dbsize<\/p>\n<p>View all KEYs<\/p>\n<p>127.0.0.1:6379> KEYS *<\/p>\n<p>List all client connections<\/p>\n<p>127.0.0.1:6379> CLIENT LIST<\/p>\n<p>Close the ip: port client<\/p>\n<p>127.0.0.1:6379> CLIENT KILL 127.0.0.1:11902<br \/>\nClear all keys for all databases<\/p>\n<p>127.0.0.1:6379> FLUSHALL<br \/>\nEmpty all keys in the current database<\/p>\n<p>127.0.0.1:6379> FLUSHDB<br \/>\nReturns the last time the data was successfully saved to disk, expressed in UNIX timestamp format<\/p>\n<p>127.0.0.1:6379> LASTSAVE<br \/>\nReturns the current server time, expressed in UNIX timestamp format<\/p>\n<p>127.0.0.1:6379> TIME<br \/>\nConnect to other databases (default database is 0 )<\/p>\n<p>127.0.0.1:6379> SELECT 1<br \/>\nOK<br \/>\nMoves the key of the current database to the specified database<\/p>\n<p>127.0.0.1:6379> MOVE test2 1<br \/>\n(integer) 1<\/p>\n<p>4.  Set the file<\/p>\n<p>4.1 \/etc\/redis.conf<\/p>\n<p>daemonize no<br \/>\ntimeout 0<br \/>\nmaxclients 10000  <\/p>\n<p>maxmemory <bytes><br \/>\nmaxmemory-policy volatile-lru<br \/>\nmaxmemory-samples 3<br \/>\nhash-max-ziplist-entries 512  Map<br \/>\nhash-max-ziplist-value 64      Map<br \/>\nlist-max-ziplist-entries 512?list-max-ziplist-value 64<br \/>\n slowlog-log-slower-than 10000  slow log ?microseconds?1000000?<br \/>\nslowlog-max-len 128            slow log<\/p>\n<p>4.2 View the maximum number of connections<\/p>\n<p>127.0.0.1:6379> config get maxclients<br \/>\n1) &#8220;maxclients&#8221;<br \/>\n2) &#8220;10000&#8221;<br \/>\nAdjust the parameters during operation<\/p>\n<p>127.0.0.1:6379> config set maxclients 10001<br \/>\n4.3 View slow log<\/p>\n<p>127.0.0.1:6379> SLOWLOG get<br \/>\n127.0.0.1:6379> SLOWLOG get 10<br \/>\n1) 1) (integer) 0<br \/>\n   2) (integer) 1448413479<br \/>\n   3) (integer) 124211<br \/>\n   4) 1) &#8220;FLUSHALL&#8221;<br \/>\nConfirm the number of slow log settings<\/p>\n<p>127.0.0.1:6379> SLOWLOG len<br \/>\nClear the slow log<\/p>\n<p>127.0.0.1:6379> SLOWLOG reset<br \/>\n5.  Data persistence<\/p>\n<p>5.1 Snapshot ( Snapshot )<\/p>\n<p>5.1.1 Setting up a snapshot in the settings file<\/p>\n<p>save <seconds> <changes><br \/>\nrdbcompression yes\/no<br \/>\ndbfilename dump.rdb         ?Append Only File?<br \/>\ndir \/var\/lib\/redis\/<br \/>\n5.1.2 Manually create snapshots<\/p>\n<p>Execute the save or bgsave command at the command line<\/p>\n<p>127.0.0.1:6379> SAVE<br \/>\nOK<br \/>\n5.2 Log Backup ( Append Only File )<\/p>\n<p>Similar to the mysql binlog, the operation will be recorded in the log Lane. Snapshot does not require the need to save the accuracy, and snapshots in combination, is not recommended to use alone. The default interval is 1 second and can be modified.<\/p>\n<p>5.2.1 Setting AOF in the setup file<\/p>\n<p>appendonly yes                    Append Only File<br \/>\nappendfilename &#8220;appendonly.aof&#8221;<br \/>\nappendfsync always\/everysec\/no<br \/>\nno-appendfsync-on-rewrite no<br \/>\nbgsave<br \/>\nAOF ppendfsync<br \/>\nnone<\/p>\n<p>5.3 Restore<\/p>\n<p>To restore Redis data simply move Redis&#8217;s backup file (dump.rdb, appendonly.aof) to the Redis directory and then start the server.<\/p>\n<p>To get your Redis directory, use the command as follows:<\/p>\n<p>127.0.0.1:6379> config get dir<br \/>\n1) &#8220;dir&#8221;<br \/>\n2) &#8220;\/var\/lib\/redis&#8221;<br \/>\n6. Postscript<\/p>\n<p>This article briefly describes the installation and use of Redis, followed by master and slave synchronization, load dispersion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis is a shallow introductory tutorial<\/p>\n<p>1. Introduction to Redis<\/p>\n<p>Redis is an open source using ANSI C language-based, memory-based Key-Value database.<\/p>\n<p>It supports storing more value types, including string, list, set, zset (sorted collection), and hash (hash type).<\/p>\n<p>Redis supports master-slave synchronization, and data can be synchronized from the master server to any number [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6766"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6766"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6766\/revisions"}],"predecessor-version":[{"id":6767,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6766\/revisions\/6767"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}