This module returns the raw TCP response from the Redis server. It's recommended to use my [[LuaRedisParser]] (written in pure C) to parse these responses into lua data structure when combined with [[HttpLuaModule]].

This module returns the raw TCP response from the Redis server. It's recommended to use my [[LuaRedisParser]] (written in pure C) to parse these responses into lua data structure when combined with [[HttpLuaModule]].

+

+

When used in conjunction with [[HttpLuaModule]], it is recommended to use the [http://github.com/openresty/lua-resty-redis lua-resty-redis] library instead of this module though, because the former is much more flexible and memory-efficient.

If you only want to use the <code>get</code> redis command, you can try out the [[HttpRedisModule]]. It returns the parsed content part of the Redis response because only <code>get</code> is needed to implement.

If you only want to use the <code>get</code> redis command, you can try out the [[HttpRedisModule]]. It returns the parsed content part of the Redis response because only <code>get</code> is needed to implement.

Line 104:

Line 106:

<geshi lang="nginx">

<geshi lang="nginx">

−

location /pipelined {

+

location = /pipelined {

redis2_query set hello world;

redis2_query set hello world;

redis2_query get hello;

redis2_query get hello;

Line 145:

Line 147:

Here's some examples

Here's some examples

<geshi lang="nginx">

<geshi lang="nginx">

−

location /pipelined {

+

location = /pipelined {

redis2_raw_queries 3 "flushall\r\nget key1\r\nget key2\r\n";

redis2_raw_queries 3 "flushall\r\nget key1\r\nget key2\r\n";

redis2_pass 127.0.0.1:6379;

redis2_pass 127.0.0.1:6379;

Line 151:

Line 153:

# GET /pipelined2?n=2&cmds=flushall%0D%0Aget%20key%0D%0A

# GET /pipelined2?n=2&cmds=flushall%0D%0Aget%20key%0D%0A

−

location /pipelined2 {

+

location = /pipelined2 {

set_unescape_uri $n $arg_n;

set_unescape_uri $n $arg_n;

set_unescape_uri $cmds $arg_cmds;

set_unescape_uri $cmds $arg_cmds;

Line 251:

Line 253:

server {

server {

−

location /redis {

+

location = /redis {

redis2_next_upstream error timeout invalid_response;

redis2_next_upstream error timeout invalid_response;

redis2_query get foo;

redis2_query get foo;

Line 261:

Line 263:

= Connection Pool =

= Connection Pool =

−

You can use the excellent [[HttpUpstreamKeepaliveModule]] with this module to privide TCP connection pool for Redis.

+

You can use the excellent [[HttpUpstreamKeepaliveModule]] with this module to provide TCP connection pool for Redis.

A sample config snippet looks like this

A sample config snippet looks like this

Line 272:

Line 274:

# a pool with at most 1024 connections

# a pool with at most 1024 connections

# and do not distinguish the servers:

# and do not distinguish the servers:

−

keepalive 1024 single;

+

keepalive 1024;

}

}

server {

server {

...

...

−

location /redis {

+

location = /redis {

set_unescape_uri $query $arg_query;

set_unescape_uri $query $arg_query;

redis2_query $query;

redis2_query $query;

Line 288:

Line 290:

= Lua Interoperability =

= Lua Interoperability =

−

This module can be served as a non-blocking redis2 client for [[HttpLuaModule]].

+

This module can be served as a non-blocking redis2 client for [[HttpLuaModule]] (but nowadays it is recommended to use the [http://github.com/openresty/lua-resty-redis lua-resty-redis] library instead, which is much simpler to use and more efficient most of the time).

−

+

Here's an example using a GET subrequest:

Here's an example using a GET subrequest:

<geshi lang="nginx">

<geshi lang="nginx">

−

location /redis {

+

location = /redis {

internal;

internal;

Line 303:

Line 304:

}

}

−

location /main {

+

location = /main {

content_by_lua '

content_by_lua '

local res = ngx.location.capture("/redis",

local res = ngx.location.capture("/redis",

Line 326:

Line 327:

<geshi lang="nginx">

<geshi lang="nginx">

−

location /redis {

+

location = /redis {

internal;

internal;

Line 335:

Line 336:

}

}

−

location /main {

+

location = /main {

content_by_lua '

content_by_lua '

local res = ngx.location.capture("/redis",

local res = ngx.location.capture("/redis",

Line 366:

Line 367:

...

...

−

location /redis {

+

location = /redis {

set_unescape_uri $query $arg_query;

set_unescape_uri $query $arg_query;

redis2_query $query;

redis2_query $query;

Line 372:

Line 373:

}

}

−

location /foo {

+

location = /foo {

content_by_lua "

content_by_lua "

-- pick up a server randomly

-- pick up a server randomly

Line 461:

Line 462:

<geshi lang="nginx">

<geshi lang="nginx">

−

location /redis {

+

location = /redis {

redis2_raw_queries 2 "subscribe /foo/bar\r\n";

redis2_raw_queries 2 "subscribe /foo/bar\r\n";

redis2_pass 127.0.0.1:6379;

redis2_pass 127.0.0.1:6379;

Line 471:

Line 472:

You can surely parse the replies with the [[LuaRedisParser]] library if you're using Lua to access this module's location.

You can surely parse the replies with the [[LuaRedisParser]] library if you're using Lua to access this module's location.

−

== Publish/Subscribe Limitations ==

+

== Limitations For Redis Publish/Subscribe ==

+

+

If you want to use the [http://redis.io/topics/pubsub Redis pub/sub] feature with this module, then you must note the following limitations:

* You cannot use [[HttpUpstreamKeepaliveModule]] with this Redis upstream. Only short Redis connections will work.

* You cannot use [[HttpUpstreamKeepaliveModule]] with this Redis upstream. Only short Redis connections will work.

* There may be some race conditions that produce the harmless <code>Redis server returned extra bytes</code> warnings in your nginx's error.log. Such warnings might be rare but just be prepared for it.

* There may be some race conditions that produce the harmless <code>Redis server returned extra bytes</code> warnings in your nginx's error.log. Such warnings might be rare but just be prepared for it.

* You should tune the various timeout settings provided by this module like [[#redis2_connect_timeout|redis2_connect_timeout]] and [[#redis2_read_timeout|redis2_read_timeout]].

* You should tune the various timeout settings provided by this module like [[#redis2_connect_timeout|redis2_connect_timeout]] and [[#redis2_read_timeout|redis2_read_timeout]].

+

+

If you cannot stand these limitations, then you are highly recommended to switch to the [https://github.com/openresty/lua-resty-redis lua-resty-redis] library for [[HttpLuaModule]].

= Performance Tuning =

= Performance Tuning =

Line 489:

Line 494:

Alternatively, you can install this module manually by recompiling the standard Nginx core as follows:

Alternatively, you can install this module manually by recompiling the standard Nginx core as follows:

Description

This is an Nginx upstream module that makes nginx talk to a Redis 2.x server in a non-blocking way. The full Redis 2.0 unified protocol has been implemented including the Redis pipelining support.

This module returns the raw TCP response from the Redis server. It's recommended to use my LuaRedisParser (written in pure C) to parse these responses into lua data structure when combined with HttpLuaModule.

When used in conjunction with HttpLuaModule, it is recommended to use the lua-resty-redis library instead of this module though, because the former is much more flexible and memory-efficient.

If you only want to use the get redis command, you can try out the HttpRedisModule. It returns the parsed content part of the Redis response because only get is needed to implement.

Another option is to parse the redis responses on your client side yourself.

Directives

redis2_query

syntax:redis2_query cmd arg1 arg2 ...

default:no

context:location, location if

Specify a Redis command by specifying its individual arguments (including the Redis command name itself) in a similar way to the redis-cli utility.

Multiple instances of this directive are allowed in a single location and these queries will be pipelined. For example,

Lua Interoperability

This module can be served as a non-blocking redis2 client for HttpLuaModule (but nowadays it is recommended to use the lua-resty-redis library instead, which is much simpler to use and more efficient most of the time).
Here's an example using a GET subrequest:

where \r\n is CRLF. That is, this module returns the raw TCP responses from the remote redis server. For Lua-based application developers, they may want to utilize the LuaRedisParser library (written in pure C) to parse such raw responses into Lua data structures.

When moving the inlined Lua code into an external .lua file, it's important to use the escape sequence \r\n directly. We used \\r\\n above just because the Lua code itself needs quoting when being put into an Nginx string literal.

You can also use POST/PUT subrequests to transfer the raw Redis request via request body, which does not require URI escaping and unescaping, thus saving some CPU cycles. Here's such an example:

Here we assume that your Redis server is listening on the default port (6379) of the localhost. We also make use of the LuaRedisParser library to construct raw Redis queries for us and also use it to parse the replies.

Accessing the /test location via HTTP clients like curl yields the following output

OK
hello world

A more realistic setting is to use a proper upstream definition for our Redis backend and enable TCP connection pool via the keepalive directive in it.

Redis Publish/Subscribe Support

This module has limited support for Redis publish/subscribe feature. It cannot be fully supported due to the stateless nature of REST and HTTP model.

Performance Tuning

When you're using this module, please ensure you're using a TCP connection pool (provided by HttpUpstreamKeepaliveModule) and Redis pipelining wherever possible. These features will significantly improve performance.

Using multiple instance of Redis servers on your multi-core machines also help a lot due to the sequential processing nature of a single Redis server instance.

When you're benchmarking performance using something like ab or http_load, please ensure that your error log level is high enough (like warn) to prevent Nginx workers spend too much cycles on flushing the error.log file, which is always non-buffered and blocking and thus very expensive.

Copyright & License

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.