Skip to content

Using Resty Redis Cluster library

Conor McKnight edited this page Jun 5, 2026 · 5 revisions

The script will work without the need for any external libraries but I did build in the option to use them if you like.

To use the resty redis cluster library all you need to do is edit your nginx config to include this in your http block http {

# Updated and maintained Library https://github.com/api7/lua-resty-redis-cluster
lua_package_path "./conf/lua/lua-resty-redis-cluster/lib/resty/?.lua;;";

Then you can change your config to work with Resty Redis Cluster like so. Set all vars to nil the 12th var is the Options table what is used for connecting to rediscluster servers.

localized.remote_servers_table = {
	8, --storage server for cache redis = 1 memcached = 2 lrucache = 3 ngx.shared.dict = 4 resty.redis.fast = 5 resty.redis.cluster.fast = 6 resty.memcached.fast = 7 rediscluster = 8
	nil, --ipaddress or "unix:/path/to/unix.sock" if using socket set port to nil
	nil, --port memcached 11211 redis 6379
	nil,--1000, --connect_timeout 1 second
	nil,--1000, --send_timeout 1 second
	nil,--1000, --read_timeout 1 second
	nil,--10000, --keepalive max_idle_timeout 10 seconds
	nil,--100, --keepalive pool_size
	nil,--"user", --auth_user
	nil,--"pass", --auth_pass
	nil,--{}--11th table fallback incase server offline or goes down
	{--12th table for connection options :connect(host, port, options_table?) Examples on usage here https://github.com/api7/lua-resty-redis-cluster#sample-usage
		dict_name = "test_locks",               --shared dictionary name for locks
		name = "testCluster",                   --rediscluster name
		serv_list = {                           --redis cluster node list(host and port),
			{ ip = "127.0.0.1", port = 7001 },
			{ ip = "127.0.0.1", port = 7002 },
			{ ip = "127.0.0.1", port = 7003 },
			{ ip = "127.0.0.1", port = 7004 },
			{ ip = "127.0.0.1", port = 7005 },
			{ ip = "127.0.0.1", port = 7006 }
		},
		keepalive_timeout = 60000,              --redis connection pool idle timeout
		keepalive_cons = 1000,                  --redis connection pool size
		connect_timeout = 1000,              --timeout while connecting
		max_redirection = 5,                    --maximum retry attempts for redirection
		max_connection_attempts = 1,             --maximum retry attempts for connection
	},
	nil,--13th close_connection handling setting this to 1 will close connections after use, to use this instead of keepalive max_idle_timeout and pool_size above set the above to nil, having both above and this value as nil resty library will resort to default connection handling behaviour
}

This library is up-to-date and maintained by API7 :)

Clone this wiki locally