This article brings you relevant knowledge about redis atomic operations. In order to ensure the correctness of concurrent access, Redis provides two methods, namely locking and atomic operations. I hope it will be helpful to everyone.
When we use Redis, we will inevitably encounter concurrent access problems, for example, if multiple users place orders at the same time , the product inventory cached in Redis will be updated concurrently. Once there are concurrent write operations, the data will be modified. If we do not control concurrent write requests, the data may be corrected and affect the normal use of the business (for example, inventory data errors lead to abnormal order placement).
In order to ensure the correctness of concurrent access, Redis provides two methods, namely locking and atomic operations.
Locking is a common method. Before reading data, the client needs to obtain the lock first, otherwise the operation cannot be performed. When a client obtains a lock, it will hold the lock until the client completes the data update and then releases the lock.
It seems like a good solution, but there are actually two problems here: one is that if there are too many locking operations, it will reduce the concurrent access performance of the system; the second is that the Redis client needs When locking, you need to use distributed locks, and the implementation of distributed locks is complicated and requires an additional storage system to provide locking and unlocking operations. I will introduce it to you in the next lesson.
Atomic operations are another method of providing concurrent access control. Atomic operations refer to operations that maintain atomicity during execution, and do not require additional locks when executing atomic operations, thus achieving lock-free operations. In this way, concurrency control can be guaranteed and the impact on system concurrency performance can be reduced.
What needs to be controlled during concurrent access?
What we call concurrent access control refers to controlling the process of multiple clients accessing and operating the same data to ensure that operations sent by any client are mutually exclusive when executed on the Redis instance. For example, while client A's access operation is being executed, client B's operation cannot be executed and must wait until client A's operation is completed.
The operations corresponding to concurrent access control are mainly data modification operations. When the client needs to modify data, the basic process is divided into two steps:
We call this process the "Read-Modify-Write" operation (Read-Modify-Write, referred to as RMW operation). When multiple clients perform RMW operations on the same data, we need to allow the code involved in the RMW operations to be executed atomically. The RMW operation code that accesses the same data is called critical section code.
However, when multiple clients execute critical section code concurrently, there will be some potential problems. Next, I will use an example of multiple clients updating product inventory to explain.
Let’s take a look at the critical section code first. Assume that the client wants to deduct 1 from the product inventory. The pseudo code is as follows:
current = GET(id) current-- SET(id, current)
As you can see, the client will first read the current inventory value of the product from Redis based on the product id. (Corresponding to Read), then, the client decreases the inventory value by 1 (corresponding to Modify), and then writes the inventory value back to Redis (corresponding to Write). When multiple clients execute this code, this is a critical section code.
If we have no control mechanism over the execution of critical section code, data update errors will occur. In the example just now, assuming there are two clients A and B, executing the critical section code at the same time, an error will occur. You can look at the picture below.
It can be seen that client A reads the inventory value 10 and deducts 1 at t1. At t2, client A has not yet written the deducted inventory value 9. Return to Redis, and at this time, client B reads the inventory value of 10, which is also deducted by 1, and the inventory value recorded by B is also 9. When t3, A writes back the inventory value 9 to Redis, and when t4, B also writes back the inventory value 9.
If processed according to the correct logic, clients A and B each deduct the inventory value once, and the inventory value should be 8. Therefore, the inventory value here is obviously updated incorrectly.
The reason for this phenomenon is that the client in the critical section code involves three operations: reading data, updating data, and writing back data, and these three operations are not mutually exclusive during execution. sex, multiple clients modify based on the same initial value, rather than modify based on the value modified by the previous client.
In order to ensure the correctness of concurrent modification of data, we can use locks to turn parallel operations into serial operations, and serial operations are mutually exclusive. After a client holds the lock, other clients can only wait until the lock is released before they can take the lock and make modifications.
The following pseudocode shows the use of locks to control the execution of critical section code, you can take a look.
LOCK() current = GET(id) current-- SET(id, current) UNLOCK()
虽然加锁保证了互斥性,但是加锁也会导致系统并发性能降低。
如下图所示,当客户端 A 加锁执行操作时,客户端 B、C 就需要等待。A 释放锁后,假设 B 拿到锁,那么 C 还需要继续等待,所以,t1 时段内只有 A 能访问共享数据,t2 时段内只有 B 能访问共享数据,系统的并发性能当然就下降了。
和加锁类似,原子操作也能实现并发控制,但是原子操作对系统并发性能的影响较小,接下来,我们就来了解下 Redis 中的原子操作。
Redis 的两种原子操作方法
为了实现并发控制要求的临界区代码互斥执行,Redis 的原子操作采用了两种方法:
我们先来看下 Redis 本身的单命令操作。
Redis 是使用单线程来串行处理客户端的请求操作命令的,所以,当 Redis 执行某个命令操作时,其他命令是无法执行的,这相当于命令操作是互斥执行的。当然,Redis 的快照生成、AOF 重写这些操作,可以使用后台线程或者是子进程执行,也就是和主线程的操作并行执行。不过,这些操作只是读取数据,不会修改数据,所以,我们并不需要对它们做并发控制。
你可能也注意到了,虽然 Redis 的单个命令操作可以原子性地执行,但是在实际应用中,数据修改时可能包含多个操作,至少包括读数据、数据增减、写回数据三个操作,这显然就不是单个命令操作了,那该怎么办呢?
别担心,Redis 提供了 INCR/DECR 命令,把这三个操作转变为一个原子操作了。INCR/DECR 命令可以对数据进行增值 / 减值操作,而且它们本身就是单个命令操作,Redis 在执行它们时,本身就具有互斥性。
比如说,在刚才的库存扣减例子中,客户端可以使用下面的代码,直接完成对商品 id 的库存值减 1 操作。即使有多个客户端执行下面的代码,也不用担心出现库存值扣减错误的问题。
DECR id
所以,如果我们执行的 RMW 操作是对数据进行增减值的话,Redis 提供的原子操作 INCR 和 DECR 可以直接帮助我们进行并发控制。
但是,如果我们要执行的操作不是简单地增减数据,而是有更加复杂的判断逻辑或者是其他操作,那么,Redis 的单命令操作已经无法保证多个操作的互斥执行了。所以,这个时候,我们需要使用第二个方法,也就是 Lua 脚本。
Redis 会把整个 Lua 脚本作为一个整体执行,在执行的过程中不会被其他命令打断,从而保证了 Lua 脚本中操作的原子性。如果我们有多个操作要执行,但是又无法用 INCR/DECR 这种命令操作来实现,就可以把这些要执行的操作编写到一个 Lua 脚本中。
然后,我们可以使用 Redis 的 EVAL 命令来执行脚本。这样一来,这些操作在执行时就具有了互斥性。
再举个例子,具体解释下 Lua 的使用。
当一个业务应用的访问用户增加时,我们有时需要限制某个客户端在一定时间范围内的访问次数,比如爆款商品的购买限流、社交网络中的每分钟点赞次数限制等。
那该怎么限制呢?我们可以把客户端 IP 作为 key,把客户端的访问次数作为 value,保存到 Redis 中。客户端每访问一次后,我们就用 INCR 增加访问次数。
不过,在这种场景下,客户端限流其实同时包含了对访问次数和时间范围的限制,例如每分钟的访问次数不能超过 20。所以,我们可以在客户端第一次访问时,给对应键值对设置过期时间,例如设置为 60s 后过期。同时,在客户端每次访问时,我们读取客户端当前的访问次数,如果次数超过阈值,就报错,限制客户端再次访问。你可以看下下面的这段代码,它实现了对客户端每分钟访问次数不超过 20 次的限制。
//获取ip对应的访问次数 current = GET(ip) //如果超过访问次数超过20次,则报错 IF current != NULL AND current > 20 THEN ERROR "exceed 20 accesses per second" ELSE //如果访问次数不足20次,增加一次访问计数 value = INCR(ip) //如果是第一次访问,将键值对的过期时间设置为60s后 IF value == 1 THEN EXPIRE(ip,60) END //执行其他操作 DO THINGS END
可以看到,在这个例子中,我们已经使用了 INCR 来原子性地增加计数。但是,客户端限流的逻辑不只有计数,还包括访问次数判断和过期时间设置。
对于这些操作,我们同样需要保证它们的原子性。否则,如果客户端使用多线程访问,访问次数初始值为 0,第一个线程执行了 INCR(ip) 操作后,第二个线程紧接着也执行了 INCR(ip),此时,ip 对应的访问次数就被增加到了 2,我们就无法再对这个 ip 设置过期时间了。这样就会导致,这个 ip 对应的客户端访问次数达到 20 次之后,就无法再进行访问了。即使过了 60s,也不能再继续访问,显然不符合业务要求。
所以,这个例子中的操作无法用 Redis 单个命令来实现,此时,我们就可以使用 Lua 脚本来保证并发控制。我们可以把访问次数加 1、判断访问次数是否为 1,以及设置过期时间这三个操作写入一个 Lua 脚本,如下所示:
local current current = redis.call("incr",KEYS[1]) if tonumber(current) == 1 then redis.call("expire",KEYS[1],60) end
假设我们编写的脚本名称为 lua.script,我们接着就可以使用 Redis 客户端,带上 eval 选项,来执行该脚本。脚本所需的参数将通过以下命令中的 keys 和 args 进行传递。
redis-cli --eval lua.script keys , args
这样一来,访问次数加 1、判断访问次数是否为 1,以及设置过期时间这三个操作就可以原子性地执行了。即使客户端有多个线程同时执行这个脚本,Redis 也会依次串行执行脚本代码,避免了并发操作带来的数据错误。
推荐学习:《Redis视频教程》、《2022最新redis面试题大全及答案》
The above is the detailed content of Understand redis atomic operations in ten minutes. For more information, please follow other related articles on the PHP Chinese website!