Redis, a popular in-memory data structure store, can be a fantastic tool for caching, session management, and more. Installing it locally with Homebrew is a breeze, but understanding why it works the way it does is key to leveraging its power.
Here’s how you get Redis up and running on your Mac using Homebrew, and then we’ll dive into what’s actually happening under the hood.
First, if you don’t have Homebrew installed, open your terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the on-screen prompts. Once Homebrew is set up, install Redis with:
brew install redis
This command downloads the latest stable version of Redis and its dependencies, then compiles and installs them. Homebrew also sets up a default configuration file for you.
To start the Redis server as a background service (daemon), run:
brew services start redis
You’ll see output indicating that the service has started. To verify it’s running, you can check its status:
brew services list
You should see redis listed with started next to it.
Now, let’s interact with your local Redis instance. Open another terminal window and run the Redis CLI (Command Line Interface):
redis-cli
This connects you to your running Redis server. You’re now in an interactive prompt that looks like 127.0.0.1:6379>.
Let’s try a simple SET and GET command:
SET mykey "hello world"
The server will respond with OK. Now, retrieve the value:
GET mykey
You should see "hello world". To exit the Redis CLI, type exit or press Ctrl+C.
When you installed Redis via brew install redis, Homebrew didn’t just copy an executable to your bin directory. It also managed the creation of a plist file that tells launchd, macOS’s system service manager, how to run and manage the Redis server. This plist file is typically located in ~/Library/LaunchAgents/. The brew services start redis command registers this plist with launchd, ensuring Redis starts automatically on login and can be managed with brew services.
The default configuration file for Redis, usually found at /usr/local/etc/redis.conf (or similar, depending on your Homebrew prefix), is what dictates how your Redis server behaves. This file controls everything from the port it listens on (default is 6379) to memory limits, persistence options, and security settings. When you run redis-cli, it defaults to connecting to 127.0.0.1 on port 6379, the standard address for a local Redis instance.
The SET command in Redis stores a key-value pair. The key is mykey (a string), and the value is "hello world" (also a string). Redis stores this in its memory for extremely fast retrieval. The GET command then looks up the value associated with mykey and returns it. Because Redis operates entirely in RAM, these operations are incredibly quick, often measured in microseconds.
A crucial, yet often overlooked, aspect of Redis is its persistence. By default, the Redis server installed via Homebrew might have basic persistence enabled, but it’s not always configured for robust data safety out-of-the-box. The configuration file (redis.conf) contains directives like save (for RDB snapshots) and appendonly (for AOF logging). Understanding and configuring these is vital if you need your data to survive server restarts or crashes. For instance, save 900 1 means "save the dataset to disk if at least 1 change has occurred in 900 seconds," and appendonly yes enables the append-only file, which logs every write operation.
With your local Redis instance running and understood, the next logical step is exploring how to connect to it from an application, or perhaps setting up replication for high availability.