在Heroku上使用Memcache时,我应该配置Beaker的`session.lock_dir`吗?

时间:2012-08-06 17:44:17

标签: python heroku memcached pyramid beaker

我正在将Pyramid应用程序的会话配置从cookie切换到ext:memcached。我的应用程序托管在Heroku上,我已根据memcache addon配置了their documentation

我从Beaker documentation了解到,指定session.lock_dir对于阻止dog pile effect至关重要。要明确:我必须提供目录的文件路径。 Beaker然后将其用作某种锁,以防止多个客户端同时尝试设置相同的值。

对我来说,这听起来像糟糕的建筑。 memcache的一个主要优点是它可以作为共享的外部服务。将我的应用程序进程绑定到磁盘绑定锁似乎是错误的方法。

同样,在Heroku上,每个“dyno”我有一个ephemeral个文件系统(我理解这意味着每个进程)。因此,虽然我可以提供lock_dir目录路径,但如果每个进程使用不同的目录,那么这会保护我免受狗堆效应吗?

我不确定我是否应该:

  • 指定一个lock_dir而不用担心
  • 尝试分叉Beaker并以某种方式修补

另外,我想知道其他语言/框架在这里使用的模式。这只是一个烧杯问题还是其他非文件绑定设置遭受狗堆效应?

提前致谢,

詹姆斯。

1 个答案:

答案 0 :(得分:0)

不幸的是,根据我的经验,由于缺少分布式锁定,Beaker在多主机基础架构上运行不佳。如果你在2个主机上运行的web应用程序中使用beaker,那么狗堆效应仍然会发生(但是可以通过将来自同一用户的所有请求粘贴到一个服务器来避免它,但它不适用于所有平台)。

有时你可以忽略狗堆效应,有时候不会。例如。如果你缓存全局数据,狗堆效应是痛苦的。如果您缓存用户数据,有时可以忽略它。

在你的情况下,我会忽略它。烧杯允许您使用Memcache扩展而无需任何锁定目录。在这种情况下,Beaker仅使用线程锁锁定每个进程的工作。

以下是证据:

from beaker.middleware import SessionMiddleware
from werkzeug.wrappers import Response
from werkzeug.serving import run_simple


def simple_app(environ, start_response):
    # Get the session object from the environ
    session = environ['beaker.session']

    # Check to see if a value is in the session
    user = 'user_id' in session

    # Set some other session variable
    session['user_id'] = 10
    session.save()

    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['User is logged in: %s' % user]

# Configure the SessionMiddleware
session_opts = {
    'session.type': 'ext:memcached',
    'session.url': '127.0.0.1:11211',
    'session.auto': True,
    'session.cookie_expires': True,
}
wsgi_app = SessionMiddleware(simple_app, session_opts)


if __name__ == '__main__':
    run_simple('127.0.0.1', 5000, wsgi_app, use_debugger=True, use_reloader=True)

如果狗堆效应仍然是一个问题,我建议从Beaker迁移到其他地方。例如。 dogpile.cache