Redis cache database application concept and redis basic operation

Posted by Crave on Sun, 16 Jan 2022 02:00:58 +0100

Basic concept and deployment of redis

1. Introduction to redis

Redis is a NoSQL database based on key value pairs. Unlike many key value pair databases, the values in redis can be composed of string, hash, list, set, zset, geo and other data structures and algorithms.

redis will put all data in memory, so its read-write performance is very fast. redis can also save the data in memory on the hard disk in the form of snapshots and logs. redis also provides additional functions such as key expiration, publish and subscribe, transaction, pipeline and so on

2. Features of redis

  • Fast speed
    • All redis data is stored in memory
    • redis is implemented in c language
    • redis uses a single thread architecture
  • Data structure server based on key value pair
    • The so-called key value pair is key/value
    • Five data structures: string, hash, list, set and ordered combination
  • Rich functions
    • Key expiration function is provided to realize caching
    • It provides publish and subscribe function, which can realize message system
    • The pipeline function is provided. The client can pass a batch of commands to redis at one time, reducing the network overhead
  • Stable and simple
    • There are few source codes. After version 3.0, there are only 50000 lines of code
    • Using the single thread model makes the redis server processing model simple
    • Does not depend on class libraries in the operating system
  • The client supports many languages
    • java,php,python,c,c++,nodejs
  • Persistence
    • RDB and AOF
  • colony
    • Master slave replication
    • Sentinel mode
    • colony

3.redis application scenario

  • Cache (key expiration time)
    • The cache can be set by the key expiration time
    • Cache session
      • Each website has a login expiration time. For example, if you don't log in for 24 hours, you will quit
    • Cache the user information. If the data cannot be found, go to mysql and write it to redis again
  • Leaderboards (lists and ordered collections)
    • The ranking function can be made through redis list and ordered collection
    • Heat ranking
    • Release time ranking
  • Counter application
    • Number of Posts viewed
    • Video playback times
    • Number of product views
  • social networks
    • Step on / like, fans, common friends, preferences, push, tag
  • Message queuing system
    • It can be used as a warehouse. When all the data is taken away, the warehouse will be deleted
    • It can cooperate with elk to realize Japanese collection

4. Deployment, configuration, startup and shutdown of redis

4.1. Deploy redis

1.establish redis Deployment path
[root@redis-1 ~]# mkdir -p /data/redis_cluster/redis_6379/{conf,pid,logs,data}
[root@redis-1 ~]# tree /data/redis_cluster/
/data/redis_cluster/
└── redis_6379
    ├── conf
    ├── logs
    ├── pid
    └── data
    
2.download redis    
[root@redis-1 ~]# mkdir /data/soft
[root@redis-1 ~]# cd /data/soft
[root@redis-1 /data/soft]# wget https://repo.huaweicloud.com/redis/redis-3.2.9.tar.gz

3.Easy to install redis
[root@redis-1 /data/soft]# tar xf redis-3.2.9.tar.gz -C /data/redis_cluster/
[root@redis-1 /data/soft]# cd /data/redis_cluster/
[root@redis-1 /data/redis_cluster]# ln -s redis-3.2.9/ redis
[root@redis-1 /data/redis_cluster]# cd redis/src
[root@redis-1 /data/redis_cluster/redis]# make && make install

4.2.redis executable

After make install, the executable commands of redis are stored in / usr/local/bin

[root@redis-1 /data/redis_cluster/redis]# ll /usr/local/bin/redis-*
-Rwxr-xr-x. 1 root 2433000 January 27 13:32 / usr / local / bin / redis benchmark
-Rwxr-xr-x. 1 root 25128 January 27 13:32 / usr / local / bin / redis check AOF
-Rwxr-xr-x. 1 root 5181912 January 27 13:32 / usr / local / bin / redis check RDB
-Rwxr-xr-x. 1 root 2585960 January 27 13:32 / usr / local / bin / redis cli
lrwxrwxrwx. 1 root 12 January 27 13:32 / usr / local / bin / redis Sentinel - > redis server
-Rwxr-xr-x. 1 root 5181912 January 27 13:32 / usr / local / bin / redis server

4.3. Use the official redis script to generate the redis configuration file

[root@redis-1 ~]# cd /data/redis_cluster/redis/utils/
[root@redis-1 /data/redis_cluster/redis/utils]# sh 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/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...

After execution/etc/redis/You can see the configuration file under the directory
[root@redis-1 /data/redis_cluster/redis/utils]# ls /etc/redis/
6379.conf

4.4. Configure redis and start

Generally, after the compilation and installation, the redis directory is useless. In the production environment, a new redis directory is created to store configuration files and start it directly, because many redis will be deployed on one machine

1.to configure redis
[root@redis-1 ~]# vim /data/redis_cluster/redis_6379/conf/redis_6379.conf 
#Start in daemon mode
daemonize yes

#Bind host address
bind 192.168.81.210 127.0.0.1

#Monitor end
port 6379

#Path of pid and log files
pidfile /data/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /data/redis_cluster/redis_6379/logs/redis_6379.log

#Set the number of databases. The default value is 0
databases 16

#Specifies the file name of the local persistent file. The default is dump rdb
dbfilename redis_6379.rdb

#Directory of local database, persistent file path
dir /data/redis_cluster/redis_6379/data


2.start-up redis
[root@redis-1 ~]# redis-server /data/redis_cluster/redis_6379/conf/redis_6379.conf


3.View process and port numbers
[root@redis-1 ~]# ps aux | grep redis
root      16597  0.1  0.4 136972  7528 ?        Ssl  13:57   0:00 /usr/local/bin/redis-server 127.0.0.1:6379

[root@redis-1 ~]# netstat -lnpt | grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      16597/redis-server 

4.5.redis closing method

Neither relational database nor cache database can be directly kill ed, which will lead to the loss of data files

To close redis, you need to enter shutdown in the redis cli interactive mode, or use redis cli shutdown

[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> SHUTDOWN
not connected> exit

[root@redis-1 ~]# ps aux | grep redis

Second closing redis Method of
[root@redis-1 ~]# redis-cli shutdown

Topics: Redis