This is a very basic cluster capable command line tool for Redis (OSS Cluster).
Redis is coming with a very sophisticated command line tool, called redis-cli. Especially older versions of this tool are using a light-weighted approach for interacting with an OSS Redis Cluster.
- Establish the connection to one of the endpoints (shards)
- Execute the command to the shard to which the tool is currently connected
- If the shard is not responsible for the key which was passed over as an argument, then the client is retrieving a redirect (MOVED) response
- Follow the redirect response by replacing the current connection with a new one to this shard
- Execute the command again
There is a challange when a Key-less command (a command which doesn't get a key passed) is executed. In this case redis-cli will just execute the command to the current connection which then causes partial results (i.e. only the size of the shard to which the tool was connected). The tool redis-cli-pycluster is using the redispy-cluster smart client library which means that it behaves differently:
- Establish the connection to one of the endpoints
- Fetch the cluster topology by executing
CLUSTER SLOTS - Establish a connection to each endpoint in the cluster
- A command which is getting a key passed will be executed against the right endpoint from the very beginning
- Commands without key arguments will be executed in parallel against every endpoint. The result is grouped by endpoint.
- This command line tool is not (yet) interactive, but it allows to pass a command as an argument
- The tool doesn't return the result in the same format as
redis-clidoes. The response is already parsed and converted into an equivalent Python type/object (i.e. lists, dictonary, boolean, ...). The output is the string representation of this value.
Usage: redis-cli-pycluster.py [OPTIONS] COMMAND
Options:
--host TEXT Database host name or IP address
--port INTEGER Database port
--passwd TEXT Database password
--help Show this message and exit.
If the COMMAND argument contains spaces (i.e. SET hello world) then it's required to wrap the command with single quotes.
Command:
python redis-cli-pycluster.py --host=myhost --port=18468 'SET hello world'
Output:
True
Command:
python redis-cli-pycluster.py --host=myhost --port=18468 --passwd mypass 'GET hello'
Output:
world
Command:
python redis-cli-pycluster.py --host=myhost --port=18468 'DBSIZE'
Output:
{'3.81.97.87:18468': 1L, '3.81.32.34:18468': 0L}
Command:
python redis-cli-pycluster.py --host=myhost --port=18468 'SCAN 0'
Output:
{'3.81.97.87:18468': (0L, [u'hello']), '3.81.32.34:18468': (0L, [])}
The build box should have Python 2.7 and pip installed
The build script is working the following way:
- Create Python 2.7 VirtualEnv
- Install all dependencies to this environment
- Use
PyInstallerin order to package a standalone version (doesn't need Python installed on the target machnine) - Wrap the dist package into a TAR file
The result is the file redis-cli-pycluster.tar.
PyInstaller is not a cross-platform compiler, which means that you will need to execute the build script at least once on your target platform.