NoSQLのRedis-Redisの導入と構成


リレーショナル・データベースと非リレーショナル・データベース
      :
         ,          ,       
  oracle,mysql,sqlserver,db2
       :
                 ,         
  redis,mongdb,hbase,couhdb

非リレーショナル・データベース生成の背景
           
              
               

Redisの概要
Redisはメモリベースで動作し、永続化をサポート
key-value(キー値ペア)の格納形式を採用
メリット:
           
         
        
   
      

1、必要な環境コンポーネントをインストールし、redisをインストールする
[root@localhost ~]# yum install gcc gcc-c++ make -y  ##      
[root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/  ##  
Password for root@//192.168.100.3/LNMP-C7:  
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# tar zxvf redis-5.0.7.tar.gz -C /opt/  ##  
[root@localhost mnt]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make  ##  
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis/ install  ##  

2.構成Redisプロファイルスクリプトを実行し、構成する
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh    ##        
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]   ##    
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]   ##    
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]   ##    
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]   ##    
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server
##       
[root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/   ##      
[root@localhost utils]# netstat -ntap | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      44510/redis-server  
[root@localhost utils]# /etc/init.d/redis_6379 stop  ##  redis
Stopping ...
Redis stopped
[root@localhost utils]# /etc/init.d/redis_6379 start  ##  redis
Starting Redis server...
[root@localhost utils]# vim /etc/redis/6379.conf   ##      
bind 127.0.0.1 192.168.13.128  ##      
[root@localhost utils]# /etc/init.d/redis_6379 restart  ##  redis  
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost utils]# redis-cli -h 192.168.13.128 -p 6379   ##  redis
192.168.13.128:6379> help set  ##help  

    SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
    summary: Set the string value of a key
    since: 1.0.0
    group: string

192.168.13.128:6379> set teacher zhangsan   ##     
OK
192.168.13.128:6379> set tea red
OK
192.168.13.128:6379> KEYS *   ##      
1) "teacher"
2) "tea"
192.168.13.128:6379> keys t??  ##    t          
1) "tea"
192.168.13.128:6379> get tea   ##     
"red"
192.168.13.128:6379> EXISTS tea  ##       
(integer) 1     ##1   ,0    
192.168.13.128:6379> EXISTS teas
(integer) 0
192.168.13.128:6379> del teacher  ##   
(integer) 1
192.168.13.128:6379> KEYS *
1) "tea"
192.168.13.128:6379> type tea   ##      
string
192.168.13.128:6379> rename tea t1   ##     
OK
192.168.13.128:6379> keys *
1) "t1"
192.168.13.128:6379> get t1
"red"
192.168.13.128:6379> exit   ##  

3、圧力測定を行う
[root@localhost utils]# redis-benchmark -h 192.168.13.128 -p 6379 -c 100 -n 100000
##  100,100000   
====== SET ======
    100000 requests completed in 1.14 seconds   ##       
    100 parallel clients
    3 bytes payload
    keep alive: 1

84.66% <= 1 milliseconds
98.48% <= 2 milliseconds
99.69% <= 3 milliseconds
99.90% <= 18 milliseconds
100.00% <= 18 milliseconds
87642.41 requests per second

====== GET ======
    100000 requests completed in 1.13 seconds
    100 parallel clients
    3 bytes payload
    keep alive: 1
    [root@localhost utils]# redis-benchmark -h 192.168.13.128 -p 6379 -q -d 100
    ##       set/get      
    SET: 90497.73 requests per second
    GET: 90991.81 requests per second

4キー値ペアを他のライブラリに移動(合計16ライブラリ)
[root@localhost utils]# redis-cli -h 192.168.13.128 -p 6379         ##  Redis            
192.168.13.128:6379> select 10  ##   11  
OK
192.168.13.128:6379[10]> keys *
(empty list or set)
192.168.13.128:6379[10]> select 0  ##   1  
OK
192.168.13.128:6379> move t1 10  ##       11  
(integer) 1
192.168.13.128:6379> select 10  ##   11  
OK
192.168.13.128:6379[10]> keys *   ##    
1) "t1"
192.168.13.128:6379[10]> get t1
"red"
192.168.13.128:6379[10]> flushdb  ##      
OK
192.168.13.128:6379[10]> keys *     ##     
(empty list or set)
192.168.13.128:6379[10]> select 0   ##       
OK
192.168.13.128:6379> keys *      ##      
1) "myset:__rand_int__"
2) "mylist"
3) "key:__rand_int__"
4) "counter:__rand_int__"
192.168.13.128:6379>

Redis持続化
Redis       ,          
      Redis  ,        ,     Redis            ,    

永続化分類
RDB  :             Redis        
AOF  :              ,              

RDB持続化
Redisのデフォルトの永続化方法
デフォルトのファイル名dump.rdb
トリガ条件:
         ,          (      )
  save   bgsave(  )  
  flushall  ,         
  shutdown  ,                 

メリットとデメリット:
          
                  ,RDB      
            
       

RDBファイルによるデータ復旧
 dump.rdb     redis      bin   ,  redis    

RDB永続化の構成
[root@localhost utils]# vim /etc/redis/6379.conf 

#900          
save 900 1

#300       10    
save 300 10

#60       10000    
save 60 10000

#              ,     save     RDB

#RDB    
dbfilename dump.rdb

#RDB    
dir /var/lib/redis/6379

#      
rdbcompression yes

AOF持続化
Redis     
  RDB   (       )
               ,       
Redis                                  

AOFファイルによるデータ復旧
 appendonly.aof     redis     bin   ,  redis    

AOF持続化構成
[root@localhost utils]# vim /etc/redis/6379.conf 

#  AOF   
appendonly yes

#AOF    
appendfilename "appendonly.aof"

#always:     ,               
# appendfsync always

#everysec:    ,       (   )
appendfsync everysec

#no:   ,            
# appendfsync no

#               
aof-load-truncated yes

AOFの書き換えメカニズム
AOF                ,            
 AOF              ,Redis   AOF       

AOF書き換えの原理
Redis fork      ,        (        ),            ,      aof  

AOF書き換え構成
[root@localhost utils]# vim /etc/redis/6379.conf 
#     BGREWRITEAOF ,      yes           fsync,
#        ,      I0    ,         。redis    no
no-appendfsync-on-rewrite no

#  AOF            AOF       ,  BGREWRITEAOF  
auto-aof-rewrite-percentage 100

#  AOF    BGREWRITEAOF      ,
#       Reids              BGREWRITEAOF
auto-aof-rewrite-min-size 64mb

Redisパフォーマンス管理
##  redis    
[root@localhost utils]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> info memory

メモリフラグメント率
●         used_ _memory_ _rss  redis      
used_ _memory    
●               /         
          
●          redis             
        1    ,             
       1.5,  redis            150%,  50%      
       1 ,  Redis           ,            

メモリ使用率
●redis                ,         
   swap    
●      
          
      Hash    
  key     

回収キー
●      redis       
●               ,      key     
              
redis.conf       maxmemory-policy   
- volatile-lru:  LRU                    
- volatile-ttl:                         (    )
- volatile-random:                      
- allkeys-lru:  LRU              
- allkeys-random:              
- no-enviction:      

読んでくれてありがとう