Redis configuration for NoSQL

1. Relational database and non-relational database

1. Relational database

1. Instance -> database -> table -> record row (row), data field (column)
2. Relational database is a structured database, created on the basis of a relational model (two-dimensional table model), Generally oriented towards records.
3. SQL statement (standard data query language) is a language based on relational database, used to perform the retrieval and operation of data in the relational database.
4. Simply put, the relational model refers to the two-dimensional table model, and a relational database is a data organization composed of two-dimensional tables and the connections between them.
5. Mainstream relational databases: Oracle, MySQL, SQL Server, Microsoft Access, DB2, etc.

(1). Commonly used concepts in relational models

Explanation Description
relationship Can be understood as a two-dimensional table, each relationship has a relationship name, which is usually called the table name
Tuple Can be understood as a row in a two-dimensional table, often referred to as a record in the database
Attributes Can be understood as a column in a two-dimensional table, often referred to as a field in the database
area The value range of the attribute, that is, the value limit of a column in the database
Keyword A group of attributes that can uniquely identify a tuple, often called the primary key in the database, and consists of one or more columns
Relationship model Refers to the description of the relationship. The format is: relationship name (attribute 1, attribute 2,……, attribute N), which becomes a table structure in the database

(2). The advantages of relational databases

1. Easy to understand: The two-dimensional table structure is a concept very close to the logical world. The relational model is easier to understand than other models such as mesh and hierarchy.
2. Easy to use: The general SQL language makes it very convenient to operate relational databases.
3 .Easy to maintain: rich integrity (entity integrity, referential integrity and user-defined integrity) greatly reduces the probability of data redundancy and data inconsistency

(3). Relational database bottleneck

  • 1. High concurrent read and write requirements
    The user concurrency of the website is very high, often reaching tens of thousands of read and write requests per second. For traditional relational databases, hard disk I/O is a big bottleneck

  • 2. Efficient reading and writing of massive data The amount of data
    generated by the website every day is huge. For relational databases, querying in a table containing massive data is very inefficient.

  • 3. High scalability and availability.
    Among the web-based structures, the database is the most difficult to scale horizontally. When the number of users and visits of an application system is increasing day by day, the database cannot be as simple as web server and app server. Expand performance and load capacity by adding more hardware and service nodes. For many websites that need to provide 24-hour uninterrupted service, it is very painful to upgrade and expand the database system, which often requires downtime for maintenance and data migration.
    For websites, many features of relational databases are no longer needed:

  • 4. Transaction Consistency
    Relational databases have a lot of overhead in maintaining the consistency of things, but now many web2.0 systems have low consistency in reading and writing things.

  • 5. Read and write real-time performance
    For relational databases, you can read the data immediately after inserting a piece of data, but for many web applications, such high real-time performance is not required, such as sending a message After that, it is completely acceptable to see this dynamic after a few seconds or even ten seconds later.

  • 6. Complex SQL, especially the multi-table associated query.
    Any web system with large data volume is very taboo against the associated query of multiple large tables, as well as complex SQL report queries of complex data analysis types, especially SNS-type websites. The demand and product class perspective avoids this situation. Often it is more only the primary key query of a single table, and the simple conditional paging query of a single table. The function of SQL is greatly weakened
    in the relational database. The main reason for poor performance is the multi-table associated query, and Complex SQL report query of complex data analysis type. In order to ensure the ACID characteristics of the database, we must try our best to design according to the required paradigm. The tables in the relational database store a formatted data structure. The composition of each tuple field is the same. Even if not every tuple requires all fields, the database will allocate all fields for each tuple. This structure can facilitate operations such as linking between slogan tables. But from another perspective, it is also a factor in the performance bottleneck of relational databases.

2. Non-relational database

1. Instance -> database -> collection -> key-value (key-value)
2. Non-relational databases do not need to manually build databases and collections (tables).
3. NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is the general term for non-relational databases.
4. Except for the mainstream relational databases, all databases are considered non-relational.
5. The mainstream NoSQL databases include Redis, MongBD, Hbase, CouhDB, etc.

(1) The background of non-relational databases

It can be used to deal with the three high problems of Web2.0 pure dynamic website type.
(1) High performance-high concurrent read and write requirements for the database
(2) Huge Storage-high-efficiency storage and access requirements for massive data
(3) High Scalability && High Availability-high scalability and high availability requirements for the database

Both relational databases and non-relational databases have their own characteristics and application scenarios. The close combination of the two will bring new ideas to the development of Web2.0 databases. Let relational databases focus on relationships, and non-relational databases focus on storage. For example, in a MySQL database environment where read and write are separated, frequently accessed data can be stored in a non-relational database to improve access speed.

(2)NoSQL

  • The term NoSQL was first proposed by Carlo Strozzi in 1998, referring to a lightweight, open source relational database he developed without SQL functions. This definition is very different from our current definition of NoSQL. It is true to its name, referring to a "no SQL" database. However, the development of NoSQL has gradually deviated from the original intention. What we want is not "no sql", but "no relational", which is what we often call non-relational databases now.
  • In early 2009, Johan Oskarsson held a discussion on open source distributed databases. Eric Evans once again proposed the term NoSQL in this discussion, which is used to refer to those non-relational, distributed, and generally not guaranteed to follow ACID principle of data storage system. Eric Evans uses the word NoSQL not because of the literal meaning of "no SQL". He just thinks that many classic relational databases are called "**SQL", so in order to express their positioning with these relational databases The difference is that the term "NoSQL" is used.
  • Note: The database transaction must have ACID characteristics, ACID is Atomic atomicity, Consistency consistency, Isolation isolation, Durability durability.
  • A non-relational database proposes another concept. For example, it is stored in key-value pairs, and the structure is not fixed. Each tuple can have different fields, and each tuple can add some key-value pairs as needed. It will not be limited to a fixed structure, and some time and space overhead can be reduced. In this way, users can add the fields they need according to their needs. In this way, in order to obtain different user information, there is no need to perform related queries on multiple tables like in a relational database. Only need to retrieve the corresponding value according to the id to complete the query. However, non-relational databases have very few constraints, so they cannot provide queries on field attribute values ​​like where provided by SQL. And it is difficult to reflect the integrity of the design. It is only suitable for storing some relatively simple data. For data that requires more complex queries, SQL databases are obviously more suitable.

(3) Classification of non-relational databases

  • Due to the natural diversity of non-relational databases and their short time of appearance, there are several databases that can dominate the country without relational databases. There are many non-relational databases, and most of them are open source.
  • In fact, most of these databases are relatively simple to implement. Except for some commonalities, most of them are for specific application requirements. Therefore, for this type of application, they have extremely high performance. According to different structural methods and application scenarios, it is mainly divided into the following categories:
  • 1. Key-value database for high-performance concurrent reading and writing:
    The main feature of key-value database, even with extremely high concurrent reading and writing performance, Redis, Tokyo Cabinet, Flare are representatives of this type
  • 2. Document-oriented database for mass data access:
    The characteristic of this type of database is that it can quickly query data in the mass data. Typical representatives are MongoDB and CouchDB
  • 3. Scalability-oriented distributed database:
    The problem that this kind of database wants to solve is that the traditional database has scalability defects, and this kind of database can adapt to the increase in data volume and the change of data structure

3. The difference between relational database and non-relational database

1. Different data storage methods The main difference
between relational and non-relational databases is the data storage method. Relational data is naturally in table format, so it is stored in the rows and columns of the data table. Data tables can be stored in association with each other, and it is easy to extract data.
In contrast, non-relational data is not suitable for storage in the rows and columns of the data table, but is grouped together in large chunks. Non-relational data is usually stored in data sets, like documents, key-value pairs, or graph structures. Your data and its characteristics are the primary factors influencing the choice of data storage and retrieval methods.

2. Different expansion methods The
biggest difference between SQL and NoSQL databases may be in the expansion method. Of course, it must be expanded to support the increasing demand.
To support more concurrency, the SQL database is scaled vertically, that is to say, to increase the processing power and use a faster computer, so that the same data set can be processed faster. Because the data is stored in relational tables, the performance bottleneck of the operation may involve many tables, all of which need to be serviced by improving computer performance. Although the SQL database has a lot of room for expansion, it will definitely reach the upper limit of vertical expansion in the end.
The NoSQL database is scaled horizontally. Because non-relational data storage is naturally distributed, the expansion of NoSQL databases can share the load by adding more ordinary database servers (nodes) to the resource pool.

3. Different support for transactionality
If data operations require high transactionality or complex data queries need to control the execution plan, then traditional SQL databases are your best choice in terms of performance and stability. SQL database supports fine-grained control of transaction atomicity and is easy to roll back transactions.
Although NoSQL databases can also use transaction operations, they cannot be compared with relational databases in terms of stability, so their real shining value is in the scalability of operations and the processing of large amounts of data.

Two. Redis

Concept: Redis is an open source NoSQL database written in C language.
Redis runs on memory and supports persistence. It uses a key-value (key-value pair) storage form, which is an indispensable part of the current distributed architecture.

The Redis server program is a single-process model, that is, multiple Redis processes can be started at the same time on a server, and the actual processing speed of Redis is completely dependent on the execution efficiency of the main process. If only one Redis process is running on the server, when multiple clients access at the same time, the processing capacity of the server will be reduced to a certain extent; if multiple Redis processes are opened on the same server, Redis is improving the concurrent processing capacity At the same time, it will put a lot of pressure on the server's CPU. That is: in the actual production environment, it is necessary to decide how many Redis processes to start according to actual needs. If you have higher requirements for high concurrency, you may consider opening multiple processes on the same server. If CPU resources are tight, a single process can be used.

1. The difference between Redis and Memcached

  • Memcached is multi-threaded, while Redis uses single-threaded. (Personally think-Memcached is due to Redis in the read and write processing speed)
  • Memcached uses a pre-allocated memory pool, and Redis uses an on-site memory application method to store data, and virtual memory can be configured.
  • Redis can achieve persistence (that is, redis needs to frequently synchronize the data in the memory to the hard disk to ensure persistence), master-slave replication, and realize failure recovery.
  • Memcached is just a simple key and value, but Redis supports more data types. Including string (string), list (linked list), set (collection), zset (sorted set-ordered set) and hash (hash type).
/ Memcached Redis
Types of Key-value database Key-value database
Expiration policy stand by stand by
type of data Single data type Five data types
Master-slave replication not support stand by
Persistence support not support stand by
Virtual Memory not support stand by
Storage value capacity Max 1M Maximum 512M
Memory allocation Pre-allocate memory pool to manage memory, which can save memory allocation time Temporary application for space, section can cause debris
Support is off Need secondary development Redis naturally supports the cluster function, which can realize active replication and separation of read and write. The official also provides a sentinel cluster management tool, which can realize master-slave service monitoring and automatic failover. Duyu client is transparent, without program changes or manual intervention.
Network model Non-blocking IO reuse model Non-blocking IO multiplexing model, providing some sorting and aggregation functions other than non-KV storage. When performing these functions, complex CPU calculations will block the entire IO scheduling
Horizontal expansion support No No
Multithreading Support multi-threading, CPU utilization is better than Redis Support single thread
Supported data structure 纯Kev-value Hash, list, set, ordered set

2. Advantages of Redis

  • (1) Very high data read and write speed: the data read speed can reach up to 110,000 times/s, and the data write speed can reach up to 81,000 times/s.
  • (2) Support rich data types: support key-value, Strings, Lists, Hashes, Sets and Ordered Sets and other data type operations.
  • (3) Support data persistence: the data in the memory can be saved in the disk, and it can be loaded again for use when restarting.
  • (4) Atomicity: All Redis operations are atomic.
  • (5) Support data backup: namely data backup in master-salve mode.
  • Redis is a memory-based database, and caching is one of its most commonly used scenarios. In addition, the common application scenarios of Redis also include the operation of obtaining the latest N data, ranking applications, counter applications, storage relationships, real-time analysis systems, and log records.
  • (6) Living completely in the memory, the data is read and written to the memory in real time, and it flashes back to the file regularly. Single threading avoids unnecessary context switching and race conditions
  • (7) Disaster recovery-data cannot be recovered after memcache is down; redis data can be recovered through aof after loss;-(
    8) virtual memory-Redis can exchange some values ​​that have not been used for a long time when the physical memory is used up Disk

3. Redis supports two persistence methods

  • (1) Snapshotting (snapshot) is also the default method. (Make a backup of the data and store the data to a file)

  • (2) Append-only file (abbreviated aof) method

  • Snapshot is the default persistence method. This method writes the data in the memory to a binary file as a snapshot. The default file name is dump.rdb. The snapshot persistence method can be automatically made through configuration settings. We can configure redis to automatically take a snapshot if more than m keys are modified within n seconds.

  • Aof method: Since the snapshot method is done once at a certain interval, if redis is accidentally down, all the changes after the last snapshot will be lost. Aof has better persistence than the snapshot method, because when using AOF, redis will append every write command received to the file through the write function, and when redis restarts, it will be saved by re-executing the file. Write commands to rebuild the contents of the entire database in memory.

4. Redis architecture

Architecture Explanation
File Event Process file events, accept command requests (read events) from them, and return the execution results of the commands to the client (write events)
Time Event Time events (update statistics, clean up expired data, synchronization of affiliated nodes, regular persistence, etc.)
AOF Data persistence of command log
RDB Actual data persistence
Lua Environment The running environment of the Lua script. In order to make the Lua environment meet the requirements of the Redis script function, Redis has made a series of modifications to the Lua environment, including adding function libraries, replacing random functions, protecting global variables, etc.
Command table When executing a command, find the implementation function of the corresponding command according to the character
Share Objects Two categories, one is LSO-Local Share Object (local shared object) is actually similar to cookie, and the other RSO-Remote Share Object (remote share object) is more similar to the Application object in JSP

Three. Redis installation and deployment

systemctl stop firewalld
setenforce 0

yum install -y gcc gcc-c++ make

tar zxvf redis-5.0.7.tar.gz -C /opt/

cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
#由于Redis源码包中直接提供了 Makefile 文件,所以在解压完软件包后,不用先执行 ./configure 进行配置,可直接执行 make 与 make install 命令进行安装。

#执行软件包提供的 install_server.sh 脚本文件设置 Redis 服务所需要的相关配置文件
cd /opt/redis-5.0.7/utils
./install_server.sh
......					#一直回车
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server  	#需要手动修改为 /usr/local/redis/bin/redis-server ,注意要一次性正确输入
----------------------------------------------------------------------------------------------------------
Selected config:
Port           : 6379								#默认侦听端口为6379
Config file    : /etc/redis/6379.conf				#配置文件路径
Log file       : /var/log/redis_6379.log			#日志文件路径
Data dir       : /var/lib/redis/6379				#数据文件路径
Executable     : /usr/local/redis/bin/redis-server	#可执行文件路径
Cli Executable : /usr/local/bin/redis-cli			#客户端命令工具
----------------------------------------------------------------------------------------------------------

#把redis的可执行程序文件放入路径环境变量的目录中便于系统识别
ln -s /usr/local/redis/bin/* /usr/local/bin/

#当 install_server.sh 脚本运行完毕,Redis 服务就已经启动,默认侦听端口为 6379
netstat -natp | grep redis

#Redis 服务控制
/etc/init.d/redis_6379 stop				#停止
/etc/init.d/redis_6379 start			#启动
/etc/init.d/redis_6379 restart			#重启
/etc/init.d/redis_6379 status			#状态

#修改配置 /etc/redis/6379.conf 参数
vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.80.10				#70行,添加 监听的主机地址
port 6379									#93行,Redis默认的监听端口
daemonize yes								#137行,启用守护进程
pidfile /var/run/redis_6379.pid				#159行,指定 PID 文件
loglevel notice								#167行,日志级别
logfile /var/log/redis_6379.log				#172行,指定日志文件


/etc/init.d/redis_6379 restart

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/LI_MINGXUAN/article/details/113988060