redis

Description

The Redis protocol support allows APISIX to proxy Redis commands, and provide various features according to the content of the commands, including:

  • Redis protocol codec
  • Fault injection according to the commands and key
redis - 图1note

This feature requires APISIX to be run on APISIX-Base.

It also requires the data sent from clients are well-formed and sane. Therefore, it should only be used in deployments where both the downstream and upstream are trusted.

Granularity of the request

Like other protocols based on the xRPC framework, the Redis implementation here also has the concept of request.

Each Redis command is considered a request. However, the message subscribed from the server won’t be considered a request.

For example, when a Redis client subscribes to channel foo and receives the message bar, then it unsubscribes the foo channel, there are two requests: subscribe foo and unsubscribe foo.

Attributes

NameType          RequiredDefault                                      Valid values                                                      Description                                                                                                                                                                                                                                          
faultsarray[object]        False                                                  Fault injections which can be applied based on the commands and keys

Fields under an entry of faults:

NameType          RequiredDefault                                      Valid values                                                      Description                                                                                                                                                                                                                                          
commandsarray[string]        True                                                 [“get”, “mget”]  Commands fault is restricted to
keystring        False                                                 “blahblah”  Key fault is restricted to
delaynumber        True                                                 0.1  Duration of the delay in seconds

Metrics

  • apisix_redis_commands_total: Total number of requests for a specific Redis command.

    LabelsDescription
    routematched stream route ID
    commandthe Redis command
  • apisix_redis_commands_latency_seconds: Latency of requests for a specific Redis command.

    LabelsDescription
    routematched stream route ID
    commandthe Redis command

Example usage

Assumed the APISIX is proxying TCP on port 9101, and the Redis is listening on port 6379.

Let’s create a Stream Route:

  1. curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "upstream": {
  4. "type": "none",
  5. "nodes": {
  6. "127.0.0.1:6379": 1
  7. }
  8. },
  9. "protocol": {
  10. "name": "redis",
  11. "conf": {
  12. "faults": [{
  13. "commands": ["get", "ping"],
  14. "delay": 5
  15. }]
  16. }
  17. }
  18. }
  19. '

Once you have configured the stream route, as shown above, you can make a request to it:

  1. redis-cli -p 9101
  1. 127.0.0.1:9101> ping
  2. PONG
  3. (5.00s)

You can notice that there is a 5 seconds delay for the ping command.