Redis Getting Started Tutorial

Source: Internet
Author: User
Tags benchmark key string set set redis server

) About Redis
Redis is an open source log that is written in ANSI C language, supports the network, and can be persisted based on memory
Type, Key-value database.
2) Data type
2.1. Redis Key
The Redis key is a string type, but the key cannot include a boundary character because key is not a binary safe
strings, so keys such as "My Key" and "mykey\n" that contain spaces and line breaks are not allowed.
2.1.1. Key related Instructions
Exits key detects whether the specified key exists, returns 1 for existence, 0 does not exist
Del key1 key2 ... keyN deletes the given key, returns the number of deleted keys, 0 means the given key does not exist
Type key returns the kind of the given key value. Returns none indicates that key does not exist, string character type, list linked lists
Type set unordered collection type ...
Keys pattern returns all keys that match the specified pattern
Randomkey returns a randomly selected key from the current database and returns an empty string if the current database is empty
Rename Oldkey newkey Rename a key, if Newkey exists, will be overwritten, return 1 to indicate success,
0 failed. The Oldkey may not exist or be the same as Newkey.
Renamenx Oldkey Newkey Ibid, but if Newkey exists, the return fails.
Expire key seconds Specifies the expiration time, in seconds, for the key. Returns 1 success, 0 indicates that key has already been set
Period of time or does not exist.
The TTL key returns the number of remaining expired seconds of the set over expiration key. 1 indicates that the key does not exist or the expiration time is not set.
Select Db-index selects the database by index, the default connected database is 0, and the default number of databases is 16. Returns 1
Indicates success, 0 failed.
Move key Db-index moves the key from the current database to the specified database. A return of 1 indicates success. 0 = key
Does not exist or is already in the specified database.
2.2. Redis's Vaule
Redis offers five types of data: String,hash,list,set and sorted set.
2.2.1. String type
The string is the most basic type, and the string type is binary safe. This means that the Redis string can
contain any data. For example, JPG images or serialized objects. From an internal implementation, a string can actually be considered a byte
3 array with a maximum limit of 1G bytes.
Introduction to the String type data manipulation directives
The S et key value sets the key corresponding to the string type value, which returns 1 for success, and 0 for failure.
Setnx key value If key does not exist, set key to the value of type string. If key already exists, return to
Back to 0.
Get key gets the string value corresponding to key, if key does not exist return nil
Getset key value gets the key first, and then sets the value of key. Returns nil if key does not exist.
Mget key1 Key2 ... keyN gets the value of more than one key at a time and returns nil if the corresponding key does not exist.
Mset key1 value1 ... keyN Valuen set the value of multiple keys at once, successfully returning 1 means that all values are set
, the failure returns 0 to indicate that no value is set.
Msetnx key1 value1 ... keyN Valuen sets the value of multiple keys at once, but does not overwrite keys that already exist
INCR key does the + + operation on the value of key and returns the new value. Note INCR A value that is not an int returns an error
Error, incr a nonexistent key, set the key value to 1.
DECR key to the value of key to do-operation, decr a nonexistent key, then set the key value is-1.
Incrby key integer to the key plus the specified value, key does not exist when the key is set, and think the original value
is 0.
Decrby key integer Subtracts the specified value from key. Decrby is purely for readability, and we can completely pass the Incrby
A negative value to achieve the same effect, and vice versa.
2.2.2. Hash type
Hash is a string-type field and value mapping table. Add, the delete operation is O (1) (average).
Hash is particularly useful for storing objects. Relative to Gencun each word of an object into a single string type. Adds an object
Storing in hash types consumes less memory and makes it easier to access the entire object. The reason for saving memory is
When a new hash object is created, it is initially stored with Zipmap (also known as small hash). This zipmap actually and
Not hash table, but zipmap compared to the normal hash implementation can save a lot of the hash itself needs some of the elements
Data storage overhead. Although Zipmap's additions, deletions, and lookups are all O (n), the General object's field
The number is not too much. So the use of Zipmap is also very fast, that is, add delete average or O (1). If field
Or if the value exceeds a certain limit, Redis automatically replaces the zipmap with the normal hash implementation internally.
This restriction can be specified in the configuration file.
Hash-max-zipmap-entries 64
#配置字段最多 of 64
Hash-max-zipmap-value 512
#配置 value is 512 bytes maximum
Description of the hash type data operation instruction
Hset key field value sets the hash field to the specified value, or if key does not exist, creates
Hget key field gets the specified hash field.
Hmget Key FILED1....FIELDN gets all the specified hash filed.
Hmset key filed1 value1 ... filedn Valuen also set multiple fields for hash.
Hincrby key field integer adds the specified hash filed plus the specified value. Successful return of the hash filed after the change
Value.
Hexists key field detects whether the specified field exists.
Hdel key field deletes the specified hash field.
Hlen Key returns the number of field for the specified hash.
4hkeys Key returns all field of hash.
Hvals Key returns all the value of the hash.
Hgetall returns all filed and value of hash
2.2.3. List type
List is a linked list structure that can be understood as a doubly linked list where each child element is a string type. Main functions
Can be push, pop, get all values of a range, and so on. Key in operation is understood as the name of the linked list.
Introduction to the List type data manipulation directives
Lpush key string adds a string element to the header of the key corresponding to the list, returns 1 for success, and 0 for key save
In and not a list type.
The Rpush key string adds a string element at the end of the key corresponding list.
Llen Key returns the length of key corresponding to the list, if key does not exist return 0 if key corresponds to type is not list
Returns an error.
Lrange Key start end returns the element within the specified interval, with the subscript starting at 0, the negative value being calculated from the back, and 1 representing
The countdown first element, key does not exist, returns an empty list.
LTrim key start end intercepts the list within the specified range of elements and returns 1,key without a return error.
LSet Key index value sets the value of the element in the list specified by the subscript, successfully returning 1,key, or the subscript does not exist return
Error.
Lrem Key Count value removes a certain number (count) from the List's head (count positive) or tail (count negative)
Matches the element of value, returning the number of deleted elements. Count is 0 when all is removed.
Lpop key is removed from the head of the list and returns the Delete element. If the key corresponds to the list does not exist or is null to return nil,
If the key corresponds to a value other than list returns an error.
Rpop key is removed from the tail of the list and returns the Delete element.
Blpop Key1 ... keyN timeout scans from left to right, returns a Lpop operation on the first non-empty list, and returns
For example Blpop List1 list2 list3 0, if the list does not exist list2,list3 are non-empty then to List2 do
Lpop and returns the element that was removed from the list2. If all the lists are empty or nonexistent, the timeout is blocked
Seconds, and a timeout of 0 means that it has been blocked. When blocking, if there is a client to any key in the Key1...keyn
When the push operation is performed, the first client that is blocked on this key will return immediately. If the timeout occurs, the return
Nil Sort of like a Unix select or poll.
Brpop with Blpop, one is removed from the head and one is removed from the tail.
2.2.4. Set type
Set is an unordered collection, which can contain (2 of 32 square-1) elements. The set is implemented by hash table,
So the complexity of adding, deleting, and finding is O (1). The hash table is automatically resized as it is added or removed.
It is important to note that when adjusting the hash table size, synchronization (acquisition of write locks) is required to block other read and write operations. May not
After a long time, you will use a skip list to implement. The jump table has been used in sorted sets. About Set collection types
In addition to the basic add-delete operation, other useful operations include the collection Union, the intersection (intersection),
Subtraction (difference). Through these actions can easily realize the SNS in the Friend referral and blog tag function.
Introduction to 5set type data manipulation directives
Sadd Key member adds a string element to the key corresponding to the set set, successfully returning 1 if the element and
Returns an error if the 0,key corresponding set does not exist in the collection.
Srem Key member removes the specified element from the key corresponding set and successfully returns 1 if member is not in the collection
exists or key does not exist return 0 if key corresponds to a value that is not of the set type returns an error.
Spop key deletes and returns the key corresponding to a random element in the set, if the set is empty or the key does not exist return
Nil
Srandmember key with Spop, randomly takes an element in set, but does not delete the element.
Smove Srckey Dstkey member remove Srckey from the member corresponding set and add it to the dstkey corresponding set.
The entire operation is atomic. Successful return 1 if member does not exist in Srckey returns 0 if key is not set
The type returned an error.
SCard Key returns the number of elements of set, if set is empty or key does not exist return 0.
Sismember Key member determines whether member is in set, there is a return 1,0 that does not exist or key does not
Exist.
Sinter key1 Key2 ... KeyN returns the intersection of all given keys.
Sinterstore dstkey key1 ... KeyN return the intersection of all given keys and save the intersection to Dstkey.
Sunion key1 Key2 ... KeyN returns the set of all the given keys.
Sunionstore dstkey Key1 ... KeyN returns the set of all the given keys and saves and sets them to Dstkey.
Sdiff key1 Key2 ... KeyN returns the difference set for all given keys.
Sdiffstore dstkey Key1 ... KeyN returns the difference set for all given keys and saves the difference to Dstkey.
Smembers key returns all elements of the set corresponding to the key, and the result is unordered.
2.2.5. Sorted set Type
The sorted set is an ordered set, which adds a sequential property to the set based on the addition of a fixed
You can specify when you change the element, and automatically re-adjust the order of the new values each time you specify it. You can understand that there are two columns of
MySQL table, a column of value, and a list of the order of storage. Key is understood as the name of the sorted set in the operation.
Introduction to Sorted Set type data operation directives
Add key score member adds an element to the collection, and the element exists in the collection to update the corresponding score.
Zrem Key member Deletes the specified element, 1 indicates success if the element does not exist return 0.
Zincrby Key INCR member increases the score value of the corresponding member, then moves the element and keeps the skip list
Order. Returns the updated score value.
Zrank Key member returns the rank (subscript) of the specified element in the collection, and the elements in the collection are small to large by score
Sort of.
Zrevrank Key member Ibid, but the elements in the collection are sorted by score from large to small.
Zrange key start end resembles the lrange operation to specify the element of the interval from the collection. Returns an ordered result
Zrevrange key Start end Ibid, the return result is in reverse order of score.
Zrangebyscore Key min Max returns the element in the collection that score at a given interval.
Zcount Key min Max returns the number of score in the collection for a given interval.
Zcard Key returns the number of elements in the collection.
The Zscore key element returns the score corresponding to the given element.
6zremrangebyrank Key min Max deletes the element in the collection that is ranked in the given interval.
Zremrangebyscore key min Max deletes elements from the collection score at a given interval
3) Persistence
Typically, Redis stores data in memory or in virtual memory, and it implements the data in the following two ways
Long-lasting.
3.1. Snapshot mode: (default persistence mode)
This is the way in which the in-memory data is written to the binary in a snapshot, the default file name is
Dump.rdb.
The client can also use the Save or Bgsave command to notify Redis to do a snapshot persistence. The save operation is
The main thread that holds the snapshot, because Redis is using a main thread to handle requests from all clients, this method can hinder
Plug all client requests. So it is not recommended. Another point to note is that each snapshot persistence is the amount of memory
It is not incremental to synchronize incremental data until it is fully written to disk. If the amount of data is large, the write operation will compare
Will inevitably cause a lot of disk IO operations, which can severely impact performance.
Note: Because the snapshot is done at a certain interval, if Redis accidentally becomes a machine, it will
All data modifications after the last snapshot are lost.
3.2. How to append the log:
In this way, Redis appends each received write command to the file via the Write function (the default
APPENDONLY.AOF). When Redis restarts, the entire memory is rebuilt by re-executing the Write command saved in the file.
The contents of a database. Of course, because the operating system caches write modifications in the kernel, it may not be immediately written
to the disk. This persistence is still likely to lose some of the modifications. But we can tell by the configuration file
Redis we want to force the operating system to write to disk by using the Fsync function. There are three ways to do this (the default is:
Fsync once per second)
AppendOnly Yes
#appendfsync always
The persistence of, not recommended to use
Appendfsync everysec
, recommended
#appendfsync No
Enable the log append persistence mode
Each time a write command is received, it is forced to write to the disk immediately, the slowest, but guaranteed full
Force writes to disk once per second, doing a good job of performance and persistence
Full reliance on operating system, best performance, no guarantee of persistence
The log Append method also brings up another problem. Persistent files can become more and more large. For example we call INCR
Test command 100 times, the file must save all 100 commands, in fact, there are 99 are redundant. Because to restore
Database state The fact that a set test 100 is saved in the file is sufficient. In order to compress the log file for this persistence mode.
Redis provides the bgrewriteaof command. Receive this command Redis will use the data in memory in a way similar to the snapshot
Save to the temporary file as a command, and replace the original persistent log file.
74) virtual memory (for cases where value is larger than key)
4.1. Introduction to Redis virtual memory
First, the following Redis virtual memory and operating system virtual memory is not matter, but the idea and purpose are phase
The same. is to temporarily swap infrequently accessed data from memory to disk, freeing up valuable memory space. For
In a memory database like Redis, memory is never enough. In addition to splitting data into multiple Redis servers, you can
Outside Another way to improve database capacity is to use virtual memory technology to exchange data that is infrequently accessed
to the disk. If the data we store is always accessed with a small portion of the data, most of the data is rarely accessed,
It is true that only a small number of users are always active on the website. When a small amount of data is accessed frequently, the use of virtual memory is not only
Can increase the capacity of a single Redis database server and will not have too much impact on performance.
Redis does not use the virtual memory mechanism provided by the operating system but itself implements its own virtual inside the user state.
The existing mechanism.
The main reasons are the following two points:
1. The operating system's virtual memory is exchanged in the smallest units of the 4k/page. And most of the Redis objects are far smaller.
At 4k, there may be multiple Redis objects on an operating system page. In addition, the collection object types for Redis are as follows
List,set may exist on more than one operating system page. It may eventually cause only 10% of the key to be accessed frequently, but
Is that all operating system pages are considered active by the operating system so that only the operating system is actually exhausted
The page will be exchanged.
2. The mode of exchange compared to the operating system. Redis can compress objects that are swapped to disk, save to disk
Pointers and object metadata information can be removed as well. Generally compressed objects are 10 times times smaller than objects in memory. This
The virtual memory of the sample Redis will do much less IO than the operating system's virtual memory.
4.2. Redis virtual memory-related configuration
vm-enabled Yes
#开启虚拟内存功能
Vm-swap-file/tmp/redis.swap
#交换出来 the file path saved by value/tmp/redis.swap
Vm-max-memory 268435456
Maximum memory limit #redis Used (256MB), after the upper limit
Redis starts swapping value into the disk swap file. Recommended setting for 60%-80% of system free memory
Vm-page-size 32
#每个 the size of a Redis page 32 bytes
Vm-pages 134217728
#最多在文件中使用多少个页, size of swap file =
(Vm-page-size * vm-pages) 4GB
Vm-max-threads 8
The number of worker threads swapped out #用于执行 the value object. 0
Indicates that no worker threads are used (details are described later)
Redis's virtual memory is designed to guarantee key query speed and only exchange value to swap files
In If the memory problem is caused by too many key's small value, then Redis's virtual memory cannot be solved
Problems. Like the operating system, Redis also swaps objects by page. Redis rules that the same page can only hold one
Object. However, an object can be saved on multiple pages. Before Redis uses less memory than vm-max-memory
is not to exchange any value. When the maximum memory limit is exceeded, Redis chooses to swap older objects to swap
File. If two objects are as old as the older one, the more large objects are exchanged, and the exact exchange calculation formula
swappability = Age*log (size_in_memory). The settings for Vm-page-size should be based on your application
Sets the size of the page to the size that can accommodate most objects. Too big will waste disk space, too small will cause exchange
8 files are too fragmented. For each page in the interchange file, Redis uses a 1bit value in memory to correspond to the
The idle state of the recording page. So like the number of pages in the configuration above (Vm-pages 134217728) consumes 16MB of memory to remember
The idle state of the recording page. Vm-max-threads represents the number of worker threads used for the swap task. If the greater than 0 recommended setting
The number of cores for the CPU of the server. If it is 0, the exchange process takes place on the main thread.
4.3. Introduction to how Redis virtual memory works
4.3.1. When Vm-max-threads is set to 0 o'clock (blocking mode)
Swap out
After the main thread periodically checks that the memory exceeds the maximum limit, the selected object is saved to swap directly in a blocking manner.
File, and frees the memory space that the object occupies, this process repeats until the following conditions are met
1. Memory usage drops below maximum limit
The 2.swap file is full.
3. Almost all of the objects have been swapped to disk.
Swap in
When a client requests a value that has been swapped out, the main thread loads the corresponding
The value object, which at the time of loading will block all clients. Then process the client's request
4.3.2. When Vm-max-threads is greater than 0 o'clock (working thread mode)
Swap out
When the main thread detects that more than the maximum limit is used, the object information that is selected for Exchange is placed in a queue and handed to the worker
Thread background processing, the main thread continues to process client requests.
Swap in
If a client-requested key has been swapped out, the main thread will first block the client that issued the command and then load the
Information in a queue to allow the worker to load. A worker thread notifies the main thread after loading is complete. The main thread again
Executes the command for the client. This way only blocking the requested value is the client that has been swapped out for key.
In general, the performance of blocking is better because there is no need for thread synchronization, creating threads, and recovering blocked guest
The cost of the client. But the response is also sacrificed accordingly. The working thread mode main thread does not block on disk IO,
To be more responsive. If our application doesn't happen too often, and we don't care about a bit of delay, it's recommended
Use blocking mode.
For more detailed information about Redis virtual memory, refer to the following link
Http://redis.io/topics/internals-vm
5) Master-Slave synchronization
5.1. Introduction to Redis master-slave replication
9Redis supports synchronizing data to multiple slave libraries, a feature that is useful for improving read performance.
1) master can have multiple slave.
2) In addition to multiple slave connected to the same master, slave can also connect other slave to form a graphic structure.
3) Master-slave replication does not block This means that when one or more slave and master synchronize the data for the first time
, master can continue to process requests from clients. Conversely, slave will block when the data is first synchronized
The client's request cannot be processed.
4) Master-slave replication can be used to improve the scalability of the system, we can use multiple slave dedicated to the client's read
Requests, such as the sort operation, can be handled using slave. can also be used to do simple data redundancy.
5) You can disable data persistence in master and only need to comment out all the save configurations in the master configuration file.
Only configure data persistence on slave.
5.2. Introduction to Redis Master-slave replication process
When the slave server is set up, slave will establish a connection to master and then send the Sync command. Whether it is
The first time the connection is established or after the connection is disconnected, master initiates a background process that sets the data
The library snapshot is saved to a file, and the master master process starts collecting new write commands and caches them. Background process finished
After the file is written, Master sends the file to Slave,slave to save the file to disk and then loads it into the memory recovery
Database snapshot to Slave. The master then forwards the cached command to slave. and subsequent master receives
The write command will be sent to slave by the connection that was established. Commands for synchronizing data from master to slave and from the guest
The commands sent by the client use the same protocol format. When master and slave are disconnected, slave can automatically re-
Establish the connection. If master receives multiple slave simultaneous connection commands at the same time, only one process is started to write the number
The database is mirrored and then sent to all slave.
Configuring the slave server is simple, just add the following configuration to the configuration file
Slaveof 192.168.1.1 6379
#指定 Master's IP and port
Installation and configuration of appendix A:redis
1.1. Installation
1.1.1. Compiling and installing
$ wget http://redis.googlecode.com/files/redis-2.2.7.tar.gz
$ tar xzf redis-2.2.7.tar.gz
$ cp-r Redis-2.2.7/usr/local/redis
$ cd/usr/local/redis
$ make
$ make Install
# The compiled files will be copied to the/usr/local/bin
#redis-server
#redis The Daemon launcher for the-server:redis server
#redis-CLI
#redis the-cli:redis command-line operation tool. Of course, you can also use Telnet to operate on its plain text protocol.
#redis-benchmark
#redis-benchmark:redis Performance Testing tool to test the read and write performance of Redis in your system and in your configuration
$redis-benchmark-n 100000–c #模拟同时由 50 clients send 100,000 Sets/gets
10 Consultation
# redis-check-aof
Redis-check-aof: Update log check
#redis-check-dump
#redis-check-dump: Local Database check
1.1.2. Configuration
Modify the configuration file and copy it to the ETC directory
VI redis.conf
$CP redis.conf/etc/redis.conf
Configuration file Basic Description
Daemonize:
#是否以后台守护进程方式运行
Pidfile:
#pid File Location
Port
#监听的端口号
Timeout
#请求超时时间
LogLevel
#log information level, with a total of four levels supported: Debug, verbose, notice, warning,
Default is verbose
LogFile
#默认为标准输出 (stdout), if configured to run as a daemon, and this
is set to logging as standard output, the log will be sent to/dev/null
Databases
#开启数据库的数量. To switch operations to each database using the Select Library ID mode
Save * *:
#保存快照的频率, the first * indicates how long, the second * indicates how many write operations
For Snapshots are automatically saved when a certain number of writes are performed within a certain amount of time. You can set multiple conditions.
Rdbcompression: #保存快照是否使用压缩
Dbfilename:
#数据快照文件名 (just the file name, excluding the directory). The default value is Dump.rdb
Dir
#数据快照的保存目录 (This is the directory)
Requirepass:
#设置 Redis Connection Password, if the connection password is configured, the client needs to connect to Redis
To provide a password via the AUTH <password> command, it is turned off by default.
1.1.3. Starting Redis
$redis-server/etc/redis.conf
1.1.4. Turning off Redis
$ redis-cli Shutdown
#关闭指定端口的 Redis-server
$redis-cli-p 6379 shutdown
111.1.5. Updating the installation of Redis
Other installations, only the last make install need to turn off the old version of Redis that is running.
1.1.6. Introduction to Redis System Management related directives
Dbsize returns the number of keys in the current database.
INFO returns the current Redis server status and some statistical information.
Monitor monitors and returns all request information received by the Redis server in real time.
SHUTDOWN Save the data to disk and close the Redis service.
CONFIG get parameter gets a Redis configuration parameter information. (Individual parameters may not be available)
CONFIG Set parameter value sets a Redis configuration parameter information. (Individual parameters may not be available)
CONFIG Resetstat resets the statistics for the info command. (Reset includes: Keyspace hit count,
Keyspace number of errors, number of processing commands, number of received connections, expired keys)
Debug OBJECT key gets debug information for a key.
DEBUG Segfault Manufacturing a server.
FLUSHDB Delete all keys in the current database, this method does not fail. Be careful with caution
Flushall Delete all keys in all databases, this method does not fail. Be careful with caution
Appendix B: Installing the Phpredis module
Https://github.com/nicolasff/phpredis
Download Phpredis Latest Version
Extract
> CD Phpredis
>/usr/local/php5/bin/phpize #这个 phpize is to install the PHP module
>./configure–with-php-config=/usr/local/php5/bin/php-config
> Make
> CP modules/redis.so/usr/local/php5/etc/redis.so
Next, add extension=redis.so in the php.ini.
Restart Apache
PHP Code Testing
<?php
$redis = new Redis ();
$redis->connect (
$redis->connect (' 127.0.0.1
127.0.0.1′′,6379);
$redis->set (
$redis->set (' Test
Test ', ' Hello world!
World! ");
Echo $redis->get (
$redis->get (' Test
Test ");
?>
Phpredis Method Description:
12https://github.com/nicolasff/phpredis/blob/master/readme.markdown#readme
References and knowledge expansion
Redis Command Daquan:
Http://redis.io/commands
Redis instruction Online Simulation exercise:
http://try.redis-db.com/
13

Redis Getting Started Tutorial

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.