Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

Node.js で Redis を使う場合、コネクションプールは必要ない

$
0
0

TL;DR

Node.js も Redis もシングルスレッドなので、 Node 1プロセスにつき接続ひとつでよい。
トップレベルで createClientをして、その先で使えばよい。

本文

Redisはよく使われるインメモリデータストアです。

ふつうの DB (Postgres とか)だとコネクションプールを普通に使うので、 Redis にもあるのかなーと思って node-redis のドキュメントを見に行くとコネクションプールに関する記載がない。
どういうこっちゃと思って GitHub の issue をあさりに行くと、その理由がありました。

connecting pool · Issue #1354 · NodeRedis/node-redis

Since Node and Redis are both (practically) single-threaded a single connection per Node process is the general pattern. Just use createClient at the top level of your script and move on.

(筆者訳)
Node も Redis も両方(実質)シングルスレッドだから、 Node のプロセスひとつにつき1コネクションとするのが一般的なパターン。たんにトップレベルで createClientを使って、その先に進めばよい。

なるほどなぁ。
例外もあるらしいので、気をつけて実装しましょう。

Exceptions would be blocking commands (BLPOP, XREAD, etc.) and SUBSCRIBE - those need their own connection.

(筆者訳)
ブロッキングコマンド (BLPOP とか XREAD とか……)と SUBSCRIBE は例外で、これらには独自のコネクションが必要。


Viewing all articles
Browse latest Browse all 8691

Trending Articles