Master-slave backup

In Redis, you can make one server replicate another by executing the SLAVEOF command or setting the SLAVEOF option. We call the replicated server master. The servers that replicate the master are called slave servers, as shown in the figure.



127.0.0.1:12345 > SLAVEOF 127.0.0.1 6379

Secondary server Primary server

Persistence and Principles (Native Persistence & Mysql Database Persistence)

Method 1: Snapshot RDB (default) Persistence Enables point-in-time snapshots of data sets to be generated at a specified interval. The following Settings will automatically save the data set once Redis meets the condition of “at least 1000 key changes within 60 seconds” :

save 60 1000
Copy the code

Method 2: Synchronize all write operation commands executed by the server to the data file AOF persistence and restore the data set by executing these commands again when the server is started. Start AOF in the configuration file

appendonly yes
Copy the code

Mode 3: Use the virtual memory

Cache invalidation policy (i.e., flushing out stale data) that is, what happens when data exceeds memory? Configure the maximum memory in the Settings file

server.maxmemory
Copy the code

Three main algorithms

  • FIFO: First In First Out. Determine how long the data has been stored, and the data farthest away from the present is eliminated first.
  • LRU: Least Recently Used Determine the most recently used data, and the most recent data is eliminated first.
  • LFU: The Least Frequently Used. The data that has been used the least over a period of time will be eliminated first.

Redis provides six maxmemory-policies:

  • Volatile – lRU: Selects the least recently used expires data from a set with an expiration date (server.db[I].expires)
  • Volatile – TTL: Selects expired data from a set (server.db[I].expires) to be discarded
  • Volatile -random: Selects any data from a set with an expiration date (server.db[I].expires) to be discarded
  • Allkeys-lru: Culls the least recently used data from the dataset (server.db[I].dict)
  • Allkeys-random: Random selection of data from a dataset (server.db[I].dict)
  • No-enviction: disables data expulsion by default

Three expiration strategies

  • Scheduled deletion: When the expiration time of a key is set, a timer is created for the key. The timer is used to delete the key when the expiration time is up
  • Lazy deletion: The key is not deleted when it expires. Each time the key is obtained from the database, the system checks whether the key is expired. If the key is expired, the system deletes it and returns NULL.
  • Periodically delete: Deletes expired keys once in a while

How to handle the outage

  • Create a cron job that backs up one RDB file to one folder every hour and one RDB file to another folder every day.
  • Make sure that snapshots are backed up with date and time information, and use the find command to delete expired snapshots each time you execute a periodic task script: for example, you can keep snapshots every hour within the last 48 hours and daily snapshots within the last month or two.
  • At least once a day, back up your RDB outside of your data center, or at least outside of the physical machine on which your Redis server is running.

Transactions and distributed locking mechanisms

A Redis transaction is a collection of commands.

At the beginning of the transaction, the Redis server sends MULTI command, then sends the commands that need to be processed in the transaction, and finally sends EXEC command to indicate the end of the transaction command.



Commands are not executed immediately; they are executed one by one only when the EXEC command is executed.

Optimistic and distributed locks The WATCH, MULTI, and EXEC commands of Redis only notify the executing client to cancel the modification if the data has been modified by other clients. They do not prevent other clients from modifying the data. Therefore, we can only call it optimistic locking.

Optimistic locks may occupy resources for a long time due to transaction execution failure, resulting in deadlocks. Distributed through mutual exclusion to prevent interference to ensure the consistency of transactions Redis mainly used to achieve distributed lock command is SETNX command (SET if Not eXists). Syntax: SETNX key value Function: Set the key value to value and return 1 if and only if the key does not exist. If the given key already exists, SETNX does nothing and returns 0.