In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Xiaobian to share with you the example analysis of Redis advanced application, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
Redis advanced utility features are divided into 6 parts:
1. Safety
A password is required for any other operation after setting up the client connection
Modify the redis.conf configuration file, the requirepass password command is used to set the password
Restart redis service after modifying configuration file
After restart, found in the command line to perform operations, there will be the following prompts. You need to use auth password to authorize it.
127.0.0.1:6379> keys *(error) NOAUTH Authentication required.127.0.0.1:6379> auth redis123OK127.0.0.1:6379> keys *(empty list or set)
If you do not want to enter a password on the command line to complete authorization, you can enter the password when logging in to the client
[root@localhost init.d]# /usr/local/redis/bin/redis-cli -a redis123127.0.0.1:6379> keys *(empty list or set)
2. Master-slave copy
Redis master-slave replication is easy to configure and use. Master-slave replication allows multiple slave servers to have the same database copy as the master server
How master-slave replication works:
1. After slave establishes connection with master, send sync synchronization command
The master starts a background process that saves the database snapshot to a file, and the master process starts collecting new write commands and caching them.
3. After the background is saved, send this file to slave.
Save this file to your hard drive.
Features of master-slave replication:
A Master can have multiple slaves.
b. Multiple slaves can be connected to the same master, and can also be connected to other slaves (this feature is to prevent the slave from being unable to synchronize after the master fails. If the slave is also connected to other slaves, then after the master hangs, the slave will become the master and take over the service)
c. Master-slave replication does not block master. When synchronizing data, master can continue to process client requests.
d. Improve system scalability
Configuration of master-slave replication:
Add the following configuration to the slava configuration file:
slaveof 192.168.1.1 6379 #Specify ip and port of master
masterauth PASSWORD #This is master's password
Note: Notes for master-slave configuration:
The bind 127.0.0.1 configuration in the master and slave configuration files is changed to the ip address of the eth0 network card, otherwise the port of redis is listening on 127.0.0.1 by default. This way the slave to master port is blocked.
After changing bind's IP address, enter the client using the command:
$redis_home/bin/redis-cli -h 172.16.206.140
That is, use the-h parameter to specify the IP address of the eth0 NIC. The default is 127.0.0.1.
3. Transaction processing
Redis transaction processing is relatively simple at present, Redis can only ensure that the commands in the things initiated by a client can be executed continuously, and no commands of other clients will be inserted in the middle. When a client issues a multi command in a connection, the connection enters a transaction context. Subsequent commands in the connection are not executed immediately, but are placed in a queue first. When the exec command is executed, redis executes the commands in the queue sequentially.
For example:
172.16.206.142:6379> set age 27OK172.16.206.142:6379> 172.16.206.142:6379> get age"27"172.16.206.142:6379> multiOK172.16.206.142:6379> set age 37QUEUED172.16.206.142:6379> set age 47\QUEUED172.16.206.142:6379> set age 47QUEUED172.16.206.142:6379> exec1) OK2) OK3) OK172.16.206.142:6379> get age"47"
cancel: cancel a transaction
172.16.206.142:6379> get age"47"172.16.206.142:6379> multiOK172.16.206.142:6379> set age 100QUEUED172.16.206.142:6379> set age 200QUEUED172.16.206.142:6379> discardOK172.16.206.142:6379> get age"47"
Transaction Rollback:
Redis does not support transaction rollback, when there are two transactions in the queue, one completed and the other not completed, then the incomplete transaction does not affect the entire transaction.
172.16.206.142:6379> set name zengOK172.16.206.142:6379> get age"47"172.16.206.142:6379> multiOK172.16.206.142:6379> incr ageQUEUED172.16.206.142:6379> incr nameQUEUED172.16.206.142:6379> exec1) (integer) 482) (error) ERR value is not an integer or out of range172.16.206.142:6379> get name"zeng"172.16.206.142:6379> 172.16.206.142:6379> get age"48"
Optimistic lock:
The watch command monitors the given key, and if the key has changed since watch was called when exec is executed, the entire transaction fails. You can also call watch to monitor multiple keys multiple times, so that you can add an optimistic lock to the specified key.
4. Persistence mechanism
Redis is an in-memory database that supports persistence, which means that redis needs to synchronize data from memory to hard disk frequently to ensure persistence. Redis supports two persistence methods:
Snapshotting is also the default.
Append only file (aof)
Snapshotting methods:
Snapshot is the default persistence mode, which is to write the data in memory to a binary file in the form of snapshot. The default file name is dump.rdb. You can configure the automatic persistence mode. We can configure redis to automatically take snapshots if more than m keys are modified in n seconds
save 900 1 #900 seconds if more than 1 key is modified, initiate snapshot save
save 300 10 #300 seconds if more than 10 keys are modified, initiate snapshot save
save 60 10000
aof way:
Because snapshots are taken at intervals, if redis accidentally goes down, all changes made since the last snapshot are lost.
aof has better persistence than snapshot because redis appends every write command received to the file through the write function when aof is used, and reconstructs the entire database content in memory by reexecuting the write command saved in the file when redis restarts
appendonly yes //enable aof persistence mode
#appendfsync always //Write to disk immediately after receiving a write command, the slowest, but guaranteed to be completely persistent
appendfsync everysec //writes to disk once per second, a good compromise between performance and persistence
#appendfsync no //completely dependent on os, the best performance, persistence is not guaranteed
5. Publishing subscription messages
Pub/sub is a message communication mode, the main purpose of which is to decouple message publishers and message subscribers. Redis, as a pub/sub server, plays a message routing function between subscribers and publishers. Subscribers can subscribe to redis server for message types of interest through the subscribe and psubscribe commands, which redis calls channels. When a publisher sends a particular type of information to redis server via the publish command, all clients subscribing to that type of information receive the message
Testing:
Redis server opens three session windows and enters the redis client command line.
Terminal 1 subscribes to TV1 and TV2 channels
> subscribe tv1 tv2Reading messages... (press Ctrl-C to quit)1) "subscribe"2) "tv1"3) (integer) 11) "subscribe"2) "tv2"3) (integer) 2
Terminal 2 subscribes to tv channel 2
> subscribe tv1Reading messages... (press Ctrl-C to quit)1) "subscribe"2) "tv1"3) (integer) 1
Terminal three-way tv channel 1 release message hello
>publish tv1 hello(integer) 2
Results:
Terminals 1 and 2 both received messages:
1) "message"2) "tv1"3) "hello"
6. Use of virtual memory
Redis virtual memory is not the same thing as operating system virtual memory, but the idea and purpose are the same. It is to temporarily swap infrequently accessed data from memory to disk, thus freeing up valuable memory space for other data that needs to be accessed. Especially for an in-memory database like redis, memory is always running out. In addition to the ability to split data into multiple redis servers. Another way to increase database capacity is to use virtual memory to swap infrequently accessed data onto disk.
The configuration is as follows:
vm-enabled yes #Enable vm
really-use-vm yes #Make sure to use virtual memory, this configuration needs to be added manually
vm-swap-file /tmp/redis.swap #The path to the file saved by the value exchanged
vm-max-memory 100000 #Maximum memory used by redis online
vm-page-size 32 #Size of each page 32 bytes
vm-pages 134217728 #How many pages can be used at most
vm-max-threads 4 #Number of worker threads used to perform value object swapping
The above is all the content of this article "Redis Advanced Application Sample Analysis", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.