简体   繁体   English

PHP Redis 会话锁定内部结构

[英]PHP Redis Session Locking Internals

How does phpredis session locking work internally? phpredis会话锁定如何在内部工作? Does it store a value internally and keep checking it?它是否在内部存储一个值并不断检查它? If I have a high redis.session.lock_retries will that put load on the server if it takes 10 seconds to get a lock and had to try thousands of times?如果我有一个很高的redis.session.lock_retries ,如果需要 10 秒才能获得锁定并且必须尝试数千次,这是否会给服务器带来负载?

Below are the configuration parameters.下面是配置参数。

; Should the locking be enabled? Defaults to: 0.
redis.session.locking_enabled = 1
; How long should the lock live (in seconds)? Defaults to: value of max_execution_time.
redis.session.lock_expire = 60
; How long to wait between attempts to acquire lock, in microseconds (µs)?. Defaults to: 2000
redis.session.lock_wait_time = 50000
; Maximum number of times to retry (-1 means infinite). Defaults to: 10
redis.session.lock_retries = 10

Looks like it's a hot loop: 看起来这是一个热循环:

static int lock_acquire(RedisSock *redis_sock, redis_session_lock_status *lock_status)
{
  ...
  for (i = 0; retries == -1 || i <= retries; i++) {
      set_lock_key_result = set_session_lock_key(redis_sock, cmd, cmd_len);
      ...
      /* Sleep unless we're done making attempts */
      if (retries == -1 || i < retries) {
        usleep(lock_wait_time);
      }

There is no exponential back-off or other mitigation for long-blocking requests.对于长时间阻塞的请求,没有指数回退或其他缓解措施。 If you have a high spread (eg sometimes instant lock acquisition, sometimes 1s, sometimes 10s, etc.) then I'd suggest setting the lock attempt length quite low, then writing your own hot loop that does back-off more in line with your expected wait time.如果您有一个高点差(例如有时是即时锁定获取,有时是 1s,有时是 10s 等),那么我建议将锁定尝试长度设置得相当低,然后编写您自己的热循环,使回退更符合您预计的等待时间。 If you have a pretty stable expected wait time (eg always about 10s) then just set the wait time quite high.如果您有一个相当稳定的预期等待时间(例如总是大约 10 秒),那么只需将等待时间设置得相当高。

The commit that added this is here .添加这个的提交 在这里

It seems to imply that it uses the locking recipe described in the Redis docs .这似乎暗示它使用了 Redis 文档中描述的锁定方法。

Overall this recipe relies on atomically trying to set a key if it doesn't exist (this is the lock) and failing if it already exists.总的来说,这个秘籍依赖于原子地尝试设置一个不存在的键(这是锁),如果它已经存在则失败。 This means that every attempt is a command to the Redis server which on its own is very fast but if your Redis server is hosted remotely then there is the overhead of a network round-trip这意味着每次尝试都是对 Redis 服务器的命令,该命令本身非常快,但是如果您的 Redis 服务器是远程托管的,则存在网络往返的开销

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM