What is a Redis

I believe that as long as programmers, whether just into the line or into the line for a long time, the name of Redis should not have not heard of it, then redis in the end what magic charm? Start with a brief introduction to Redis

Redis is a high performance key-value database that is completely open source and free and complies with the BSD protocol. Redis is essentially a key-value in-memory database, much like memcached. The entire database is loaded into memory for operation, and periodically flushed to hard disk asynchronously.

The benefits of Redis favored by major manufacturers are as follows:

  • Fast, because the data is in memory, similar to a HashMap, which has the advantage of having O(1) time for both lookup and operation.
  • Support rich data types, support string, list, set, sorted set, hash
  • Transactions are supported, and operations are atomic, meaning that changes to data are either all or none
  • Rich features: can be used for cache, message, by key set expiration time, will be automatically deleted after expiration

If redis is so important and has so many benefits, do you want a whole wave? Again, learning will become system, I personally feel that a system of study, better your east, west point of learning, finally feel learn a lot, but desultorily, ask what are sciolistic, see this piece of knowledge map of redis, platform limitation, the picture may not be very clear, want Gao Qingtu, DMS architecture diagram available

Ok, let’s look at it separately (because my picture is too big to take a screenshot, I found a copy from the Internet, sorry)

Redis basic data structure

1, String 1.1 data structure Long Len byte Array length Long Free Available array length char buff[setSetnx ()setNot exist) : setting the key value | if there is no key can be, otherwise it returns 0. Setex (setExpire: sets key values (including expiration time),setKey seconeds value setrange: Set the key value of the specified position. Example: setrange name diaodiao 2 haha--> Dihahaao Replace the second position. Mset: set multiple key values. Getset: Obtains the value from the key first, and then sets the value. Getrange (0 to -1): obtains a specified range of characters. Range | count from left to right starting from 0 start from right to left - 1. Hello, for example (0 ~ 4 | - 5 ~ 1) mget: to obtain the value of multiple keys. Operations related to numeric typesset age </span>10<span style="color: #000000;"<span style= "max-width: 100%; clear: both; min-height: 1em"color: #000000;"<span style= "max-width: 100%; clear: both; min-height: 1em"color: #000000;"Decr decrement 1 Decrby Decrement the character property operation of the specified valueset name </span>"hello"<span style="color: #000000;"> append: append the string append name </span>"Diaodiao"-><span style="color: #000000;"</span>->52, hashes(store key value pair, similar to hashmap)2.1 configuration (in the redis.conf configuration file) default:hash-max-zipmap-entries Maximum of 64 configuration fields (number of keys)hash2.2 Command (see String)hset key field valuehset: The key is created if it does not exist. Otherwise, the key is overwritten. Hsetnx: sethashField is the specified value. If key does not exist, it is created first. If field already exists, return 0, nx means not exist. Hmset: set simultaneouslyhashMultiple fieldhget: gets the specifiedhashFieldhmget: Gets all specifiedhashFieldhexists: tests whether the specified field exists. Hlen: Returns the number of specified fields. Hdel: deletes the specified field. Hkeys: query all fieldhvals of the specified key: A list is a data structure based on a bidirectional linked list. The operations are push and pop, including the left (header) in and out of the stack, right (tail) in and out of the stack. Also contains timeout blocking. 3.2 Lpush: Add elements to the head of the list corresponding to the key. Lrange: Gets the values of the list range. Lrange myList start(0) stop(2) lrange myList start(0) stop(2) Linsert myList before "world" "hello" lset: Set list to specify the elements of the following table (starting from 0)lrem: Delete count elements with the same value from the list. Ltrim: Saves the data within the specified key value range. Rpop: delete the element from the end of the list, and return delete the element rpoplpush: Lindex: returns the index element in the list named key. Llen: Returns the length of the list corresponding to the key. Additional features include union, intersection, and difference sets. 4.2 Run the sadd command to query key informationsetSrem: delete the element named key spop: randomly return and deletesetSdiff: two key elementssetSdiffstore: suppose there isset3,set1.set2 -- - >set1 andset2 difference sets return elements to add toset3 Sinter: TwosetThe intersection of. Sinterstore: Suppose there isset3,set1.set2 -- - >set1 andset2 Intersection of the returned elements, added toset3 sunion: twosetSunionstore: assuming there isset3,set1.set2 -- - >set1 andset2 Intersection of the returned elements, added tosetSmove in 3: suppose there isset1.set2 - > deletesetA key value of 1 and added toset2 scard: returnsetSismember: testsetIs there a member in. 5. Sorted: returns a random element but does not delete itset5.1 introduction sortedset(the skip list | two-way chain table and the combination of hashtable)setThe updated version of setSets has two latitudes, one for order and one for value. 5.2 Zadd: Add elements member and score to the zset named key for sorting. Zrem: deletes the zset element named key memberzincrby: If the zset element named key already exists, increment is added to the score of the element. Otherwise add the element to the collection, Zrevrank: returns the rank of member elements in the zset named key (incrementzrank: returns the rank of member elements in the zset named key (press Zrevrange: returns all elements from start to end in zset with key (score sorted by score); zrevrange: returns all elements from start to end in zset with key (score sorted by score); Zrangebyscore returns all elements from index in zset with name key from start to end zrangebyScore: Returns elements in the set whose score is in a given interval. Zcount: Returns the number of elements in the set whose score is in a given interval. Zcard: Returns the number of elements in the set Scorezremrangebyrank: deletes elements in a set whose score is in a given rangeCopy the code


Redis common command

1, the key value related command keys * * | | key key??????? Exists key: checks whether a key exists. Del Key: deletes a key. Expire key seconeds: sets the expiration time of a key (unit: second). Move: transfers the key in the current database to another database. Persist: remove the expiration time of a given key TTL: check how long it will take to expire RandomKey: return a key from the namespace at random renamekey: rename the keytype: Return value type </span>2<span style="color: #000000;"Ping: tests whether the connection is aliveecho: Prints some content on the command line. Select: selects a database. Redis database id ranges from 0 to 15<span style="color: #000000;">, we can select any database for data access. Quit: Exits the connection. Dbsize: Returns the number of keys in the current database. Info: Obtains server information and statistics. Monitor: dumps received requests in real time. Config: Obtains server configuration information. Flushdb: deletes all keys in the current selected database. Flushall: deletes all keys in all databases. </span></span></pre>Copy the code

Redis advanced use properties

1. Security: Set each time before to confirm password | in redis. Conf configuration file modified requirepass2, master-slave replication 2.1 features (1) the master can have multiple slave (2) multiple slave can connect the same master, (3) Master/slave replication does not block the master, data is synchronized, and the master can continue processing the client. (4) improve the scalability of the system, the building process reference 2.2: http://www.cnblogs.com/qiuyong/p/6705689.html3 3.1 simple transaction, transaction control control multi - > transaction to beginexec--> Exit commit 3.2 Transaction rollback Muliti --> transaction begin discard--> transaction rollback 4. Persistency 4.1 Snapshotting (default) -- Snapshotting Writes data to binary files in snapshotting mode, also dump.rpb. Dump. RPB save mode: Manually save, block the current thread, and save memory data to dump. RPB. Bgsave: Starts the child thread, invokes the fork operation, and stores the memory data to dump. RPB in the background. Automatic BGSave is set by default in redis.conf. Exception: if client1 and client2 are used in the flushall, all data in the memory will be flushed. Client2 execution can cause a lot of trouble because the data was previously purged without being known. Usually, before saving, dump. RPB is transferred to another directory to facilitate data recovery. 4.2 AOF (appending-only file)--> If the application requires that no changes be lost, you can use aOF persistence: by default every second redis will receive write commands to append content to appendonnesture. aof file. Conf appendonly yes // Enable aOF persistence# appendfsync always write to disk immediately after receiving the write command, slowest, but with complete persistence Good compromise between performance and persistence # appendfsync no // completely dependent on OS, 5. Publish and subscribe messages 5.1 Subscribe channels such as Tv1 / Tv2 / Tv3 pSUBSCRIBE TV * For example, all messages starting with TV can be received 5.2 Sender publish Tv1 message 5.3 Exit subscription mode: unsubscribe, unpsubscribe6, Pipeline sends requests in batches 1, common connection mode based on TCP, each time to wait for a reply to execute 2, Pipeline mode after the execution of multiple commands, and then return the execution structure to the client. Vm-enabled yes # Enable the vm function vm-swap-file/TMP /redis. Swap # Vm-max-memory 1000000 #redis Vm-page-size 32 # Size of each page 32 bytes VM-pages 134217728 # Number of pages vM-max-Threads 4 # Number of worker threads used to perform value object swapsCopy the code

Redis persistent disk IO and its problems

Those who have experience in online operation and maintenance of Redis will find that Redis will be unstable or even crash when the physical memory usage is high, but the actual total physical memory capacity is not exceeded. Some people think that the memory usage is doubled by the fork system call based on snapshot-based persistence. This view is not accurate. Since the copy-on-write mechanism for fork calls is based on the operating system page unit, only dirty pages with writes are copied, but your system will not have all pages written in a short period of time, so what causes Redis to crash?

Redis writes to and reads from the Page Cache of physical memory for persistent files. Most database systems use Direct IO to bypass the Page Cache and maintain a data Cache. However, when Redis’s persistent files are too large (especially snapshot files) and read and write to them, The data in the disk file is loaded into the physical memory as a layer of Cache for the operating system. The data in this Cache is stored repeatedly with the data managed in Redis memory, although the kernel does the work of cull Page Cache when the physical memory is short. But the kernel will probably decide that a Page Cache is more important and let your process Swap, and your system will start to wobble or crash. Our rule of thumb is that when your Redis physical memory usage exceeds 3/5 of the total memory capacity, it starts to get dangerous.

  

Conclusion:

1. Select an appropriate data type and set compact storage parameters for different application scenarios.

2. When business scenarios do not require data persistence, disable all persistence modes for optimal performance and maximum memory usage.

3. If you need to use persistence, select snapshot or statement appending based on whether you can tolerate the loss of some data after restarting. Do not use virtual memory or DiskStore.

4. Don’t make your Redis machine use more than 3/5 of its physical memory.

Well, that’s all for today, and I’ll be updating redis and other series of technical articles in the future. Welcome to the public account “Java Architect Consortium” and don’t get lost

I think it’s pretty good, so I need a thumbs-up to support it

See the more cool, forward to more people, so that we share joy together