“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.”

The slow query

The slow query log is the execution time of each command. When the command execution time exceeds the threshold, the system records the command

Redis command execution process

  1. Send the command

  2. The command line

  3. Command execution

  4. Returns the result

Command execution is the time of slow query statistics

Query two configuration parameters slowly

  • Slowlog-log-slower: Preset threshold in milliseconds for slower commands that are logged if executed for longer than the threshold

  • Slowlog-max-len: sets the maximum number of slow query logs to be stored

Publish and subscribe model

Redis provides publish and subscribe functions, which can be used for message transmission. The publish and subscribe mechanism of Redis consists of three parts, publisher, subscriber and Channel.

Publish and subscribe

  • The publish command is used to send messages

  • Subscribe to a channel with the subscribe command

  • Pattern matching: subscribe multiple channels at the same time, the command is PSUBSCRIBE

Redis expiration time processing

  1. Take the initiative to deal with

Timing processing, when the expiration time is set to create a timer, when the expiration time is up to delete the operation immediately, this operation is real-time, no matter how many expired keys in this period, no matter the server running condition, will be deleted, CPU is not very friendly.

Periodic deletion, periodic deletion is to set a time interval each time period will detect whether there is an expired key, if there is a deletion,

  1. Passive process

When the expired key is accessed again, the system checks whether the key is expired or not. If the key is expired, the system deletes it and returns NIL. This method is cpu-friendly and does not occupy CPU resources for other expired keys. However, it is not memory friendly. A key that has expired is not deleted until it is operated on, which still occupies memory space.

3. Processing of expired keys by RDB and AOF

If an RDB is created by executing the save or BGSave commands, the database keys are checked and expired keys are not saved to the newly created RDB file.

When an expired key is lazily deleted or periodically deleted, the program appends a DEL command to the AOF file to explicitly record that the key has been deleted.

During an AOF rewrite, keys in the database are checked, and expired keys are not saved to the rewritten AOF file.

Memory recovery

Noeviction: Default policy, won’t delete any data, rejects all write operations and returns client error messages, Redis will only respond to read operations.

Volatitle-rlu: Deletes keys with the timeout attribute based on LRU until sufficient space is available. If there are no key objects to remove, retreat to the Noeviction policy.

Allkeys-lru: Deletes keys according to the LRU algorithm, regardless of whether the timeout attribute is set, until enough space is made.

Allkeys-random: randomly delete allkeys until enough space is left.

Volatitle-random: Deletes expired keys randomly until sufficient space is available.

Volatitle-ttl: deletes the data that will expire recently according to the TTL attribute of the key value object. If not, retreat to Noeviction strategy