# # Copyright (c) Carlos Abalde # # You're free to use and distribute this under terms in the # LICENSE file. # $Module redis 3 Redis VMOD $ABI strict DESCRIPTION =========== VMOD using the synchronous hiredis library API to access Redis servers from VCL. $Event event_function $Function VOID subnets(PRIV_VCL, STRING masks="") Arguments subnets: Comma-delimited list of weights + subnet masks used to select among servers associated to a database instance when executing commands. Order is relevant. Only IPv4 addresses are supported. If not provided, value in the VMOD_REDIS_SUBNETS environment variable will be used. This is useful in setups using Redis instances deployed across multiple data centers or availability zones. Description Using this function is not mandatory. If used, it must be called during ``vcl_init`` before creating any database instance. If a database instance has already been created calls to this function are silently ignored. $Function VOID sentinels(PRIV_VCL, STRING locations="", INT period=60, INT connection_timeout=500, INT command_timeout=0, ENUM { RESP2, RESP3, default } protocol="default", BOOL tls=0, STRING tls_cafile="", STRING tls_capath="", STRING tls_certfile="", STRING tls_keyfile="", STRING tls_sni="", STRING password="") Arguments locations: Comma-delimited list of Redis Sentinel servers. Only host (IP or DNS name) + port format is allowed. If not provided, value in the VMOD_REDIS_SENTINELS environment variable will be used. This is useful in setups using non-clustered Redis instances in order to (1) keep roles (i.e. master / slave) updated; and (2) decrease priority of unreachable servers when creating execution plans. This is **not** designed for auto-discovery of Redis Servers. period: how frequently (seconds) Redis Sentinel periodical checks are executed (0 means disabling periodical checks). Beware (1) proactive checks are also executed anytime the VCL state is changed to warm; and (2) Sentinel Pub/Sub events are listened and processed even when periodical checks are disabled. connection_timeout: connection timeout (milliseconds; 0 means no timeout) to the Redis Sentinel servers. command_timeout: command timeout (milliseconds; 0 means no timeout) when executing Redis Sentinel commands. protocol: protocol to be used when talking to Redis Sentinel servers. Beware switching from ``RESP2`` to ``RESP3`` is only possible since Redis 6.0 and it uses an additional ``HELLO`` command when creating new Redis Sentinel connections. Be careful ``RESP3`` support is only available since ``hiredis`` 0.15.0. tsl: if enabled, TLS will be used when establishing connections. tls_cafile: if not empty, this CA certificate file will be used to verify TLS connections. tls_capath: if not empty, this will be the directory storing trusted CA certificates used to verify TLS connections. If neither ``tls_cafile`` nor ``tls_capath`` are specified, the default system-wide trusted root certs configuration will apply. tls_certfile: if not empty, this certificate file will be used to authenticate TLS connections. tls_keyfile: if not empty, this private key file will be used to authenticate TLS connections. tls_sni: if not empty, this server name indication (i.e. SNI) will be used when establishing TLS connections. password: if not empty, requests for authentication using the ``AUTH`` command will be submitted when creating new Redis Sentinel connections. This password will be used for all Redis Sentinel servers. Description Using this function is not mandatory. $Function VOID use(PRIV_VCL, PRIV_TASK, STRING db) Arguments db: VCL name of the database instance. Description Sets the current database instance to be used by proxied methods (if none is specified with their ``db`` argument). $Function VOID add_server( PRIV_VCL, PRIV_TASK, STRING location, ENUM { master, slave, auto, cluster } type, STRING db="") Description Proxied version of ``.add_server()``. $Function VOID command(PRIV_VCL, PRIV_TASK, STRING name, STRING db="") Description Proxied version of ``.command()``. $Function VOID timeout(PRIV_VCL, PRIV_TASK, INT command_timeout, STRING db="") Description Proxied version of ``.timeout()``. $Function VOID retries(PRIV_VCL, PRIV_TASK, INT max_command_retries, STRING db="") Description Proxied version of ``.retries()``. $Function VOID push(PRIV_VCL, PRIV_TASK, STRING arg, STRING db="") Description Proxied version of ``.push()``. $Function VOID execute(PRIV_VCL, PRIV_TASK, BOOL master=1, STRING db="") Description Proxied version of ``.execute()``. $Function VOID easy_execute(PRIV_VCL, PRIV_TASK, STRING command, [STRING cmd_arg1], [STRING cmd_arg2], [STRING cmd_arg3], [STRING cmd_arg4], [STRING cmd_arg5], [STRING cmd_arg6], [STRING cmd_arg7], [STRING cmd_arg8], [STRING cmd_arg9], [STRING cmd_arg10], [STRING cmd_arg11], [STRING cmd_arg12], [STRING cmd_arg13], [STRING cmd_arg14], [STRING cmd_arg15], [STRING cmd_arg16], [INT timeout], [INT retries], BOOL master = 1, STRING db="" ) Description Proxied version of ``.easy_execute()``. $Function BOOL replied(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.replied()``. $Function BOOL reply_is_error(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_error()``. $Function BOOL reply_is_nil(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_nil()``. $Function BOOL reply_is_status(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_status()``. $Function BOOL reply_is_integer(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_integer()``. $Function BOOL reply_is_boolean(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_boolean()``. $Function BOOL reply_is_double(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_double()``. $Function BOOL reply_is_string(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_string()``. $Function BOOL reply_is_array(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.reply_is_array()``. $Function STRING get_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_reply()``. $Function STRING get_error_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_error_reply()``. $Function STRING get_status_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_status_reply()``. $Function INT get_integer_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_integer_reply()``. $Function BOOL get_boolean_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_boolean_reply()``. $Function REAL get_double_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_double_reply()``. $Function STRING get_string_reply(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_string_reply()``. $Function INT get_array_reply_length(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.get_array_reply_length()``. $Function BOOL array_reply_is_error(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_error()``. $Function BOOL array_reply_is_nil(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_nil()``. $Function BOOL array_reply_is_status(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_status()``. $Function BOOL array_reply_is_integer(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_integer()``. $Function BOOL array_reply_is_boolean(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_boolean()``. $Function BOOL array_reply_is_double(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_double()``. $Function BOOL array_reply_is_string(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_string()``. $Function BOOL array_reply_is_array(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.array_reply_is_array()``. $Function STRING get_array_reply_value(PRIV_VCL, PRIV_TASK, INT index, STRING db="") Description Proxied version of ``.get_array_reply_value()``. $Function VOID free(PRIV_VCL, PRIV_TASK, STRING db="") Description Proxied version of ``.free()``. $Function STRING stats(PRIV_VCL, PRIV_TASK, ENUM { json, prometheus } format="json", BOOL stream=0, STRING prometheus_name_prefix="vmod_redis_", BOOL prometheus_default_labels=1, STRING prometheus_extra_labels="", STRING db="") Description Proxied version of ``.stats()``. $Function INT counter(PRIV_VCL, PRIV_TASK, STRING name, STRING db="") Description Proxied version of ``.counter()``. $Object db(PRIV_VCL, STRING location="", ENUM { master, slave, auto, cluster } type="auto", INT connection_timeout=1000, INT connection_ttl=0, INT command_timeout=0, INT max_command_retries=0, BOOL shared_connections=1, INT max_connections=128, ENUM { RESP2, RESP3, default } protocol="default", BOOL tls=0, STRING tls_cafile="", STRING tls_capath="", STRING tls_certfile="", STRING tls_keyfile="", STRING tls_sni="", STRING user="", STRING password="", INT sickness_ttl=60, BOOL ignore_slaves=0, INT max_cluster_hops=32) Arguments location: Redis connection string. Both host (IP or DNS name) + port and UNIX sockets are supported. Only the IP + port format is allowed when Redis Cluster support is enabled. **Do not use DNS names in combination with ``redis.subnets()`` or ``redis.sentinels()``**. type: type of the Redis server referenced by ``location``: - The ``auto`` value enables automatic discovery of the current role of the server on bootstrap time using the ``ROLE`` command. - The ``cluster`` value enables Redis Cluster support, automatically discovering other servers in the cluster and their roles on demand using the ``CLUSTER SLOTS`` command. connection_timeout: connection timeout (milliseconds; 0 means no timeout) to the Redis server. connection_ttl: TTL (seconds) of Redis connections (0 means no TTL). Once the TTL of a connection is consumed, the module transparently reestablishes it. See "Client timeouts" in http://redis.io/topics/clients for extra information. command_timeout: command timeout (milliseconds; 0 means no timeout) when executing a Redis command. It can be overridden during command execution with ``.timeout()``. max_command_retries: number of retries to be executed after a failed command execution. It can be overridden during command execution with ``.retries()``. shared_connections: if enabled, Redis connections are not local to Varnish worker threads, but shared by all threads using one or more pools. max_connections: When ``shared_connections`` is enabled, the VMOD creates one pool of Redis connections per location and database instance. This option sets the maximum number of connections in each pool. All Varnish worker threads using the same database instance will share connections in these pools. Pools are not shared between database instances. Note that when Redis Cluster support is enabled for a database, each server is the cluster is internally labeled by the VMOD with its location (IP + port). When ``shared_connections`` is disabled, Redis connections are local to each Varnish worker thread. This option sets the maximum number of Redis connections per Varnish worker thread. Each thread keeps up to one connection per location and database instance. If more than one (database, location) pair is available, incrementing this limit allows recycling of Redis connections. WARNING: when creating multiple database instances, always use the same value in all instances using private connections. Otherwise you may experience inconsistent behavior. protocol: protocol to be used when talking to Redis Server servers. Beware switching from ``RESP2`` to ``RESP3`` is only possible since Redis 6.0 and it affects to type conversion rules. Be careful ``RESP3`` support is only available since ``hiredis`` 0.15.0. tsl: if enabled, TLS will be used when establishing connections. tls_cafile: if not empty, this CA certificate file will be used to verify TLS connections. tls_capath: if not empty, this will be the directory storing trusted CA certificates used to verify TLS connections. If neither ``tls_cafile`` nor ``tls_capath`` are specified, the default system-wide trusted root certs configuration will apply. tls_certfile: if not empty, this certificate file will be used to authenticate TLS connections. tls_keyfile: if not empty, this private key file will be used to authenticate TLS connections. tls_sni: if not empty, this server name indication (i.e. SNI) will be used when establishing TLS connections. user: if not empty and a ``password`` is provided, requests for authentication using the ``AUTH`` or ``HELLO`` command will include both the user name and its password. That enables usage of the Redis ACL system, available since Redis 6.0. password: if not empty, requests for authentication using the ``AUTH`` or ``HELLO`` command will be submitted when creating new Redis connections. This password will be used for all Redis servers, including discovered servers when Redis Cluster support is enabled. sickness_ttl: TTL (seconds) of the sickness flag (0 means no sickness flags) associated to a Redis server. Once a server is flagged as sick and while the flag is not expired the module will lazily drop existing connections to that server and it will try to avoid it as much as possible when creating execution plans. This TTL is overridden when Redis Sentinel servers are configured and some Sentinel discovers an unreachable server. ignore_slaves: if enabled, slave servers are not considered when executing commands. In other words, enabling this option implicitly forces ``master == true`` when using the ``.execute()`` method. max_cluster_hops: maximum number of redirections (0 means no limit) when executing a command and Redis Cluster support has been enabled. Description Creates a new database instance. $Method VOID .add_server( PRIV_VCL, STRING location, ENUM { master, slave, auto, cluster } type) Arguments location: Redis connection string. Both host (IP or DNS name) + port and UNIX sockets are supported. If Redis Cluster support has been enabled only the IP + port format is allowed. **Do not use DNS names in combination with ``redis.subnets()`` or ``redis.sentinels()``**. type: type of the Redis server referenced by ``location``: - The ``auto`` value enables automatic discovery of the current role of the server on bootstrap time using the ``ROLE`` command. - If Redis Cluster support has been enabled ``cluster`` is the only allowed value. Description Adds an extra Redis server. If Redis Cluster support has been enabled other servers in the cluster and their roles are automatically discovered by the VMOD using the ``CLUSTER SLOTS`` command. In any case, knowing more cluster servers during startup increases the chances of discover the cluster topology if some server is failing. $Method VOID .command(PRIV_TASK, STRING name) Arguments name: name of the Redis command to be executed. Description Enqueues a Redis command (only the name of the command) for further execution. Arguments should be enqueued separately calling one or more times to the ``.push()`` method. On execution time, ``EVAL`` commands are internally replace by ``EVALSHA`` commands, which fallback to the original ``EVAL`` command if the Redis server returns a ``NOSCRIPT`` error (see http://redis.io/commands/eval). $Method VOID .timeout(PRIV_TASK, INT command_timeout) Arguments command_timeout: command timeout (milliseconds; 0 means no timeout) to be used when executing a specific Redis command. Description Allows overriding the default command timeout provided when calling ``.db()``. $Method VOID .retries(PRIV_TASK, INT max_command_retries) Arguments max_command_retries: number of retries to be executed after a failed command execution. Description Allows overriding the default number of retries provided when calling ``.db()``. $Method VOID .push(PRIV_TASK, STRING arg) Arguments arg: argument of a previously enqueued Redis command. Description Provides an argument to a previously enqueued Redis command. $Method VOID .execute(PRIV_VCL, PRIV_TASK, BOOL master=1) Arguments master: when enabled slave servers are not considered for execution. Return value VOID Description Executes a previously enqueued Redis command. When more than one Redis server is available, the destination server is selected according with ``master`` and with the list of weights + subnet masks provided when calling ``redis.subnets()``. WARNING: allowing execution of commands in slave servers (i.e. ``master == false``) must be carefully used: - When Redis Cluster support is disabled, sending a read-write command to a read-only slave server will result in an error reply. - It's safe to send read-only LUA scripts using ``EVAL`` or ``EVALSHA`` commands to a read-only slave server when Redis Cluster support is disabled. Being a read-only slave server does not block writes in the script cache. - When Redis Cluster support is enabled, sending a read-write command to a slave server will result in a redirection to the right master server. This kind of redirections trigger an internal rediscovery of the cluster topology. The client side won't notice the redirection, but triggering such an expensive operation on every command is a **terrible idea**. - **It's not safe to send read-only LUA scripts using ``EVAL`` or ``EVALSHA`` commands to a slave server when Redis Cluster support is enabled**. Redis Cluster handles ``EVAL`` and ``EVALSHA1`` command as write operations that must be redirected to a master server. Therefore, this would result on a rediscovery of the cluster topology on every command execution. The VMOD internally forces ``master == true`` when Redis Cluster support is enabled and ``EVAL`` or ``EVALSHA`` command are submitted in order to avoid this counterintuitive scenario. $Method VOID .easy_execute(PRIV_VCL, PRIV_TASK, STRING command, [STRING cmd_arg1], [STRING cmd_arg2], [STRING cmd_arg3], [STRING cmd_arg4], [STRING cmd_arg5], [STRING cmd_arg6], [STRING cmd_arg7], [STRING cmd_arg8], [STRING cmd_arg9], [STRING cmd_arg10], [STRING cmd_arg11], [STRING cmd_arg12], [STRING cmd_arg13], [STRING cmd_arg14], [STRING cmd_arg15], [STRING cmd_arg16], [INT timeout], [INT retries], BOOL master = 1 ) Arguments The command argument is mandatory, followed with up to 16 arguments, optionally ending with `timeout` (as passed to `.timeout()`) and/or `retries` (as passed to `.retries()`) and/or `master` (`.execute()`). For example: `db.easy_command("set", "foo", "hello", retries=3, master=true);` Return value VOID Description Equivalent to calling, `.command()`, `.push()` (possibly multiple times), `.timeout()`, `.retries()` then finally `.execute()` using a single command. $Method BOOL .replied(PRIV_TASK) Return value TRUE if a previously executed Redis command using ``.execute()`` returned any reply. Not returning a reply usually means a failed connection, a connection timeout, etc. $Method BOOL .reply_is_error(PRIV_TASK) Return value TRUE if a previously executed Redis command using ``.execute()`` returned an error reply. $Method BOOL .reply_is_nil(PRIV_TASK) Return value TRUE if a previously executed Redis command using ``.execute()`` returned a nil reply. $Method BOOL .reply_is_status(PRIV_TASK) Return value TRUE if a previously executed Redis command using ``.execute()`` returned a status reply. $Method BOOL .reply_is_integer(PRIV_TASK) Return value TRUE if a previously executed Redis command ``.execute()`` returned an integer reply. $Method BOOL .reply_is_boolean(PRIV_TASK) Return value TRUE if a previously executed Redis command ``.execute()`` returned an boolean reply. $Method BOOL .reply_is_double(PRIV_TASK) Return value TRUE if a previously executed Redis command ``.execute()`` returned an double reply. $Method BOOL .reply_is_string(PRIV_TASK) Return value TRUE if a previously executed Redis command ``.execute()`` returned a string or verbatim reply. $Method BOOL .reply_is_array(PRIV_TASK) Return value TRUE if a previously executed Redis command using ``.execute()`` returned an array, map or set reply. $Method STRING .get_reply(PRIV_TASK) Return value A string representation of the reply of a previously executed Redis command using ``.execute()``. Description Do not use this function to access to array, map or set replies. $Method STRING .get_error_reply(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned an error reply, this function returns a string representation of that reply. $Method STRING .get_status_reply(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned a status reply, this function returns a string representation of that reply. $Method INT .get_integer_reply(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned an integer reply, this function returns an integer representation of that reply. $Method BOOL .get_boolean_reply(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned an boolean reply, this function returns an boolean representation of that reply. $Method REAL .get_double_reply(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned an double reply, this function returns an double representation of that reply. $Method STRING .get_string_reply(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned a string or verbatim reply, this function returns a string representation of that reply. $Method INT .get_array_reply_length(PRIV_TASK) Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns the number of elements in that reply. $Method BOOL .array_reply_is_error(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is an error reply (nested responses are not supported). $Method BOOL .array_reply_is_nil(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is a nil reply (nested responses are not supported). $Method BOOL .array_reply_is_status(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is a status reply (nested responses are not supported). $Method BOOL .array_reply_is_integer(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is an integer reply (nested responses are not supported). $Method BOOL .array_reply_is_boolean(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is an boolean reply (nested responses are not supported). $Method BOOL .array_reply_is_double(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is an double reply (nested responses are not supported). $Method BOOL .array_reply_is_string(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is a string or verbatim reply (nested responses are not supported). $Method BOOL .array_reply_is_array(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns TRUE if the nth element in that reply is an array, map or set reply (nested responses are not supported). $Method STRING .get_array_reply_value(PRIV_TASK, INT index) Arguments index: index in the array reply. Return value If a previously executed Redis command using ``.execute()`` returned an array, map or set reply, this function returns a string representation of the nth element in that reply (nested responses are not supported). $Method VOID .free(PRIV_TASK) Description Frees memory internally used by Redis commands an replies. It's recommended to use this function, but if not called this will be handled automatically during the next call to ``.command()`` using the same object. $Method STRING .stats( ENUM { json, prometheus } format="json", BOOL stream=0, STRING prometheus_name_prefix="vmod_redis_", BOOL prometheus_default_labels=1, STRING prometheus_extra_labels="") Arguments format: format of the output string. stream: if enabled, the response object will be streamed as a synthetic response. prometheus_name_prefix: prefix to be used in all Prometheus metric names. It's used as a raw value; no validations or transformations at all. prometheus_default_labels: if enabled, some default Prometheus labels will be added to all stats. prometheus_extra_labels: extra Prometheus labels to be added to all stats. It's used as a raw value; no validations or transformations at all. Description Returns internal stats represented as a string. If called during ``vcl_synth`` or ``vcl_backend_error`` with the ``stream`` argument enabled, this function will return an empty string and behave as a call to the ``synthetic`` VCL function with the response object as input. This highly reduces the amount of required workspace memory. $Method INT .counter(STRING name) Arguments name: name of the counter. Description Returns internal counter.