Introduction to Redis features and its installation and configuration

Table of contents

1. Know Redis

Redis main features

Main application scenarios

2.MySQL VS NoSQL

3. Redis installation and configuration

redis5 installation 

Modify the configuration file 

 start redis

4. Redis client

command line client

Graphical interface client

Self-developed client based on redis API


1. Know Redis

Redis (Remote Dictionary Server) is a middleware for storing data based on client-server architecture . It is an in-memory database , a type of NoSQL (non-relational database) , which can be used as a database , cache/session storage , and message queue .

It is usually used as an intermediate cache layer to store frequently accessed data in memory, thereby greatly improving read performance. The cache server in the master-slave separation/hot and cold separation architecture mentioned above can be implemented with Redis to improve the reading performance of hot data.

Redis main features

1. Key-value storage: Redis uses a simple key-value pair (KV) data model . Each key is associated with a unique value through which the corresponding value can be quickly accessed and manipulated.

2. Memory storage: Redis stores data in memory to achieve high-speed read and write operations (it also introduces IO multiplexing, and one thread manages multiple sockets) . This enables Redis to achieve very low latency and high throughput data access

3. Programmable: It can be operated directly through simple interactive commands, or it can be executed in batches through scripts

4. Extensible: Redis provides a set of APIs to expand on the original functions (to support more data structures and commands), and write Redis extensions in several languages, which is essentially a dynamic link library

5. Persistence and backup: Provides a persistence function to write data to disk or other persistent storage media. In addition, some RDS also support data backup and restore functions to prevent data loss

6. Support clusters: support horizontal expansion (similar to sub-database and sub-table), and shard data to multiple nodes to improve storage and processing capabilities

7. High availability: Redis supports master-slave replication (Master-Slave Replication) and sentinel (Sentinel) mechanisms to provide high availability and fault tolerance of data. By configuring master-slave replication and sentinel nodes, Redis can achieve automatic failover and failover

Main application scenarios

1. Real-time data store (Real-time data store), using Redis as a database. Applicable to business scenarios with high performance requirements

2. As a cache/session storage (Caching & session storage), MySql has a large space for storing data, but the reading speed is slow, which cannot meet the requirements of caching

When storing sessions, cookies are used to store the identity (sessionId) of user information, which requires session cooperation, and Redis actually stores user information. No matter which application server the load balancer distributes the login request to, the session can be obtained from Redis

3. Message queue (server) (Streaming & messaging), which can realize the network version of the producer-consumer model (advantages: decoupling; peak cutting and valley filling)

Note: Since memory is used to store data, Redis cannot be used to store large-scale data. Consider using other databases

2.MySQL VS NoSQL

Redis is a NoSQL database

Data model:

  • MySQL is a relational database management system (RDBMS) that uses tables to organize data and uses Structured Query Language (SQL) for data manipulation. Data is stored in the form of rows and columns, and the data schema and data type need to be predefined.
  • NoSQL is a non-relational database, and its data model can be key-value pairs (Key-Value), documents (Document), column families (Wide-Column), or graphs (Graph). NoSQL databases are more flexible and do not require predefined fixed data schemas

Scalability:

  • There are some limitations in MySQL's horizontal expansion, and the conventional deployment is usually a stand-alone or master-slave replication architecture. While performance and scalability can be improved through techniques such as partitioning and sharding, they are relatively complex.
  • NoSQL databases are usually inherently scalable, easy to build distributed clusters, and can be expanded horizontally by adding more nodes to meet the needs of large-scale data and high concurrent access.

Strong consistency and flexibility:

  • MySQL supports strong consistency, which means that all read operations see the latest write operations. Ensure data consistency and integrity through transaction and lock mechanisms.
  • Some types of NoSQL databases, such as key-value stores, may relax consistency requirements in exchange for higher performance and availability. This weak consistency model is more applicable in some scenarios.

Query complexity:

  • As a relational database, MySQL supports SQL language for complex queries, and can perform join operations, aggregate functions, and multi-table associations.
  • The query method of NoSQL database is relatively simple, usually using key-value pairs or query syntax similar to JSON. They are more suitable for scenarios with simple data access patterns and flat data structures.

If you need powerful transaction support, complex queries and consistency guarantees, and you already have an existing SQL architecture and tool chain, then MySQL is a good choice. For large-scale data, high concurrency and flexible data models, and easier horizontal expansion, NoSQL databases can be considered.

In addition, the two can also be used in combination in specific scenarios, such as using MySQL as the primary database, and then using a NoSQL database as a secondary or cache database to improve performance.

3. Redis installation and configuration

redis5 installation 

Redis does not officially support the Windows version and needs to be installed in Linux

Pre-work: Install the Linux environment

We install Redis5 on CentOS, if it is CentOS8, the default redis in the yum warehouse is version 5, directly yum install

If it is CentOS7, the default redis in the yum warehouse is version 3, you cannot directly yum install

Need to install additional software source first - scl source

yum install centos-release-scl-rh

install redis5 

Modify the configuration file 

After the installation is complete, because the default installation directory is relatively deep and complicated, we set the key content that needs to be used in a convenient directory through a symbolic link, which is equivalent to a shortcut

1) Set up a symbolic link for the executable program

[root@localhost user]# cd /usr/bin
[root@localhost bin]# ln -s /opt/rh/rh-redis5/root/usr/bin/redis-server ./redis-server
[root@localhost bin]# ln -s /opt/rh/rh-redis5/root/usr/bin/redis-sentinel ./redis-sentinel
[root@localhost bin]# ln -s /opt/rh/rh-redis5/root/usr/bin/redis-cli ./redis-cli

 2) Set a symbolic link against the configuration file

[root@localhost etc]# ln -s /etc/opt/rh/rh-redis5/ ./redis
[root@localhost etc]# ll redis/
总用量 76
drwxr-xr-x. 2 root  root     6 4月  17 2019 opt
drwxr-xr-x. 2 root  root     6 4月  17 2019 pki
drwxr-xr-x. 5 root  root    52 7月  10 07:36 pm
-rw-r-----. 1 redis root 61916 10月 25 2021 redis.conf
-rw-r-----. 1 redis root  9837 10月 25 2021 redis-sentinel.conf

Modify the configuration file

1) Set the ip address

First enter the redis configuration file

Change the ip address 127.0.0.1 to 0.0.0.0 

 2) Set the protected mode to no

 3) Start the daemon

Server programs generally run as a background process, and processes in Linux are divided into foreground processes and background processes. The foreground process will end when the terminal is closed, and the background process will not be closed when the terminal is closed

4) Set the working directory

create working directory

In the configuration file, set the working directory

[root@localhost redis]# mkdir -p /var/lib/redis
[root@localhost redis]# vim redis.conf

5) Set the log directory

Create the log directory first

[localhost redis]# mkdir -p /var/log/redis/

In the configuration file, set the log directory

 start redis

Start the redis command: redis-server /etc/redis/redis.conf

[root@localhost redis]# mkdir -p /var/log/redis/
[root@localhost redis]# redis-server /etc/redis/redis.conf
[root@localhost redis]# netstat -anp | grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      64192/redis-server  
[root@localhost redis]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

ping: do a heartbeat test with the redis service, if the server is normal, it will return pong 

Redis starts successfully and binds to port 6379. The process id is 64192

Close redis: query the pid of the redis server according to netstat or ps, and then kill it according to the pid

Close redis command: kill 64192

If you modify the configuration file later, you need to restart redis to take effect 

View the log files generated by redis

[root@localhost redis]# cd /var/log/redis/
[root@localhost redis]# ll
总用量 4
-rw-r--r--. 1 root root 2496 7月  10 08:31 redis-server.log
[root@localhost redis]# vim redis-server.log

4. Redis client

Redis is also an in-memory database based on client-server (Client-Server) architecture

command line client

The built-in command line client can be started directly by command

redis-cli -h <host> -p <port>

Where <host>is the host name or IP address of the Redis server, <port>and is the port number that the Redis server listens to. 

Enter a password (if password protected). If the Redis server is password protected, the command line client will prompt for a password for authentication. After entering the password, press the Enter key 

Graphical interface client

Desktop programs or web programs are rarely used and rely on the windows system. After connecting to the windows system, there will be many restrictions.

Self-developed client based on redis API

The most important form in the work, the follow-up will demonstrate the process of developing the client through the java perspective


Note : The "fastness" of redis is relative to relational databases such as mysql. If it is directly compared with the operating variables in memory, it has no advantage or even slower.

For example, for a stand-alone system, if the application needs to store KV structure data, is it better to use redis or HashMap?

The introduction of redis must be slower than the variable HashMap, because map directly operates memory, and redis first operates memory through the network

Whether to use redis or not depends on the actual needs. The disadvantage of introducing redis is that it will be slower, but the data can be stored separately, and the subsequent restart of the server will not affect the data content. Using hashmap, the data will be gone after the server restarts.

In addition, as described above, if you want to expand into a distributed system in the future, it is better to use redis

Guess you like

Origin blog.csdn.net/chenchenchencl/article/details/131622480