Redis Cache Server

Source: Internet
Author: User

Redis Server

Remote Dictionay Server

Redis is a key-value persistence product, often referred to as a data structure server.

The Redis key is a string type; value can be a string, hash, list, set, sorted set, and so on;

In fact, Redis internally stores both the key and the value in the binary byte stream format.

installation Redis:

Redis Official website:

http://code.google.com/p/redis/

Installation method:

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

$ tar xvzf redis-1.02.tar.gz

$ CD redis-1.02

$ make

$ make Test

Deployment:

REDIS-CLI Redis-benchmark redis-server redis.conf to the specified directory.

To modify a configuration file:

In redis.conf you can specify the save path for the DB, which is saved in the current directory by default. See the notes in redis.conf for details.

Start:

./redis-server

Key operation

Exists key tests if key exists, returns 1 exists, 0 does not exist

Dbsize the number of keys in the current database

Dump key returns the serialized value

Del key1 Key2 ... Deletes the given key and returns the number of deleted keys

Type key returns the value type of the given key, and none indicates that no key exists

The Keys Patten returns all keys that match the specified pattern

Randomkey returns a randomly selected key

Rename Oldkey newkey Rename a key if the Newkey presence will be overwritten

Renamenx oldkey newkey Rename a key if the Newkey exists return failure

Expire key seconds specifies the expiration time for the key, relative time

Expireat key Timestamp specify expiration time for key, absolute time (Unix time)

TTL key returns the remaining number of expired seconds for the key that set the expiration time

Persist key to remove key expiration time

Value of Five Kinds Data Structure

1, String (strings)

Set key value inserts a piece of data (Key-value pair), overwriting the old value if the key already exists

Append key value adds new data to the existing key, at the end of its value, as if key does not exist, equivalent to the SET command

Setex key seconds value Atomicity completes two operations, one setting the key value to specify value, while setting the TTL of the key

Setnx key Val key does not exist when the record is inserted

Get key key index data, such as key does not exist, return nil

Getset key value sets key to the new value and returns the old value

Mget key1 Key2 Returns the value of multiple keys

Mset key1 val1 Key2 val2

GetRange Key start end returns a slice of value, double closed interval

SetRange Key offset value replaces the partial string value of key

SUBSTR Key Start end

Getbit

Setbit

Strlen Key value length

INCR Key value++ (atomic operation), value is treated as integral type

Incrby Key N value+=n

DECR Key value--

Decrby Key N value-=n

2, hash (hash table)

Hash table structure

Hset table field Value

Hmset table field1 val1 field2 val2 ...

Hget table field

Hmget table Field1 field2 field3 ...

Hdel table field

Hgetall table

Hkeys table

Hvals table

3. List (lists)

FIFO structure, implemented with linked lists, supports fast insertion of elements, but low lookup performance

Lpush list node is inserted in the list header

Rpush list node is inserted at the end of the list

Lpop list is removed from the list header

Rpop list is deleted at the tail of list

Linsert list before Val_b node inserts a new element in front of Val_b Val

Lrange list start end returns a slice of the list list[start:end], double closed interval, index can be negative, 1 represents the last element, 2 means the penultimate element

LTrim list start end cuts the list, preserving only the elements within the specified range

Llen List List length

4. Set (SET)

Stores a series of distinct values into a collection

Sadd set element to add elements to the collection

Srem Set element removes the specified elements from the collection

Smembers set returns all elements of the collection

Sinter Set1 Set2 Intersection

Sunion Set1 Set2 Fetch and set

Sdiff Set1 Set2 Take the difference set

5. Sorted set (ordered set)

Similar to set, but the data in sorted set has a score property, and all the elements in the collection are sorted by their score.

Sorted set adopts the implementation of the skip list structure, and the time complexity of inserting an element is O (logn)

Zadd Sorted_set score member add element and specify score

Zscore Sorted_set member returns the score of the specified element

Zrange sorted_set start end [Withscores] returns the element from start to end order, Zrange Sorted_set 0-1 returns all member

Zrevrange sorted_set start end reverse order, score from large to small

Zrangebyscore Sorted_set Min_score Max_score return score elements within a specified range

Zcard Sorted_set Returns the number of elements in the collection

Zcount Sorted_set min Max Returns the number of elements in the collection that are within the specified fraction range

Zremrangebyscore sorted_set min_score Max_score Delete elements within a specified range

Zrank Sorted_set member returns the rank of the element in the collection (calculated from 0)

Zincrby Sorted_set Increment member adds an increment to the score of the specified element increment

Zrem Sorted_set member Delete the specified element

6, Publish/subscribe (subscription)

You can push data to a message pipeline, and others can subscribe to these pipelines to get the information that is pushed over.

Publish Channlone Key

Subscribe Channlone

Common commands:

Info Print Redis Information

Select n Select a database (default 16 db supported)

Move key n Moves the key from the current library to the specified database

FLUSHDB emptying the current database

Flushall emptying all databases

Config get parameter read runtime parameters of the server (configuration items in the redis.conf file)

Config Set parameter value reconfigure run-time parameters

Save data Snapshot (DUMP.RDB)

Bgsave

Bgrewriteaof compressing aof persistent files

Shutdown stops all clients while persisting memory data in a blocking manner

Slaveof Host port Modify replication settings for the slave server

Pipeline (pipelining)

Instead of waiting for a response from the server immediately after the command is sent, the client can continue to send the following command.

After the command has been sent, the reply of all commands is read again.

Persistence (persistence)

Redis's data persistence is achieved by synchronizing the in-memory data to disk, and Redis supports two persistence modes:

1, snapshotting , the in-memory data is written to the binary file in a snapshot, the default file name is Dump.rdb.

Related configuration:

Save seconds changed # in a specified time, if more than changed key is modified, the snapshot is saved

Snapshot save process:

    • Fork a child process, the parent process continues to process the client request, the child process is responsible for the memory contents of the temporary file;
    • Because of the OS's write-time replication technology (copy on write), when the parent process processes a write request, the OS creates a copy of the page it wants to modify, so the address space data for the child process is a snapshot of the entire database at fork time;
    • After the child process writes the snapshot to the temporary file, it replaces the original snapshot file with a temporary file, and the child process exits.

Using snap shot, when Redis physical memory uses more than 3/5 of the total memory, there is a risk of crash.

The copy-on-write mechanism for fork invocation is based on the unit of the operating system page, where only dirty pages that are written are copied, and usually the system does not write to all pages in a short period of time, causing replication to occur.

The real reason for crash is that the buffer io is used for persistence, and the so-called buffer io refers to the page Cache of physical memory used by Redis for write and read operations to persisted files.

2, append-only file (aof) , snapshot mode is a certain interval to do once, if Redis sent an unexpected crash, will lose all the changes after the last snapshot.

The AoF method appends each received write command to the file via the Write function (by default, appendonly.aof), and rebuilds the contents of the entire database in memory by re-executing the Write command saved in the file when the Redis restarts.

Related configuration:

AppendOnly Yes # enable AOF persistence mode;

Appendfsync always # Every time you receive a write command, it is forced to write to disk immediately, the slowest, but guaranteed to be fully persisted;

Appendfsync Everysec # Force write to disk once per second

Appendfsync No # relies on the OS kernel's own caching mechanism

redis-check-aof--fix <filename> # repairing corrupted aof files

The aof-like approach to MySQL-based binlog can cause the log file to be too large to load when the system restarts the recovery data if the AoF method is very slow.

Transactions (Transactions)

Transaction characteristics:

1, all commands in the transaction will be serialized in the order of execution, during the transaction execution, Redis no longer provide any services for client requests, thus ensuring that all commands in the transaction are atomic execution;

2, Redis transaction if one command fails, the subsequent command will continue to execute;

3. When using AOF mode, Redis writes all writes within the transaction to disk in this call, and if a crash occurs during the write process (such as a power outage), then perhaps only some of the data is written to disk, and the Redis server performs a series of necessary consistency checks at reboot. If a problem is found, you can roll back some of the data that was written.

Commit and rollback operations for a transaction:

1, multi...exec command combination similar to the relational database of the BEGIN TRANSACTION ... Commit statement;

Watch Key1, Key2 ...

Multi

Cmd1

Cmd2

...

Exec

2, Multi...discard command combination similar to the relational database of the BEGIN TRANSACTION ... Rollback statement;

Multi

Cmd1

Cmd2

...

Discard

Before the Multi command executes, you can use the Watch command to specify the keys to be monitored;

Then, before exec executes, if the monitored keys have been modified (changed by another client), exec discards all commands in the transaction queue.

In a transaction, you can use the Unwatch command to cancel the keys of the current transaction monitoring;

If you have already executed the exec or discard command, you do not need to manually execute the unwatch command, because after that, all the monitored keys in the transaction are automatically canceled.

Master-slave Replication (replication)

Characteristics of the replication:

1, the same master can synchronize multiple slaves;

2, Master Server in a non-blocking way to provide services for slaves, so during the master-slave synchronization, the client can still submit queries or modify requests;

3.

How the Replication works:

After the slave is started and connected to master, it will actively send a sync command, and master will then start the background disk process and collect all the received commands to modify the dataset.

After the background process finishes executing, master will transfer the entire database file to slave to complete a full synchronization. The slave server, after receiving the database file data, will save it and load the memory.

Thereafter, Master continues to pass all the modified commands that have been collected, and the new modification commands to Slaves,slave will execute these data modification commands at this time to achieve final data synchronization.

Virtual Memory (Vsan)

Redis's virtual memory is designed to guarantee the lookup speed of keys, and only the value is exchanged to swap files.

Therefore, if Redis's memory problem is caused by too many value-small keys, then virtual memory does not solve the problem.

Redis does not use the virtual memory mechanism provided by the operating system, but instead implements its own virtual memory mechanism, mainly because of the following two points:

1, the operating system's virtual memory is 4 K pages for the smallest units, and most of the Redis objects are far less than 4K;

2, Redis can be swapped to disk objects to compress, generally compressed after the object is 10 times times smaller than the object in memory;

Related configuration:

vm-enabled Yes # Open virtual memory feature

Vm-max-memory bytes # Redis uses a maximum memory limit

Vm-swap-file Path # swapped out the value of the saved file path

Vm-page-size Bytes # page size

Vm-pages Number # page limit

Vm-max-threads Num # Number of worker threads executed value swapped in

A value can be kept in multiple page, but a page can only hold one value;

No value is exchanged until the memory used by Redis does not exceed vm-max-memory.

When the maximum memory limit is exceeded, Redis prioritizes an expired or larger object for Exchange:

Swappablility = Age * Log (Size_in_memory)

Vm-max-threads=0, blocking virtual memory is enabled and I/O operations are performed synchronously.

Communication Protocol (PROTOCOL)

Http://www.redisdoc.com/en/latest/topic/protocol.html

*< number of parameters > CR LF

$< number of bytes in Parameter 1 > CR LF

< data for parameter 1 > CR LF

...

$< number of bytes of parameter N > CR LF

Data for < parameter N > CR LF

Note: The command itself is also transmitted as an argument to the protocol;

For example: The client wants to send the following command to the server: set MyKey myvalue

The data formatted into the protocol is:

"*3\r\n$3\r\nset\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n"

Redis commands return different types of replies, each of which 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"

The status reply is usually returned by a command that does not need to return data (such as the SET command), which is not binary safe;

Bulk reply is used to return the binary safe string, the maximum length of the string is 512M, commonly used in the return result of the GET command;

If the requested key does not exist, it returns "$-1\r\n", which is called null Bulk Reply;

Multi Bulk reply is commonly used in commands such as Lrange, which returns multiple values.

Empty Multi Bulk Reply: "*0\r\n"

NULL Multi Bulk Reply: "*-1\r\n"

C Client Officer

Website:

Http://github.com/redis/hiredis

Installation method:

git clone Https://github.com/redis/hiredis

$ CD Hiredis

$ make

$ make Install

Compile:

Take Hiredis's own example.c program 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.