Redis Cache Server

Source: Internet
Author: User
Redis Server Remote dictionay ServerRedis is a key-value persistence product, usually called a Data Structure server. Redis keys are of the string type; values can be of the string, hash, list, set, sorted set, and Other types; in fact, redis stores both the key and value in the format of a binary byte stream. Install Redis:
Official redis Website:
Http://code.google.com/p/redis/

Installation Method:
Http://code.google.com/p/redis/wiki/QuickStart

$ Tar xvzf redis-1.02.tar.gz
$ Redis-1.02 CD
$ Make test

Deployment:
Mount redis-cli redis-benchmark redis-server redis. conf to the specified directory.
Modify the configuration file:
In redis. conf, you can specify the DB storage path, which is saved in the current directory by default. For details, see the notes in redis. conf.
Start:
./Redis-Server
  Key operationExists key test key exists, return 1, 0 does not exist dbsize current database key count dump key return serialized value del key1 key2... delete a given key. Return the number of deleted keys. Type key: return the value type of the given key, if none is returned, keykeys Patten does not exist. All keyrandomkeys matching the specified mode are returned. A randomly selected keyrename oldkey newkey is returned. If newkey exists, renamenx oldkey newkey is overwritten to rename a key, if newkey exists, return the failure expire key seconds and specify the expiration time for the key. The relative time expireat key timestamp specifies the expiration time, absolute time (UNIX time) TTL key returns the remaining expiration seconds of the key with the specified expiration time. persist key removes the expiration time of the key. Value Five Types Data Structure  1. String (string)Set key value inserts a data (key-Value Pair). If the key already exists, it overwrites the existing key of the old valueappend key value pair and adds new data at the end of the value; if the key does not exist, it is equivalent to the setex key seconds value set command to perform two operations. One is to set the key value to the specified value, at the same time, when the ttlsetnx key Val key of the key does not exist, the get key index data of the record is inserted. If the key does not exist, the nilgetset key value is returned to set the key to the new value, return the old valuemget key1 key2 and return the value of multiple keys. mset key1 val1 key2 val2getrange key start end returns a shard of the value, double Closed Interval setrange key offset value replaces Part of the string value of the key substr key start end getbit setbit strlen key value length incr key value ++ (atomic operation ), treat value as an integer. incrby key N value + = ndecr key value -- decrby key N value-= N 2. Hash (hash table)Hash table structure hset table field valuehmset table field1 val1 field2 val2... hget table fieldhmget table field1 field2 field3... hdel table field hgetall tablehkeys tablehvals table 3. List)The FIFO structure is implemented using a linked list and supports fast element insertion, however, the query performance is relatively low. lpush list node inserts rpush list in the list header. node inserts lpop list at the end of the list. Delete rpop list in the list header. Delete linsert list before val_ B node before val_ B. the new element vallrange list start end returns a slice list of the list [start: end], double closed interval, index can be negative,-1 indicates the last element,-2 indicates the last second element ltrim list start end to cut the list, only the length of the element llen list within the specified range is retained. 4. Set)Store a series of non-repeated values into a set sadd set element to add element Srem set element to the set delete the specified Element smembers set from the set returns all elements of the Set sinter set1 set2 get intersection sunion set1 set2 get Union set sdiff set1 set2 get difference set 5. Sorted set)Similar to set, data in sorted set has a score attribute. All elements in the set are sorted by score. Sorted set adopts the Skip List structure. The time complexity of inserting an element is O (logn) zadd sorted_set score member, specify scorezscore sorted_set member to return the scorezrange sorted_set start end [withscores] of the specified element to return the elements from start to end. zrange sorted_set 0-1 returns all memberzrevrange sorted_set start end reverse orders, returns zrangebyscore sorted_set min_score max_score in the order of score. returns the zcard sorted_set element of score in the specified range. returns the number of elements in the set. zcount sorted_set min Max returns the number of elements in the specified score range in the set. number of elements zremrangebyscore sorted_set min_score max_score Delete the zrank sorted_set member element of the score within the specified range to return the ranking of elements in the Set (calculated from 0) zincrby sorted_set increment member adds an incremental incrementzrem sorted_set member to the score of the specified element to delete the specified Element         6. Publish/subscribe (subscription)Data can be pushed to an information pipeline, and others can subscribe to these pipelines to obtain the pushed information. Publish channlone keysubscribe channlone Common commands:Info print redis Information Select n select database (16 databases are supported by default) move key n move key from current database to specified database flushdb clear current database flushall clear all Database Config get parameter read Server Runtime parameters (redis. configuration items in the conf file) config set parameter value reconfiguration runtime parameter save data snapshot (dump. RDB) bgsavebgrewriteaof compression aof persistent file shutdown stop all clients, and execute memory data persistence slaveof host port in blocking mode to modify the replication settings of the slave server Pipeline)After the client sends the command, it does not need to wait for the response from the server immediately. Instead, it can continue to send the subsequent command. After the command is sent, it can read the response of all previous commands at one time. Persistence)Redis data persistence is achieved by synchronizing data in the memory to the disk. redis supports two persistence methods: 1, SnapshottingTo write data in the memory to the binary file as a snapshot. The default file name is dump. RDB. Related configuration: Save seconds changed # If more than changed keys are modified within the specified time, the snapshot saving process is initiated:
  • Fork is a sub-process. The parent process continues to process client requests. The sub-process is responsible for writing memory content into temporary files;
  • Because of the OS copy on write technology, when the parent process processes write requests, the OS creates a copy of the page to be modified, therefore, the address space data of the sub-process is a snapshot of the entire database at the fork moment;
  • After the sub-process writes the snapshot to the temporary file, it replaces the original snapshot file with the temporary file, and then the sub-process exits.
Using the snap shot method, there is a risk of crash when the redis physical memory usage exceeds 3/5 of the total memory usage. The copy-on-write mechanism called by fork is based on the operating system page, that is, only the dirty pages written will be copied, generally, the system does not write all pages in a short period of time, resulting in replication. The real reason for crash is that buffer Io is used persistently. The so-called buffer Io means that redis uses the physical memory page cache for writing and reading persistent files. 2, Append-only file (AOF)The Snapshot method is performed at intervals. If redis sends an unexpected crash, all modifications made after the last snapshot will be lost. Aof method will append each received write command to the file through the write function (default is appendonly. aof), when redis restarts, it re-executes the write command stored in the file to recreate the entire database content in the memory. Related configuration: appendonly yes # enable the aof persistence mode; appendfsync always # forcibly write data to the disk immediately upon receiving the write command, which is the slowest, but ensures full persistence; appendfsync everysec # force write to disk once per second appendfsync no # dependent on the OS kernel's own cache mechanism redis-check-Aof -- fix <FILENAME> # repair damaged aof file aof method similar to MYSQL based statement BINLOG mode, the log file size may be too large. When the system restarts to recover data, if the aof method is used, loading data will be very slow. Transactions)Transaction Features: 1. All commands in the transaction will be executed in a serialized order. During the transaction execution, redis will not provide any service for client requests, this ensures that all commands in the transaction are executed by the atom; 2. If a command fails to be executed in the redis transaction, the subsequent command will continue to be executed; 3. When the aof mode is used, redis writes all write operations in the transaction to the disk in this call. If a crash (such as power failure) occurs during the write process, only part of the data may be written to the disk, when the redis server is restarted, it performs a series of necessary consistency checks. If any problem is found, it can roll back some data that has been written. Transaction commit and rollback: The command combination of 110000multi...exe C is similar to the begin transaction of the relational database... commit statement; watch key1, key2... multi extension 12.162...exe C 2, multi... the Discard command combination is similar to the relational database begin transaction... rollback statement; multicmd1cmd2... before executing the multi command, you can use the watch command to specify the keys to be monitored. If the monitored keys are modified (changed by other clients) before executing exec ), exec will discard all commands in the transaction queue. In a transaction, you can use the unwatch command to cancel the keys monitored by the current transaction. If you have executed the exec or discard command, you do not need to manually execute the unwatch command because after that, all monitored keys in the transaction are automatically canceled. Master-slave Replication)Replication features: 1. the same master can synchronize multiple slaves; 2. The master server provides services for slaves in non-blocking mode. Therefore, during master-slave synchronization, the client can still submit query or modify requests. 3. Working Principle of replication: After slave is started and connected to the master, it will send a sync command, and then the master will start the background storage process, at the same time, collect all received commands for modifying the dataset. After the background process is executed, the master will send the entire database file to slave to complete a full synchronization. The slave server saves the database file data and loads it into the memory. After that, the master continues to send all the collected modification commands and the new modification commands to slaves at a time. slave will execute these data modification commands this time to achieve the final data synchronization. Virtual Memory)Redis's virtual memory is used to ensure the key search speed. It only switches the value to the swap file. Therefore, if the redis memory problem is caused by too many keys with small values, the virtual memory will not solve the problem. Redis does not use the virtual memory mechanism provided by the operating system, but implements its own virtual memory mechanism, mainly because of the following two points: 1. The virtual memory of the operating system is carried out in the smallest unit of 4 K pages, while most redis objects are much smaller than 4 K. 2. redis can compress the objects exchanged to the disk, generally, the compressed object is 10 times smaller than the object in the memory. Related Configuration: VM-enabled yes # enable the virtual memory function VM-max-memory bytes # maximum memory used by redis VM-Swap-file path # the file path stored by the exchanged value VM-page- size bytes # page size VM-pages number # maximum number of pages VM-max-threads num # The number of worker threads that execute value exchange can be kept in multiple pages, however, only one value can be saved on a page. No value can be saved until the memory used by redis does not exceed VM-max-memory. Exchange any value. When the maximum memory limit is exceeded, redis preferentially selects expired or large objects for switching: swappablility = age * log (size_in_memory) VM-max-threads = 0, the blocking virtual memory is enabled and I/O operations are performed in synchronous mode. Protocol)Http://www.redisdoc.com/en/latest/topic/protocol.html * <parameter quantity> cr lf $ <parameter 1 byte quantity> CR LF
<Data in parameter 1> CR LF
...
$ <Number of bytes of parameter n> CR LF
<Data of parameter n> cr lf note: the command itself is also transmitted as a parameter of the Protocol. For example, the client must send the following command to the server: set mykey myvalue is formatted as the following data: "* 3 \ r \ N $3 \ r \ NSET \ r \ N $5 \ r \ nmykey \ r \ N $7 \ r \ nmyvalue \ r \ n" redis command different types of replies are returned, each type of reply is distinguished by the first byte: 1. Status reply: "+", for example: "+ OK \ r \ n" 2. Error reply: "-", for example: "-err Unknown command 'foobar'" 3. interger reply: ":", for example: ": 1000 \ r \ n" 4. Bulk reply: "$", for example: "$6 \ r \ nfoobar \ r \ n" 5. multi bulk reply: "*", for example: "* 4 \ r \ N $3 \ r \ nfoo \ r \ N $3 \ r \ nbar \ r \ N $5 \ R \ nhello \ r \ N $5 \ r \ nworld \ r \ n "status reply is usually returned by commands that do not need to return data (such as the SET command ), this type of reply is not binary safe; bulk reply is used to return the binary safe string. the maximum length of the string is 512 MB, which is often used to return the result of the GET command; if the requested key does not exist, "$-1 \ r \ n" is returned, which is called null bulk reply. multi bulk reply is commonly used in commands such as lrange, and multiple values are returned. Empty multi bulk reply: "* 0 \ r \ n" null multi bulk reply: "*-1 \ r \ n"  
C client Officer

Official Website:
Http://github.com/redis/hiredis

Installation Method:
Git clone https://github.com/redis/hiredis

$ CD hiredis
$ Make install
Compile:
Take the example. C program that comes with hiredis as an example:
Gcc-lhiredis example. c
 

Redis Cache Server

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.