[Others]Introduction of Redis cluster's support of select command through Proxy
Created#More Posted time:Mar 21, 2017 15:15 PM
Currently open-source schemes for Redis clusters mainly include Redis Cluster, Codis, and Twemproxy. But none of them support the select command, that is, you cannot perform logic database switching using the select command. This will cause trouble to users who have previously used standalone Redis products, and as a result, many users have to renovate their code for transforming to the cluster scheme. This article discusses how to enable the select command support in the Redis cluster.
Alibaba Cloud ApsaraDB for Redis cluster
The cluster version of Alibaba Cloud ApsaraDB for Redis is composed of three major components:
• Redis-config: the cluster manager.
• Redis-server: a Redis database with optimized source code. It supports slots, resizing and migration.
• Redis-proxy: single-threaded, with a kernel in the C++14 language.
The data link diagram is shown below. A user's requests are distributed to multiple backend nodes after load balancing by the Proxy.
Introduction of Proxy
Redis usually serves as the cache. Users all hope for a minimal latency when accessing Redis, so we chose C++ for development. In addition, we chose C++14 for development to leverage more new features of C++, which has substantially streamlined the code and improved the code readability. To simplify the model design, the Proxy is designed into a single-process and single-threaded structure, and all user requests are forwarded in only one worker thread. In addition, each user has an independent proxy to ensure the isolation between cloud resources and avoid the interaction between different users. At the same time, the Proxy provides an HTTP interface for the control and query by the control system. The Proxy of Alibaba Cloud ApsaraDB for Redis cluster has the following features:
• Supports single-slot commands and set, get and other single-key commands
• Supports multi-key commands and request commands across databases such as mset and mget
• Supports pubsub subscription command
• Supports transactions on the same slot
• Supports the select command
• Hot upgrade with no connection interruptions
• High performance
• Low latency
From the above features we can see Alibaba Cloud ApsaraDB for Redis cluster Proxy supports a vast majority of the Redis commands, with high compatibility. In the future, Alibaba Cloud ApsaraDB for Redis cluster Proxy will also provide block-type command support, read/write splitting and other features.
How does Proxy support the select command
Scheme 1: one-to-one connection
A simple scheme is shown in the figure above. Proxy can maintain a one-to-one connection with the client. One-to-one connections are set up for requests by all clients, and all requests are forwarded directly to the backend for processing. This scheme can directly enable the support of the select command, but it also brings about a problem: it needs to establish connections with the backend Redis frequently, compromising the performance. In addition, when short connections are used by the user, it will cause a connection storm, making the connection unstable.
Scheme 2: persistent connection
In Scheme 2, a persistent connection is maintained between the Proxy and the backend Redis and all the commands are forwarded through the persistent connection. Proxy needs to maintain the database information of every front-end client and the added select call is displayed at the forward command. When the front-end client uses different databases, this scheme will lead to frequent switching of the backend database connections. The QPS magnification is directly related with the number of client connections, so this scheme will greatly impair the cluster performance.
Scheme 3: database-level persistent connection
As shown in the figure above, every database maintains a persistent connection between the Proxy to Redis. Every time when a persistent connection is established, the select command will be executed to mark the connection as one for a database. After the user executes the select command, we mark this connection as one for the corresponding database. After Proxy receives the user request, it forwards the request according to the database information of the connection to the corresponding persistent connection in the backend. If no persistent connection exists, a corresponding database persistent connection is created. Considering that the native Redis supports 16 databases, the number of supported connections by Proxy is limited when there are not many backend Redis nodes. This scheme can reduce the number of Proxy connections and can merge and send the commands over the same connection to improve the Proxy-to-Redis performance.
Alibaba Cloud ApsaraDB for Redis adopts the aforementioned Scheme 3 to achieve support of the select command. In actual scenarios, the Proxy also needs to support transaction and subscription commands, so the real-world connection models are more complicated than the ones mentioned above. We will also continue to introduce how to implement transaction and subscription commands later.