Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of persistent configuration in redis

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of persistent configuration in redis, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

Persistent configuration of redis (Picture and text introduction) A brief introduction of redis

Redis is an open source, highly available, non-relational database (NoSQL, not just a database)-type key-value pair (key-value) database written in c.

Different from the traditional database, the data of redis is stored in memory, so the read and write performance is not generally high, up to 100000 operations per second, so it is widely used in the cache direction, such as session sharing with tomcat in the website architecture, database caching and so on.

Advantages and disadvantages of redis

Advantages

The speed of reading and writing is fast, the speed of reading and writing can reach 110000 times per second, and the number of writes can reach 81000 times per second. The code is elegant, and it is single-threaded, so it has high execution efficiency and high speed.

Support for multiple data structures, string (also the most commonly used), hash, list, SET, ZSET)

Rich functions, such as natural counter, key expiration function, message queue, etc.

Support many client languages and support php,java,python

Support for data persistence

Comes with a variety of highly available architectures, such as master-slave replication, Sentinel, and highly available clusters

Shortcoming

It is precisely because the data is stored in memory, so there are strict requirements for server performance. According to the volume of business, we choose how much memory to buy.

It is difficult to achieve online expansion, so you need to be careful when buying for the first time.

Persistence is mentioned above. What is persistence?

Persistence is to support the data in memory to be written to disk to prevent all data in memory from being lost when the server goes down.

The way to achieve persistence

Support two formats to persist data AOF, RDB, and the mixed use of AOF&RDB

Note: when this is enabled, AOF is preferred for redis data recovery, but RDB is the default persistence method.

AOF persistence: every command executed by redis is recorded in a separately specified log file, and the data in the log file is recovered when the data is restarted or when the data is to be recovered

RDB: just like taking a snapshot, the period of the snapshot is defined according to the save parameters defined in the configuration file, and then saved to the hard disk, resulting in a dump.rdb file.

Comparison of AOF and RDB:

1AOF files are updated more frequently than rdb, so aof restore is preferred.

Aof is more secure than rdb

The performance of rdb is better than that of aof. When the amount of data is large, the recovery speed of log is slower than that of rdb.

During continuous reading and writing, if rdb takes a snapshot, the data will be delayed and the recovered data will be incomplete.

Redis data structure data types store values commonly used operation commands Application scenarios string string SET (create), GET (View), DEL (Delete), MSET (batch create), MGET (batch View for caching, Expiration time of key-value pair, redis for session session, Expiration deletion, caching user Information, caching Mysql partial data, Mall coupons Expiration time, etc. List list RPUSH (create Add to the right if present), LPUSH (add to the left, LRABGE+ range (view range value), RPOP (delete the last one on the right), LPOP (delete the last one on the left) are generally used in combination with zset, and are mainly used in rankings, hotspots / clicks rankings, live room rankings, and other hash hash HMSET + objects (create the key value of an object, for an object) HGET+ object (a parameter of view object) generally key is ID or unique identity, value is the corresponding details, such as: commodity information, personal information, news, etc. SET unordered set SADD (create set), SMEMBERS (view set), SREM (delete set value), SDIFF set 1 set 2 (difference set), SINTER set 1 set 2 (join set), SUNION set 1 set 2 (join set) to intersect, join set, merge set Applied to social networks, such as common interests, common friends, etc. ZSET ordered set contract SET can be combined with list to complete the ranking to achieve the persistent deployment of redis redis

1. Create a data directory

Mkdir-p / redis/softmkdir-p / opt/redis_cluster/redis_6379/ {conf,logs,pid}

Explain why you create your own conf,logs,pid directory instead of having it initialize the automatic generation

We are in order to start multiple redis processes on a single host to achieve a later redis cluster (at least 6) (Author computer configuration does not allow me to operate wayward)

two。 Download the redis installation package

Cd / redis/softwget http://download.redis.io/releases/redis-5.0.6.tar.gz

3. Extract redis to / opt/redis_cluster/

Tar zxf redis-5.0.6.tar.gz-C / opt/redis_cluster/ln-s / opt/redis_cluster/redis-5.0.6 / opt/redis_cluster/redis # make soft connection easy to manage

4. Change the directory to install redis

Cd / opt/redis_cluster/redis make & & make install

5. Write your own configuration file / opt/redis_cluster/redis_6379/conf/6379.conf

Add some important content

Add: bind 127.0.0.1 192.168.10.1port 6379daemonize yes # start the daemon process pidfile / opt/redis_cluster/redis_6379/pid/redis_6379.pidlogfile / opt/redis_cluster/redis_6379/logs/redis_6379.logdatabases 16dbfilename redis.rdb # RDB persistence file dir / opt/redis_cluster/redis_6379 # RDB location

6. Start the current redis service

Redis-server / opt/redis_cluster/redis_6379/conf/6379.conf

[root@redis-master ~] # netstat-anpt | grep 6379tcp 00 192.168.10.1 anpt 6379 0.0.0.0 LISTEN 49206/redis-server tcp 00 127.0.0.1 anpt 6379 0.0.015 * LISTEN 49206/redis-server tcp 00 127.0.0.1 127.0.0.1 ESTABLISHED 49206/redis-server 6379 TIME_WAIT-tcp 0 192.168.10.1 ESTABLISHED 49206/redis-server configuration for persistence RDB configuration

Modify the configuration file to add a save entry

Vim / opt/redis_cluster/redis_6379/conf/6379.conf add: save 9001 # after 900s (15 minutes), if at least one key changes, then the dump memory snapshot. Save 30010 # after 300 seconds (5 minutes), if at least 10 key changes, the dump memory snapshot. Save 60500 # AOF configuration for dump memory snapshot persistence after 60 seconds (1 minute) if at least 500 key changes

Both for modifying the configuration file

Add appendonly to enable persistence

Vim / opt/redis_cluster/redis_6379/conf/6379.conf add: appendonly yes # enable AOF persistence appendfilename "redis.aof" # specify AOF file name appendfsync everysec # synchronize restart redis once a second to make it effective, verify persistence redis-cli shutdownredis-server / opt/redis_cluster/redis_6379/conf/6379.conf

Add key-value pairs to the redis database

#! / bin/bashfor I in {1.. 500} do redis-cli set kryptoni v_$idone

If the database is closed at this time, the things in memory will certainly be lost normally, but they will certainly not be lost now, and there will be persistent files.

[root@redis-master ~] # redis-cli shutdown [root@redis-master ~] # redis-server / opt/redis_cluster/redis_6379/conf/6379.conf [root@redis-master ~] # [root@redis-master ~] # redis-cli127.0.0.1:6379 > keys karma 5001) "vault 500" 127.0.0.1 root@redis-master 6379 >

Redis master-slave replication

Why do redis master-slave replication?

In order to solve single point of failure, copy data to one or more replica servers (slave servers), achieve redundancy, fault recovery and load balancing.

Set up another server and install redis

1. For simplicity, let's directly copy the previous master

[root@redis-master ~] # scp-rp / opt/redis_cluster/ root@192.168.10.8:/opt

two。 Install redis directly on make install, no need to compile again, which has been done in master

Modify the configuration file

Cd / opt/redis_cluster/redisvim / opt/redis_cluster/redis_6379/conf/6379.conf modification: bind 127.0.0.1 192.168.10.8 slaveof 192.168.10.1 6379 # add master ip port save exit

3. Start the service

Redis-server / opt/redis_cluster/redis_6379/conf/6379.conf

Create a new key value on the master server and test the slave server for automatic synchronization

Note:

During the synchronization process of the slave server, only the data of the master database can be copied, and the modified data cannot be added manually.

If you do not want to modify data from the server, you need to disconnect the synchronization:

[root@redis-slave ~] # redis-cli slaveof no one

Just prompt OK.

If the master is down, you can manually disconnect the synchronization from the server. At this time, he is an independent individual, and the other users can switch from the server to themselves.

Thank you for reading this article carefully. I hope the article "sample Analysis of persistence configuration in redis" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report