Redis Summary Notes

Source: Internet
Author: User
Tags redis cluster value store

1. Introduction to Redis and installation deployment
NOSQL = "Not only SQL"
NoSQL is stored in key-value form
Features: Non-relational, distributed, open-source, horizontally scalable
NOSQL: High data concurrency and read-write
Efficient storage and access to massive amounts of data
Scalability and high availability of data
Redis is an open-source, Advanced Key-value store. He is called a data structure server because keys contain strings, hashes, linked lists, collections, and ordered collections
All data is stored in memory and can be periodically written to disk or modified to append to the log file

Generally two ways:
Application direct access to read-write Redis master-slave Service "There is a certain data hidden danger"
Application access to Redis, access to Redis fails, then read and write database MySQL
"When applications read and write to Redis, Redis synchronizes to MySQL to ensure that the Redis cluster and MySQL cluster data are synchronized to ensure data security"


Redis Application Scenario:
For massive data
1, take the smallest n data operation
2, the leaderboard application, take top N operation
3, need to set the exact expiration time of the application
4. Counter Application
5, Uniq operation, to obtain a period of time all data row weight value
6, real-time system, anti-spam system
7, pub/sub build real-time message system
8. Build the queue system
9. Cache

Reids and MySQL are the same points and different points, all have the concept of database, Reids no table No field no sequence, MySQL has tables and fields

Reids Installation:
Unzip the file
--TAR-XZVF redis-2.8.17.tar.gz
--CD redis-2.8.17
Compiling and installing
--Make
--CD src && make install
Move files for easy management
--Mkdir-p/usr/local/redis/bin
--Mkdir-p/usr/local/redis/etc
--MV ~/redis-2.8.17/redis.conf/usr/local/redis/etc
--CD ~/REDIS-2.8.17/SRC
--MV mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server/usr/local/redis/bin
Start Redis Service
--/usr/local/redis/bin/redis-server/usr/local/redis/etc/redis.conf
--Redis default port 6379
Client connections
--/USR/LOCAL/REDIS/BIN/REDIS-CLI
Stop Redis Service
--/USR/LOCAL/REDIS/BIN/REDIS-CLI shutdown
Or
--Pkill Redis-server

--Start Redis in the background
--redis.conf daemonize modified to Yes
--View Port NETSTATE-TUNPL | grep 6379

2. Redis Data type
String type, a key corresponding to a value,string type is binary safe, Redis string can contain any data, than slice binary
The Set method sets the value of the key corresponding to a string of type
eg: adding NAME=CXQ key-value pairs
>>> Set name Cxq
Get method gets the value corresponding to key
>>> Get Name
The Setnx method setting can correspond to a value of string type value if key already exists to return 0,NX identity no exist meaning. Set a new value if it does not exist
>>> Set name Chenxiangqi
>>> Get Name
The Setex method sets the value of the key corresponding to a string type of value and specifies the validity period for this key value
>>> Setex haircolor Red//Set Haircolor key corresponds to red validity 10s
SetRange method sets the substring of the value of the specified key
>>> Get Name
"[Email protected]"
>>> SetRange Name 4 gmail.com//From [0 start] 4th character replace
(integer) 13
>>> Get Name
The Mset method sets the value of multiple keys at once, and a successful return of OK means that all values are set to fail return 0 means that all values are not set
>>> mset key1 cxq key2 Chenxiangqi
The Msetnx method sets the value of more than one key at a time, and a successful return of OK means that all values are set, and failure returns 0 to indicate that no value is set, but does not overwrite the existing key
>>> Msetex key1 cxq key2 Chenxiangqi
The Getset method sets the value of key and returns the old value of key
>>> Getset name 20
"30"
GetRange method sets the substring of key
>>> GetRange name 0 5//returns from the No. 0 character of the value of name to the 5th character
Mget Method Bulk Acquisition
>>> mget key1 Key2 Key3
The Incr method increments the value of the key and returns the incremented value
>>> incr Name
"31"
The Incrby method is similar to INCR, plus the specified value, if the corresponding can not be present, add a new value, the original value is 0
>>> Incrby Key6 5//Let Key6 value increase by 5
>>> Incrby key6-5//To reduce the value of Key6 by 5
Decr method to self-decrement the value of a key
The Descrby method reduces the value of a key by a specified value
>>> DECR Key6
>>> Desrby Key6 5
>>> Desrby key6-5//Let Key6 add 5
The Append method appends value to the string of the specified key, returning the length of the new string
>>> Append name. Net
The Strlen method takes the string length of the value of the specified key

The hash data type is a string type of field and value mapping table, it can add delete operations are 01,hash particularly suitable for storing objects, storing an object in hash type will consume less memory, and more convenient access to the entire object
The Hset method sets the hash field to the specified value, and if key does not exist, first create
>>> hset myhash field1 Hello
>>> hset user:001 name Chenxiangqi
>>> hget user:0001 Name
The Hsetnx method sets the hash field to the specified value, and if key does not exist, first create
>>> hsetnx myhash Field "HELLO"
(integer) 1
>>> hsetnx myhash Field "HELLO"
(integer) 0
Hmset method to set multiple fields with multiple hashes at the same time
>>> hmset myhash Field "HELLO" Field2 World
Hget method
>>> hget user:003 Name
Hmget method Gets the multiple key values specified by the hash
>>> hmget user:003 name Sex age
The Hincrby method adds the specified value to the specified hash field.
>>> Hincrby user:003 Age 5
The Hexists method tests whether the specified field exists
>>> hexists user:003 Name
>>> hexists user:003 hhhhh
The Hlen method returns the hash field with all the key numbers
>>> Hlen user:001
>>> Hlen user:003
Hdel method deletes the field of the specified hash
>>> Hdel Myhash Age
The Hkeys method returns all the field in the hash
>>> Hkeys user:003
The Hvals method returns the index value of the hash.
>>> hvals user:003
The Hgetall method gets all the field and value in a hash
>>> Hgetall user:003

List data Type list is a linked list structure, the main function push, pop gets a range of values, the list type is actually each element is a string type of doubly linked list
The Lpush method presses an element from the head into a linked list
>>> Lpush MyList World
>>> Lpush mylist Hello
The Lrang method gets the element of the specified range for the specified list
>>> lrange mylist 0-1//Pull out from the list from the first element to the last element
The Rpush method presses a list from the tail into an element
>>> Rpush Mylist2 Hello
>>> Rpush Mylist2 World
>>> Lrange Mylist2 0-1
The Linsert method adds a string before or after a specific position of the list for which the key corresponds
>>> Rpush list:003 World
>>> Linsert list:003 before World Hello//Insert Hello in front of world
>>> Lrange 0-1
The LSet method sets the element in the list that specifies the subscript to replace
>>> Lpush List5 One
>>> Lpush LIST5
>>> Lpush List5 Three
>>> LSet LIST5 0 four//Set the element labeled 0 to change to four
>>> Lrange list5 0-1
The Lrem method removes the values of N and value from the list corresponding to the key [n<0 never deletes n=0 delete all], the value returned is the number of deletions
>>> Lpush List6 One
>>> Lpush List6 One
>>> Lpush List6 One
>>> lrem List6 1 "one"//delete the same value from the LIST6
LTrim method preserves data within the value range of the specified key
>>> Lpush list8 "one"
>>> Lpush List8 "the other"
>>> Lpush List8 "three"
>>> LTrim list8 1-1//Keep subscript from 1 to 1 "Last" element, delete other elements
The Lpop method removes an element from the head of the list and returns the deleted element
>>> Lpop List8
The Rpop method removes an element from the tail of the list and returns the element that was deleted
>>> Rpop List8
The Rpoplpush method removes the element from the tail of the first list and adds it to the head of the second list
>>> Rpoplpush list7 list8//Remove an element from the tail of the list7 and press the element from the head into the LIST8
The Lindex method returns the element of the index position in the list of the name key
>>> lindex LIST5 1//Returns an element in List5 with the table 1[index] in the element index
The Llen method returns the length of the list corresponding to key
>>> Llen List7


Set data type collection, which is actually an unordered collection of type string, and set is implemented through Hashtable. Add Delete Find complexity is 0 (1)
Sadd method adds an element to a set named key
>>> Sadd Set1 Hello
>>> Sadd Set1 World
>>> Sadd Set1 World
Srem method to delete an element in a set named key
>>> Srem Set1 World
The Spop method randomly returns and deletes an element in a set named key
>>> spop Set2 "one"//cannot specify the error
>>> spop Set2 "Both"//cannot specify the error
>>> Spop Set2//success
The Sdiff method returns the difference set of two sets of the difference set between a given key and the first key, who is the standard before
>>> Sdiff Set1 Set2//Set1 to the standard return Set1 in contrast to different elements in Set2
The Sdiffstore method returns the difference between the given key and the first possible set, and stores the difference in the specified collection
>>> smembers Set1
>>> Smembers Set2
>>> sdifstore Set1 Set2 set3//Set1 and Set2 difference set stored in Set3
The Sinter method returns the intersection of the point key
>>> sinter Set1 Set2//return Set1 and Set2 intersection
The Sinterstore method returns the specified intersection and stores the intersection in the specified collection
>>> Sinterstore Set1 Set2 set4
The Sunion method returns the set of all key sets
>>> sunion Set1 Set2
Sunionstore method
>>> Sunionstore Set1 Set2 set5
The Smove method removes an element from the set of the given first key and adds it to the set specified by the second key.
>>> smove Set1 Set2 three//Set1 in three removed and added to Set2
The SCard method returns the number of elements of a set with the name key
>>> SCard Set2
The Sismember method tests whether an element is an element of the set corresponding to the specified key
>>> Sismember Set2//Determine if the element is Set2
The Srandmember method randomly returns an element of set with the name key but does not delete
>>> Srandmember Set2
The Smembers method shows all elements of the specified collection
>>>smembers Set1

The storted sets type is an upgraded version of set, and he adds a sequential attribute on the set basis, which can be specified when adding a modified delete element, and Zset will automatically sort you after each assignment.
The Zadd method adds an ordered set of elements, score for sorting
>>> zadd Zset1 1 "one"//item Zset1 Add an element that specifies the order of 1
>>> Zadd Zset1 2 "
>>> Zadd Zset1 3 "
Zrange method Gets the element "specified range, withscores output sequence number" in an ordered set
>>> zrange zset1 0-1 withscores//output ZSET1 index 0 to-1 elements, and output sequence number
The Zrem method removes the Zset element with the name Key member
>>> zrem zset1//delete the Zset1 in the
Zincry method if the element member is already present in Zset with the name key, the score of the element is incremented increment otherwise the item is added to the collection and its score value is increment
>>> zincry zset3 1 "one"
>>> zincry Zset3 2 "
>>> zincry Zset3 3 "three"
The Zrank method returns the rank of member in Zset with the name key (sorted by score from small to large) that is subscript
>>> Zrank Zset3 Tow
The Zrevrank method returns the rank of member in the Zset with the name key (sorted by score from large to small) that is subscript
>>> Zrevrank Zset3
The Zrangebyscore method returns the element that specifies the range of Socre
>>> Zrangebyscore zset3 2 3 withsocres
Zcount returns the number of given intervals for the specified score
>>> Zcount Zset3 2 4
The Zcard method returns the number of all elements of the zset to the point key
Zremrangebyrank method to delete elements in a given interval in Zset
>>> Zremrangebyrank Zset3 1 1//delete Zset3 elements based on the index 1 to 1 of the table rank below
Zremrangebyscore method deletes the element of the specified score interval in the collection
>>> Zremrangebyscore Method Zset3 3 4


3. Redis common commands and advanced applications
Key-Value Related commands
Keys returns all keys that satisfy the given pattern
>>> keys *
>>> Keys r*
exists confirm if a key exists 0 does not exist 1 exists
>>> exists name
>>> exists age
Del Delete a key 1 success
>>> del Age
Expire setting the expiration time for a key
TTL gets the effective duration of a key
>>> expire age 10
>>> TTL age
Move moves the key of the current database to another library
Select Database
>>> Select 0
>>> Set Age 30
>>> Get Age
>>> Move age 1
>>> Get Age
>>> Select 1
>>> Get Age
Persist remove the expiration time for a given key
>>> expire Age 3000
>>> TTL age
>>> perisist Age
>>> TTL age
Randomkey randomly returns a key in the key space
>>> Randomkey
Rename Rename key
>>> Rename Set2 set_r_2
Type test key return value types
>>> type Set2
>>> type set_r_2

Server-related commands
Ping tests if the link is alive Pong survived or the connection fails
>>> Ping
Echo Prints some content on the command line
Select Database "Redis database number from 0~15"
Quit Quit connection
Dbsize returns the number of keys in the current database
>>> dbsize
>>> Select 1
>>> dbsize
Info getting Redis server-related information
>>> Info
Config get real-time dump of received requests, return related configuration data
>>> Config get dir
>>> Config Get *
>>> Config Get timeout
FLUSHDB Delete all keys in the current database
>>> dbsize
>>> Flushdb
>>> dbsize
Flushall Delete all keys for all databases

4. Redis Advanced Applications
Security:
Set the password that the client needs to use before marrying for any other assignment in the coming year
Warning: Because the Redis speed is very fast, so the password requirement is very strong, otherwise 1s can allow 150,000 times password attempt
Configuring the Requirepass password in the configuration file
#requirepass foobared
Requirepass Beijing

Auth Password after client login;
redis-cli-a Password at login time


Master-slave replication:
Master-slave replication allows multiple slave servers to have the same database copy as master server
Redis master-slave replication features
1) master can be used for multiple slave "secondary servers"
2) Multiple slave can be connected to the same master, and can also be linked to other slave
3) Master-slave replication does not block primary, while synchronizing data, Master can continue to process client requests
4) Improve the scalability of the system "principle: when a host machine, another slave immediately become a host (rely on Heartbeat)"

Redis master-slave replication process:
Slave connect with master, send sync Sync command
Master starts the background process, saves the database snapshot to a file, and the master master process starts the phone's new write command and caches
After the background has finished saving, send this file to slave
Slave Save this file to your hard disk

Configuring the master-Slave server
Configuring the slave server is simple, just add the following configuration to the slave configuration file
Slaveof 192.168.1.1 6379 #指定master的ip和端口
Masterauth Beijing #这是主机密码

Transaction processing:
Support transaction is simple, Redis weaver a command that guarantees a client-initiated transaction
You can write in a row, and the middle does not insert other commands.
When a multi command is issued in a connection, the connection enters a transaction context,
Subsequent commands for the connection are not executed immediately, but are placed in a queue first.
When only the EXEC command is written, Redis sequentially writes only all commands in the queue
Multi opening the transaction context
Discard cancel transaction, transaction rollback
Exec execution Transaction
>>> Get Age
>>> Multi
>>> Set age 10
>>> Set age 20
>>> exec
>>> Getage
Note: When executing a transaction context, if the command inside the transaction queue has an error, the transaction cannot be rolled back after execution. This is a simple transaction for Redis

Optimistic Lock: A version field is provided based on the database version, which records the updated versions of the data.
Redis optimistic Lock: If you have an age key open 2 sessions to assign an age to an argument oh, let's see what the result is.
1) Session 1
>>> Get Age
>>> Watch age//monitor if Age is modified
>>> Multi
2) Session 2
>>> Set Age 30
>>> Get Age
3) Session 1 after opening a transaction
>>> Set age 40
>>> exec
>>> Get Age
Persistence mechanism:
Redis supports two persistence modes:
1, snapshotting (snapshot) is also the default mode
2, Append-only (abbreviated AOF) way

snapshot mode;
Writes the in-memory data to a binary file as a snapshot. Default file name Dump.rdb
You can automatically make snapshot persistence by configuring settings.
Redis can be configured to take snapshots automatically if more than M key is modified within n seconds
Save 1 #900秒内如果超过1个key被修改 to initiate a snapshot save
Save #300秒内如果超过10个可以被修改, the snapshot save is initiated
Save 60 10000

AoF Way:
Because the snapshot mode has a certain time to build, aof than the snapshot mode has a better persistence,
Due to the use of AOF, Redis appends each write command to the file via the Write function.
After Redis restarts, the entire database content is rebuilt in memory by re-executing the write commands saved in the file.

Because the OS modifies the write in the kernel, it is not immediately written to disk, and the configuration file tells Redis
The time to force the OS to write to disk via the Fsync function
AppendOnly Yes//enable AOF persistence mode
#appendfsync always//Receive write commands to write to disk immediately, slowest, but guaranteed full persistence
Appendfsync everysec//write disk once per second, a good compromise in performance and persistence
#appendfsync no//full OS dependent, best performance, no guarantee for persistence

To publish a subscription message:
Pub/sub is a kind of message communication pattern, the main purpose is to remove the coupling between the message publisher and the message subscriber,
As a pub/sub server, Redis serves as a message routing feature between subscribers and publishers.
Subscribers can subscribe to the message types they are interested in by subscribe and psubscribe command Items Redis Server
Redis calls the message type channel channels, and when a publisher sends a specific type of information to Redis Server through the Publish command,
All clients of this information type will receive this message.



Use of virtual memory:
Redis's virtual memory, which swaps infrequently used data from memory to disk.
To configure a VM:
vm-enabled Yes # turn on VM features
Vm-swap-file/temp/redis.swap # swapped out the value of the saved file path
Vm-max-memory 1000000 # Maximum memory limit used by Redis
Vm-page-size #每个页面的大小32字节
Vm-pages 134217728 #最高使用多少页面
Vm-max-threads 4 #用于执行value对象换入换出的工作线程数量



Redis Summary Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.