对于单主机 Node.js 生产应用程序来说,什么是好的会话存储? [英] What is a good session store for a single-host Node.js production app?
问题描述
我正在使用 Node 的 Express 和 Connect 中间件.Connect 的内存会话存储不适合生产:
<块引用>警告:connection.session() MemoryStore 不是为生产环境设计的,因为它会泄漏内存,而且显然只能在单个进程中工作.
对于更大的部署,mongo 或 redis 是有意义的.
但是对于生产中的单主机应用程序来说,什么是好的解决方案?
花了一天时间研究这个问题.以下是我发现的选项.请求/秒通过 ab -n 100000 -c 1 http://127.0.0.1:9778/
在我的本地机器上执行.
- 无会话 - 快速(438 请求/秒)
- cookieSession:不需要外部服务,对速度影响较小(311 请求/秒)- 最快,会话将随着 cookie 过期(由
maxAge
自定义) - connect-redis:需要 redis 服务器,速度影响大(redis2go 4 req/sec和 redisgreen) - 比 mongo 更快,会话将在一段时间后被删除(由
ttl
自定义) - connect-mongo - 需要 mongodb 服务器,速度影响很大(2 req/sec with mongohq) - 比 redis 慢,需要手动将
clear_interval
设置为清理会话
这是我用于 cookieSession 的咖啡脚本:
server.use express.cookieSession({秘密:appConfig.site.salt饼干:最大年龄:1000*60*60})
这是我用于 redis 的咖啡脚本:
RedisSessionStore ?= require('connect-redis')(express)redisSessionStore ?= 新的 RedisSessionStore(主机:appConfig.databaseRedis.host端口:appConfig.databaseRedis.portdb: appConfig.databaseRedis.username通过:appConfig.databaseRedis.passwordno_ready_check: 真ttl: 60*60 # 小时)server.use express.session({秘密:appConfig.site.salt饼干:最大年龄:1000*60*60商店:redisSessionStore})
这是我为 mongo 编写的咖啡脚本:
server.use express.session({秘密:appConfig.site.salt曲奇饼:最大年龄:100*60*60商店:新 MongoSessionStore({数据库:appConfig.database.name主机:appConfig.database.host端口:appConfig.database.port用户名:appConfig.database.username密码:appConfig.database.passwordauto_reconnect: appConfig.database.serverOptions.auto_reconnectclear_interval: 60*60 # 小时})})
当然,远程 redis 和 mongo 数据库会比它们的本地数据库慢.我只是无法让本地等效项工作,特别是考虑到与托管远程替代方案相比,我的安装和维护时间远远超过我愿意投资的时间,我觉得其他人也是如此,因此为什么这些托管的远程数据库服务首先存在!
有关本地数据库基准,请参阅@Mustafa 的回答.
很高兴有人编辑此答案以将他们的本地数据库基准添加到组合中.
I'm using Node's Express w/ Connect middleware. Connect's memory session store isn't fit for production:
Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and obviously only work within a single process.
For larger deployments, mongo or redis makes sense.
But what is a good solution for a single-host app in production?
Spent the day looking into this. Here are the options I've discovered. Requests/second are performed via ab -n 100000 -c 1 http://127.0.0.1:9778/
on my local machine.
- no sessions - fast (438 req/sec)
- cookieSession: requires no external service, minor speed impact (311 req/sec) - fastest, sessions will expire with the cookie (customised by
maxAge
) - connect-redis: requires redis server, large speed impact (4 req/sec with redis2go and redisgreen) - faster than mongo, sessions will be deleted after a while (customised by
ttl
) - connect-mongo - requires mongodb server, large speed impact (2 req/sec with mongohq) - slower than redis, requires manual
clear_interval
to be set to cleanup sessions
Here is the coffeescript I used for cookieSession:
server.use express.cookieSession({
secret: appConfig.site.salt
cookie: maxAge: 1000*60*60
})
Here is the coffeescript I use for redis:
RedisSessionStore ?= require('connect-redis')(express)
redisSessionStore ?= new RedisSessionStore(
host: appConfig.databaseRedis.host
port: appConfig.databaseRedis.port
db: appConfig.databaseRedis.username
pass: appConfig.databaseRedis.password
no_ready_check: true
ttl: 60*60 # hour
)
server.use express.session({
secret: appConfig.site.salt
cookie: maxAge: 1000*60*60
store: redisSessionStore
})
Here is my coffeescript for mongo:
server.use express.session({
secret: appConfig.site.salt
cookie:
maxAge: 100*60*60
store: new MongoSessionStore({
db: appConfig.database.name
host: appConfig.database.host
port: appConfig.database.port
username: appConfig.database.username
password: appConfig.database.password
auto_reconnect: appConfig.database.serverOptions.auto_reconnect
clear_interval: 60*60 # hour
})
})
Now of course, the remote redis and mongo databases will be slower than their local equivalents. I just couldn't get the local equivalents working, especially considering the installation and maintenance time for me was far more than what I was willing to invest when compared with hosted remote alternatives, something I feel is true for others too hence why these hosted remote database services exist in the first place!
For local database benhmarks, see @Mustafa's answer.
Happy for someone to edit this answer to add their local database benchmarks to the mix.
这篇关于对于单主机 Node.js 生产应用程序来说,什么是好的会话存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!