[NOSQL] About Redis

Source: Internet
Author: User
Tags redis

Redis Overview

Redis was developed by Salvatore Sanfilippo in 2009 for its start-up company Lloogg. It is still an independent project at the moment. But VMware likes the project (the author is its employee). It is implemented in C language, so it performs very well.

BSD license is used. Store using key values. And Amazon Dynamo,cassandra,riak. Voldemort,memcache similar.

Support for rich data types. For example, arrays, linked lists. Collections are ideal for Web services that need to express timelines. such as Weibo.

The data types supported by Redis are:

    • String
    • Linked list
    • Collection
    • Ordered collection
    • Hash table
Redis's master-slave replication Redis comes with a master-slave copy function, just set the slaveof option for the profile redis.conf, as seen in the following:

High availability for Redis

So far, the official Redis is still developing redis-cluster, can be Http://redis.io/topics/cluster-spec, Chinese version http://www.cnblogs.com/chang290/ Archive/2012/09/05/2672506.html

But we can use the Keepalived+redis method to achieve high availability, for example, as seen in the following:

1. Configuration of Redis
Host?? Port? Role
Redis0 6379? Master
Redis1 6379? Slave


2. Configuration of the Keepalived
Redis0 and Redis1 use a virtual IP

and use the following script, for example, to monitor whether the Redis service survives:

#!/bin/bash/usr/local/bin/redis-cli-h 192.168.1.53-p 6379 Info >/dev/nullif [$?-eq 0]; Then        echo "Redis OK"        exit 0else        echo "No Redis service found!"        /usr/local/bin/redis-server/path/to/redis.conf        # Try to start it again        /usr/local/bin/redis-cli-h 192.168.11.53-p 6380 Info >/dev/null        if [$?-eq 0]; then                exit 0        Else                # restart failed                Killall Keepalived                echo "Error"        Fifi


To implement Redis failback, you can use the Notify_master configured by keepalived to notify_backup both methods to run unique scripts. In fact, there are only 2 scripts on slave (that is, redis1), and the first one is used after REDIS1 takes over the virtual IP. Run Slaveof no one to turn yourself into master. The second is to ensure that REDIS0 is restored to the primary state after REDIS1 has surrendered the virtual IP and REDIS0 runs slaveof no one. and to Redis1 run slaveof redis0 6379 start again sync data from master, assuming that you are already slave is not necessary synchronization.
Redis1 on the Keepalived configuration method such as the following, REDIS0 only to remove the Notify_master, notify_backup two lines can be.

! Configuration File for keepalivedglobal_defs {router_id redis1}vrrp_script monitor_redis {script '/opt/redis_ keepalive.sh "Interval weight 2}vrrp_instance" state Buckup # (host is master, backup is Backup) interface Eth0 # (HA Monitor network interface) VIR TUAL_ROUTER_ID 110 # (master, standby virtual_router_id must be the same) Mcast_src_ip 192.168.11.53 # (Multicast source IP, set to native extranet IP, same network card as VIP) This key is not set Priority 70 # (master, standby to take different priorities.) Host values are large. The backup machine value is small, the higher the value the higher the priority) Advert_int 1 # (VRRP multicast broadcast cycle seconds) authentication {  ...} notify_master/opt/redis_2master.sh notify_backup/opt/redis_2backup.sh Track_script {Monitor_redis # (call Nginx Process test script)} virtual_ipaddress {192.168.11.4 # ( VRRP ha virtual address)}}

Redis's persistent Redis has the following 2 types of persistence mechanisms:
    • Snapshot (snapshot)
    • AOF (Append only File)
The snapshot feature configuration for Redis is as follows:

The Redis snapshot principle is as follows:

    • Redis uses the copy on write mechanism of the fork command.

      When a snapshot is generated. Fork the current import thread out of a subprocess, and then loop through all the data in the import process. Writes data to an RDB file.


    • Redis's original Rdb file is not broken, because its write operation is import line in a new import thread. When a new, RDB file is generated, the Redis-generated import process will first write the data to a temporary file. The temporary file is then renamed to an Rdb file by means of an atomic rename system call, which fails at the time of anything. Redis's RDB files are always available.
    • At the same time. The Rdb file for Redis is also a part of the Redis master-Slave synchronization internal implementation.
    • We can very clearly see that the RDB has its shortcomings, that is, once the database fails, then the data stored in our Rdb file is not entirely new. All the data from the last Rdb file generation to Redis outage was lost.

      In some businesses, this is tolerable, and we recommend that these businesses be persisted using an RDB approach. The cost of opening an RDB is not high.


The configuration of the AOF is as follows:

The AOF takes precedence over an RDB (that is, a snapshot), with an RDB performance better than aof. Because there is no repetition.
AOF Rewrite: Generate a copy of the AOF file again. A record in the new AoF file will only be done once. Unlike an old file, multiple operations on the same value may be logged.

Its build process is similar to an RDB, and it also fork a process that directly iterates through the data and writes a new aof temporary file.

In the process of writing a new file, all of the write logs will still be written to the old aof file, which is also recorded in the memory buffer at the same time.

When the operation is finished, the logs in all buffers are written to the temporary file once.

Then call the atomic Rename command to replace the old aof file with the new AoF file.

[NOSQL] Introduction to Redis

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.