[Redis] Redis advanced features and applications (slow query, Pipeline, transactions, Lua)

Table of contents

Redis slow query

Slow query configuration

Slow query operation command

Slow query suggestions

Pipeline

affairs

Redis transaction principle

Redis watch command

The difference between Pipeline and transaction

Lua

Getting Started with Lua

Install Lua

Lua basic syntax

Comment

Identifier

Key words

global variables

Data types in Lua

Functions in Lua

Lua variables

Control statements in Lua

loop control

if condition control

Lua operators

Other features of Lua

Java support for Lua

Maven

Reference Code

Lua in Redis

eval command

Command format

Command description

Example

Calling Redis commands in Lua scripts

evalsha command

redis-cli execute script

Redis and current limiting

Using Redis+Lua language to implement current limiting

Current limiting algorithm

fixed window algorithm

sliding window algorithm

Sliding window in TCP

vulnerability algorithm

token algorithm


Redis slow query

Many storage systems (such as MySQL) provide slow query logs to help development and operation and maintenance personnel locate slow operations in the system. The so-called slow query log means that the system calculates the execution time of each command before and after the command is executed. When it exceeds the preset threshold, the relevant information of this command (such as: occurrence time, time consumption, detailed information of the command) is recorded. Redis also provides similar functionality.

A command executed by the Redis client is divided into the following four parts:

image.png

1. Send command

2. Command queuing

3. Command execution

4. Return results

It should be noted that slow query only counts the time in step 3, so the absence of slow query does not mean that the client does not have timeout problems. Because there may be a network problem with the command or the command is queued in Redis, it does not mean that the command execution is slow or that it is a slow query, but it may be a network problem or the Redis service is very busy (long queue wait).

Slow query configuration

For any slow query function, two things need to be clarified: how slow is considered slow, that is, how to set the preset threshold? Where are slow query records stored?

Redis provides two ways to configure slow queries

1. Dynamic settings

The default threshold for slow queries is 10 milliseconds

Parameters: slowlog-log-slower-than is the time preset threshold. Its unit is microseconds (1 second = 1000 milliseconds = 1 000 000 microseconds). The default value is 10 000. If a "very slow" is executed A command (such as keys *), if its execution time exceeds 10,000 microseconds, which is 10 milliseconds, then it will be recorded in the slow query log.

We modify it through dynamic commands

--javascripttypescriptbashsqljsonhtmlcssccppjavarubypythongorustmarkdown

config set slowlog-log-slower-than 20000  

image.png

After using config set, if you want to persist the configuration to Redis.conf, you need to execute config rewrite

image.png

--javascripttypescriptbashsqljsonhtmlcssccppjavarubypythongorustmarkdown

config rewrite

image.png

Notice:

If slowlog-log-slower-than=0 is configured, all commands will be logged, and slowlog-log-slower-than<0 will not log any commands.

2. Configuration file settings (the service needs to be restarted after modification to take effect)

Open the Redis configuration file redis.conf, and you can see the following configuration:

Guess you like

Origin blog.csdn.net/weixin_38996079/article/details/134702779