我为什么要关闭或保持Redis连接打开?

我在PHP项目中使用Redis。 我使用phpredis作为客户端。 有时候,在长CLI脚本中,我会遇到PHP分段错误。</ p>

我之前经历过phpredis在连接超时时遇到问题。 由于我的Redis配置被配置为在300秒后自动关闭空闲连接,我猜这会导致分段错误。</ p>

为了能够选择是否增加连接超时或默认它 到0(表示“永不超时”),我想知道可能的优点和缺点是什么?</ p>

为什么我永远不会关闭连接?
\ 为什么我应该确保连接不会保持打开状态?</ strong> </ p>

谢谢</ p>
</ div>

展开原文

原文

I'm using Redis in a PHP project. I use phpredis as a client. Sometimes, during long CLI-scripts, I experience PHP segmentation faults.

I've experienced before that phpredis has problems when the connection times out. As my Redis config is configured to automatically close idle connections after 300 seconds, I guess that causes the segmentation fault.

In order to be able to choose whether to increase the connection timeout or default it to 0 (which means "never timeout"), I would like to know what the possible advantages and disadvantages are?

Why should I never close a connection?
Why should I make sure connections don't stay open?

Thanks

2个回答



通常,打开连接是一项昂贵的操作,因此现代最佳实践是保持打开状态。 另一方面,开放连接需要资源(来自数据库)来管理,因此保持大量空闲连接打开也可能是有问题的。 这种权衡通常通过使用连接池来解决。</ p>

这就是说,更有趣的是为什么PHP会出现段错误。 显然,超时是由长时间运行的命令(在您的情况下为CLI脚本)阻止Redis(主要是单个)引起的 从参加PHP应用程序的连接开始。) 虽然这是一个众所周知的Redis行为,但我认为PHP(没有在客户端库中重新连接的事件)不会如此悲惨地使用它。</ p>
</ div>

展开原文

原文

Generally, opening a connection is an expensive operation so modern best practices are to keep them open. On the other hand, open connections requires resources (from the database) to manage so keeping a lot of idle connections open can also be problematic. This trade off is usually resolved via the use of connection pools.

That said, what's more interesting is why does PHP segfault. The timeout is, evidently, caused by a long running command (CLI script in your case) that blocks Redis (which is mostly single threaded) from attending to the PHP app's connections. While this is a well-known Redis behavior, I would expect PHP (event without featuring reconnect at the client library) not to s**t its pants so miserably.



您的问题的答案很大程度上取决于您的应用程序中redis使用情况。 那么,您是否应该永远关闭与空闲连接超时的连接? </ p>

一般来说,否</ strong>,你应该保持默认 - 0。为什么或何时:</ p>


  • 任何类型的长期生活应用。 例如CLI脚本ot后台工作者。 为什么 - phpredis没有建立重新连接功能,所以你应该自己处理这个问题,或者不要空闲超时。</ li>
  • 每次处理请求或CLI脚本死亡时 - 所有连接都将关闭 通过php引擎。 Redis服务器关闭已关闭的客户端套接字的所有连接。 你将没有像僵尸连接或类似的问题。 作为扩展,phpredis在析构函数中关闭连接 - 所以你可能确定连接不会保持打开状态。</ li>
    </ ul>

    p.s。 当然你可以自己实现php中的代理类重新连接。 我们在高负载环境中有redis - 实际上每秒约4000个连接。 在2.4版本之后,我们不使用空闲连接超时。 并且没有任何类型的麻烦。</ p>
    </ div>

展开原文

原文

The answer to your question much depends on cases of redis usage in your application. So, should your never close a connection with idle connection timeout?

In general no, your should keep it default - 0. Why or when:

  • Any types of long living application. Such as CLI-script ot background worker. Why - phpredis do not has builded in reconnection feature so your should take care about this by yourself or do not your idle timeout.
  • Each time your request processed or CLI script die - all connections would be closed by php engine. Redis server close all connection for closed client sockets. You will have no problems like zombie connection or something like that. As extension, phpredis close connection in destructor - so your may be sure connections don't stay open.

p.s. Of course your can implement reconnection insome proxy class in php by yourself. We have redis in high load environment - ~4000 connections per second on instance. After 2.4 version we do not use idle connection timeout. And do not have any types of troubles with that.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐