Keywords: macOS | redis-cli | Homebrew | Docker | Redis Protocol
Abstract: This article explores various technical solutions for installing only the Redis command-line tool redis-cli on macOS systems. It first analyzes the file structure after installing the complete Redis package via Homebrew, highlighting its lightweight nature. Then it introduces the method of using third-party Homebrew tap for dedicated redis-cli installation. The article also discusses the temporary solution of running redis-cli via Docker containers and presents the alternative approach of installing JavaScript-based redis-cli through npm. Furthermore, it delves into the fundamental principles of the Redis protocol and provides example code for implementing a simple Redis client using bash scripts, helping readers understand the underlying communication mechanisms.
Analysis of Redis Command-Line Tool Installation Methods
In macOS development environments, there are scenarios where only the Redis command-line client redis-cli is needed without installing the complete Redis server. This article systematically analyzes multiple implementation approaches based on best practices from the technical community.
Complete Installation via Homebrew
The most straightforward method is installing the complete Redis package through Homebrew. Execute the following commands:
brew install redis
brew ls redis
After installation, examining the file structure reveals that the Redis package is actually quite lightweight:
/usr/local/Cellar/redis/3.2.3/bin/redis-benchmark
/usr/local/Cellar/redis/3.2.3/bin/redis-check-aof
/usr/local/Cellar/redis/3.2.3/bin/redis-check-rdb
/usr/local/Cellar/redis/3.2.3/bin/redis-cli
/usr/local/Cellar/redis/3.2.3/bin/redis-sentinel
/usr/local/Cellar/redis/3.2.3/bin/redis-server
/usr/local/Cellar/redis/3.2.3/homebrew.mxcl.redis.plist
A more detailed directory examination shows:
ls -lR /usr/local/Cellar/redis/3.2.3
total 40
-rw-r--r-- 1 mark admin 1487 2 Aug 10:00 COPYING
-rw-r--r-- 1 mark admin 376 9 Aug 10:34 INSTALL_RECEIPT.json
-rw-r--r-- 1 mark admin 6834 2 Aug 10:00 README.md
drwxr-xr-x 8 mark admin 272 2 Aug 10:00 bin
-rw-r--r-- 1 mark admin 785 9 Aug 10:34 homebrew.mxcl.redis.plist
/usr/local/Cellar/redis/3.2.3/bin:
total 3440
-r-xr-xr-x 1 mark admin 67668 2 Aug 10:00 redis-benchmark
-r-xr-xr-x 1 mark admin 13936 2 Aug 10:00 redis-check-aof
-r-xr-xr-x 1 mark admin 768704 2 Aug 10:00 redis-check-rdb
-r-xr-xr-x 1 mark admin 129712 2 Aug 10:00 redis-cli
lrwxr-xr-x 1 mark admin 12 2 Aug 10:00 redis-sentinel -> redis-server
-r-xr-xr-x 1 mark admin 768704 2 Aug 10:00 redis-server
The structure shows the package primarily contains license files, README documentation, and six binary files, with redis-sentinel being a symbolic link to redis-server. This installation approach doesn't introduce numerous services or configuration files, resulting in minimal disk space usage.
Third-Party Homebrew Tap Approach
The community provides specialized taps for redis-cli installation through these steps:
brew tap ringohub/redis-cli
brew update && brew doctor
brew install redis-cli
This method directly installs the redis-cli tool, avoiding the complete Redis package installation, making it suitable for scenarios requiring only the command-line client.
Docker Container Solution
For temporary usage or testing scenarios, redis-cli can be run via Docker without local installation:
docker run --rm -it redis:alpine redis-cli -h 192.168.0.8
Replace 192.168.0.8 with the actual Redis server IP address. The --rm parameter ensures automatic cleanup after container exit, while -it provides an interactive terminal.
npm Installation Method
The JavaScript version of redis-cli can be installed via Node.js package manager npm:
npm install -g redis-cli
After installation, use the rdcli command:
$ rdcli
127.0.0.1:6379> keys incident::sequence
1) incident::sequence
127.0.0.1:6379> GET incident::sequence
570
127.0.0.1:6379> config get dir
1) dir
2) /data
127.0.0.1:6379> exit
This approach is suitable for Node.js development environments, though functionality may be less comprehensive than the native version.
Redis Protocol and Custom Client Implementation
Redis employs a simple text protocol, and understanding its basic principles facilitates custom client development. Below is a minimal Redis client implementation in bash:
#!/bin/bash
################################################################################
# redis.sh
# Very, very simplistic Redis client in bash
# Mark Setchell
# Usage:
# redis.sh SET answer 42
#
# Ref: https://redis.io/topics/mass-insert
################################################################################
if [ $# -lt 2 ] ; then
echo "Usage: redis.sh SET answer 42" >&2
exit 1
fi
# Build protocol string
protocol="*$#\r\n"
for var in "$@" ; do
protocol+="$"
protocol+="${#var}\r\n${var}\r\n"
done
# Send to Redis on default port on local host - but you can change it
printf "$protocol" > /dev/tcp/localhost/6379
This script implements the basic Redis protocol format: starting with * to indicate array element count, each parameter begins with $ followed by length and actual value, using \r\n as separators. TCP connection is established via the /dev/tcp special file.
Technical Solution Comparison and Selection Recommendations
Different solutions have distinct advantages: Homebrew complete installation suits users needing full Redis functionality; third-party taps provide the purest redis-cli installation; Docker solution is ideal for temporary usage and testing environments; npm approach fits Node.js developers; custom implementations help deepen understanding of the Redis protocol. Selection should consider usage scenarios, environment configuration, and technical requirements.