redis principle and cluster master-slave configuration

I. Introduction

  Storage Systems background

    There are three types of storage systems:
    the RDBMS
      Oracle, DH2, PostgreSQL, MySQL, SQL Server
    the NoSQL:
      KV the NoSQL: Redis, the memcached
      -column Column Family NoSQL: HBase, each line of each field can be retained n parts, a field is stored according to
      a document Documentation NoSQL: MongoDB
      schema NoSQL Graph: Neo4j

    NewSQL
      aerospike, foundationDB, rethinkDB ...

  1. What is redis

    redis is an open source use written in ANSI C, support network, based on the persistence of memory can log type, key-value database (a key-value storage systems), supports multiple languages ​​API

    It is an advanced key-value database, similar with memcached, but redis data can be persistent, and supports data types richer, while also supporting union, intersection, and complement computing services end of the set, and support a variety of sorting .  

      

  2, features:     

    And memcache used to compare, but redis is nosql, based on the stored key-value (key) data structure that can store keys, dictionaries, charts,
    fully working in memory, data is stored in memory, a good performance, data cycle of backup to your hard disk (persistent) single-threaded server
    can be extended by lua script
    support sentinel master-slave architecture highly available
    distributed  

 

  3, performance

    Although the single-threaded, kv is a mere simple memory cpu generally not form a bottleneck, official test 50 times 10w concurrent requests, write rate is 110,000 times / s, the speed of reading is 81000 times / s, the size of the read and write is 256bytes string; 

 

  4, persistence persistence

 

    RDB: snapshot, binary format; according to pre-customized policies, periodically save data to disk; the data files by default dump.rdb;
      clients can also use an explicit command to start the snapshot SAVA or BGSAVE save mechanism;
      the SAVE: synchronization, snapshots saved in the main thread; this time will block all client requests;
      bgsave: asynchronous,
    AOF of: Append Only file
      records for each write operation to the end of the file specified for persistence; when redis restart, re-execute the file by the command to rebuild the database in memory;
      BGREWRITEAOF: AOF overwriting files;
      not read AOF file is in use, but by the data in memory in a manner commands saved to a temporary file, replacing the original files after the completion of AOF;

  5 is a data structure server that supports value types are:

 

    String string, List list, Hash hash, Set collection, Sorted Set ordered set, Bitmap, HyperLoglog

    

    Strings:
      SET key value [EX #] [NX|XX]
      GET
      INCR
      DECR
      EXIST

    Lists:
      LPUSH
      RPUSH
      LPOP
      RPOP
      LINDEX
      LSET

    Sets:
      SADD
      SINTER
      SUNION
      SPOP
      SISMEMBER

    Sorted Sets:
      ZADD
      ZRANGE
      ZCARD
      ZRANK

    Hashem:
      HSET
      HSETNX
      HGET
      HKEY
      whales
      HDEL

    Bitmaps, HyperLogLog

 

  6, daemon

    Listening port 6379 / tcp

  7, database comparison

    Common database feature comparison

name Database Type Data Storage Options Action Type Remark
repeat Memory storage, nosql database Support for strings, lists, sets, hash mark, ordered collection Add, delete, modify, update, Support for distributed storage, master-slave synchronization and high availability, single-threaded
memcached Memory Database Cache Have a mapping between key Add, delete, modify, update, Support multithreading
mysql A typical relational database, RDBMS Database consists of more than tables, each table contains multiple rows Add, delete, modify, update, Support for ACID properties
postgresql A typical relational database, RDBMS Database consists of more than tables, each table contains multiple rows Add, delete, modify, update, Support for ACID properties
mongodb Hard disk storage, nosql database The database contains multiple tables Add, delete, modify, update, From the master copy, slice, replica set, spatial index

  

 

 

 

 

 

 

 

  Comparison between 8 and briefly memcached

    redis kv only supports simple data types, support list, set, hash and other data structures

    redis support data backup

    redis support data persistence, data in memory can be kept on disk, restart time can be loaded using

    Cluster, memcache do not support cluster, coexistence of multiple memcache

   9, redis the master / slave replication:

      Support more than one master slave

      slave can introduce links to other slave master link to replace him

      In the master copy is non-blocking, the slave is blocked

      Copy utilized to provide scalability, only provides inquiry and data redundancy in the slave terminal

     

  10, application scenarios

    Cache (data queries, short connections, news content, goods, content, etc.)

    Distributed cluster architecture separate session

    Chat online buddy list

    Task queue (spike, buy, 12306)

    Application Ranking

    Website statistics

    Statistics

In order to address the high concurrency, high availability, high scalability, large data storage and other issues arising database solutions

 

 

 

Second, the master-slave replication mechanism

   After redis redis solved the persistence service can restart to restore persistent data disk into memory, but redis server hard disk is broken will result in data loss, in order to avoid this single point of failure will have a master replication mechanism from

  From a multi-master configuration

  Will not be blocked from the master copy master, when synchronizing data, master can continue to process client requests

  That may be a redis master may be from

 

Third, the multi-instance configuration redis

  1, download and install the software source

    http://download.redis.io/releases/redis-5.0.4.tar.gz

    If the source installation Download Source

    If you configure yum install source

      rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

  2, is mounted in two installation

    1), yum install

      Direct yum install redis

      systemctl enable redis

      systemctl start redis

 

Dependencies Resolved    #一些依赖

====================================================================================================================
 Package                    Arch                     Version                           Repository              Size
====================================================================================================================
Installing:
 redis                      x86_64                   3.2.12-2.el7                      epel                   544 k
Installing for dependencies:
 jemalloc                   x86_64                   3.6.0-1.el7                       epel                   105 k

Transaction Summary
====================================================================================================================
Install  1 Package (+1 Dependent package)




Complete!
[root@web1 ~]# systemctl start redis
[root@web1 ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

[root@web1 ~]# netstat -untlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      2570/redis-server 1 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2050/nginx: master  
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2176/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      875/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      879/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2035/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      2050/nginx: master  
tcp6       0      0 :::22                   :::*                    LISTEN      875/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      879/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2035/master         
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           574/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           2334/chronyd        
udp        0      0 0.0.0.0:52822           0.0.0.0:*                           574/avahi-daemon: r 
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2176/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2176/dnsmasq        
udp        0      0 0.0.0.0:177             0.0.0.0:*                           890/lightdm         
udp6       0      0 ::1:323                 :::*                                2334/chronyd        
udp6       0      0 :::177                  :::*                                890/lightdm         
[root@web1 ~]# 

 

    2), compile and install

      Unzip -> compile -> copy -> Start

     

   The compile and install a new version 

      mkdir / the Data 

      cd / the Data 

      upload and download packages into this directory 

        Redis - 5.0 . 4 . Tar .gz 

      tar redis- 5.0 . 4 . Tar .gz 
      cd rredis - 5.0 . 4 
      the make 

 

  3, compile and install and start

    1) Create a folder and copy the key file

     mkdir /data/redis2

   cp redis.conf /data/redis2

   cp src/redis-server /data/redis2

   cd /data/redis2/       

    2) edit the configuration file

       the redis.conf

    

Port 6380 
appendOnly NO Yes // log switching, packet error log is not, binary recording data changes
PidFile / var / RUN / redis_6380.pid // PID name logfile " /data/redis2/redis.log " // log log yes daemonize // backstage start   
dbfilename dump.rbd // persistent data file     

    3) Start

[root@web1 redis2]# ./redis-server ./redis.conf 
[root@web1 redis2]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      2570/redis-server 1 
tcp        0      0 192.168.216.51:6380     0.0.0.0:*               LISTEN      15434/./redis-serve 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2050/nginx: master  
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2176/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      875/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      879/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2035/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      2050/nginx: master  
tcp6       0      0 :::22                   :::*                    LISTEN      875/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      879/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2035/master         
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           574/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           2334/chronyd        
udp        0      0 0.0.0.0:52822           0.0.0.0:*                           574/avahi-daemon: r 
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2176/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2176/dnsmasq        
udp        0      0 0.0.0.0:177             0.0.0.0:*                           890/lightdm         
udp6       0      0 ::1:323                 :::*                                2334/chronyd        
udp6       0      0 :::177                  :::*                                890/lightdm         
[root@web1 redis2]# 

 

 

 

    4) variable, assign a value, normally closed, non-normal shutdown

[root @ web1 redis2] # Redis-cli -h 192.168 . 216.51 -p 6380  
192.168 . 216.51 : 6380 > 
 192.168 . 216.51 : 6380 > 
 192.168 . 216.51 : 6380 > 
 192.168 . 216.51 : 6380 > the SET Home ZXG // assign 
the OK 192.168 . 216.51 : 6380 > GET Home // value
 " ZXG " 
192.168 . 216.51 : 6380 > GET abc123
(error) ERR wrong number of arguments for 'get' command
192.168.216.51:6380> set abc 123 
OK
192.168.216.51:6380> get abc
"123"192.168.216.51:6380> KEYS *    //查看所有的key
1) "abc"
2) "home"
192.168.216.51:6380> 
192.168.216.51:6380> SHUTDOWN // normally closed, non-normal shutdown is kill the

 

   

    

Fourth, the master-slave configuration

  1, the configuration from the server

   redis master-slave configuration is very simple, just need to change from one configuration server to

   the redis.conf

 

slaveof 192.168.216.51 6379

  2, start the service

[root@web2 ~]# systemctl start redis
[root@web2 ~]# systemctl enable redis

  3, viewing the main test results from the state

[root@web1 redis2]# redis-cli -h 192.168.216.51 -p 6379
192.168.216.51:6379>  info replication 
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.216.52,port=6379,state=online,offset=99,lag=0
master_repl_offset:99
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:98
192.168.216.51:6379> 

 

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.216.51
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:253
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>

Note 1: Read-only from the server

    

    New master key test zxg

192.168.216.51:6379> set test zxg
OK
192.168.216.51:6379> get test
"zxg"
192.168.216.51:6379> 

    slave see if there is value

127.0.0.1:6379> get test
"zxg"
127.0.0.1:6379> 

 

 

Please indicate the source: https://www.cnblogs.com/zhangxingeng/p/10743476.html 

 

 

Guess you like

Origin www.cnblogs.com/zhangxingeng/p/10743476.html