Redis: Redis Quick Start
After installing Redis, let's get started quickly and learn basic Redis operations.
1. Starting the Redis Server
(1) Foreground Startup
The simplest way to start Redis — it runs in the foreground and logs directly to the terminal.
redis-server
Output:
[Redis server starting...]
Redis 7.0.11 (00000000/0) 64 bit
Running in standalone mode
Port: 6379
PID: 12345
Ready to accept connections
Ctrl+C to stop the Redis server.
(2) Background Startup
Running in the background is recommended for production environments.
# Method 1: Use daemonize parameter
redis-server --daemonize yes
# Method 2: Edit config file
# Edit /etc/redis/redis.conf
daemonize yes
# Then start
redis-server /etc/redis/redis.conf
(3) Start on a Specific Port
# Use port 6380
redis-server --port 6380
(4) Check if Redis is Running
# Linux
ps aux | grep redis-server
# Or use redis-cli
redis-cli ping
# Returns PONG if Redis is running
2. Connecting to Redis
(1) Using redis-cli
redis-cli is Redis's built-in command-line client.
# Connect to local Redis (default 127.0.0.1:6379)
redis-cli
# After connecting, the prompt changes to:
127.0.0.1:6379>
(2) Specifying Host and Port
# Connect to a specific host and port
redis-cli -h 192.168.1.100 -p 6379
# Connect to a different local port
redis-cli -p 6380
(3) Connecting with a Password
# Method 1: Command-line argument
redis-cli -a yourpassword
# Method 2: Authenticate after connecting
redis-cli
AUTH yourpassword
OK
-a parameter exposes the password in command history. For production, use the AUTH command or config file instead.
(4) Connecting via Unix Socket
redis-cli -s /tmp/redis.sock
(5) Exiting redis-cli
# Exit the connection
exit
# or
quit
# or press Ctrl+D
3. First Command: PING
The PING command tests whether the connection is working, similar to the network ping command.
▶ Example: Test Connection
PING
If Redis is working properly, it returns PONG.
(1) PING with a Message
PING "Hello"
"Hello"
PING can carry a message, and Redis returns it as-is.
4. Basic Commands: SET and GET
SET and GET are the most commonly used commands for storing and retrieving string data.
(1) SET: Store Data
The SET command sets a key-value pair.
▶ Example: Basic SET
SET name "Redis Tutorial"
GET name
▶ Example: With Expiration
# Set a key that expires in 10 seconds
SET temp "temporary" EX 10
GET temp
# Or use PX for milliseconds
SET temp2 "temp" PX 5000
GET temp2
▶ Example: Set Only When Key Doesn't Exist
# NX: Only set if key does not exist
SET name "New Name" NX
# XX: Only set if key exists
SET name "Updated Name" XX
GET name
(2) SET Command Full Syntax
SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT timestamp|PXAT timestamp]
| Parameter | Description |
|---|---|
| NX | Only set if key does not exist |
| XX | Only set if key exists |
| GET | Return the old value (Redis 6.2+) |
| EX seconds | Set expiration in seconds |
| PX milliseconds | Set expiration in milliseconds |
| EXAT timestamp | Set expiration Unix timestamp (seconds) |
| PXAT timestamp | Set expiration Unix timestamp (milliseconds) |
(3) GET: Read Data
The GET command retrieves the value of a key.
▶ Example: Basic GET
GET name
# Key doesn't exist, returns nil
GET notexist
▶ Example: GETSET (Get Old Value, Set New Value)
GETSET name "New Value"
GET name
5. Other Basic Commands
(1) DEL: Delete a Key
SET temp "delete me"
OK
DEL temp
(integer) 1 # Deletion successful, returns number of deleted keys
DEL temp
(integer) 0 # Key doesn't exist, 0 deleted
(2) EXISTS: Check if Key Exists
SET name "Redis"
OK
EXISTS name
(integer) 1 # Exists
EXISTS notexist
(integer) 0 # Doesn't exist
(3) KEYS: Find Keys
# Set some test data
SET user:1 "Alice"
SET user:2 "Bob"
SET product:1 "iPhone"
# Find all keys (use with caution!)
KEYS *
1) "user:1"
2) "user:2"
3) "product:1"
4) "name"
# Find keys matching a pattern
KEYS user:*
1) "user:1"
2) "user:2"
KEYS *:1
1) "user:1"
2) "product:1"
(4) FLUSHDB: Clear Current Database
# Clear all data in the current database (dangerous!)
FLUSHDB
OK
(5) DBSIZE: Count Keys
DBSIZE
(integer) 4 # The current database has 4 keys
6. Command Return Types
Redis commands return different types of results:
| Return Example | Type | Description |
|---|---|---|
OK |
Simple string | Operation successful |
"value" |
String | String value |
(integer) 1 |
Integer | Numeric result |
1) "a"``<br>``2) "b" |
Array | List or set |
(nil) |
Null | Key doesn't exist or no result |
# Error |
Error | Command execution failed |
7. Batch Executing Commands
(1) Interactive Mode
Enter commands one by one in redis-cli:
SET a 1
OK
SET b 2
OK
GET a
"1"
(2) Pipeline Mode
Execute commands from a file in batch:
# Create a command file commands.txt
echo "SET key1 value1" > commands.txt
echo "SET key2 value2" >> commands.txt
echo "GET key1" >> commands.txt
# Execute batch
cat commands.txt | redis-cli
Output:
OK
OK
"value1"
(3) Direct Command-Line Execution
# Execute a single command directly
redis-cli SET mykey "myvalue"
# Output: OK
redis-cli GET mykey
# Output: "myvalue"
# Execute multiple commands
redis-cli << EOF
SET a 1
SET b 2
GET a
GET b
EOF
8. Using the Help System
Redis has a built-in comprehensive help system.
(1) View Command Help
HELP SET
SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]
summary: Set the string value of a key
since: 1.0.0
group: string
(2) View Command Groups
HELP @string
# Lists all string commands
(3) Tab Completion
redis-cli supports Tab key for command completion:
SET<press Tab>
# Auto-completes to SET
9. Useful Tips
(1) View Server Info
INFO
# Returns extensive server information
# View specific sections
INFO server
INFO memory
INFO stats
(2) View Configuration
# View all config
CONFIG GET *
# View specific config
CONFIG GET maxmemory
1) "maxmemory"
2) "0"
(3) Monitor Commands
# Real-time monitoring of all executed commands
MONITOR
OK
# Then displays all commands from all clients
(4) View Client Connections
CLIENT LIST
id=1 addr=127.0.0.1:52345 fd=8 name= age=10 idle=0 ...
❓ FAQ
ps aux | grep redis), verify the port is correct, and check firewall settings.📖 Summary
- redis-server starts the Redis server, redis-cli connects to it
- PING tests the connection, SET/GET stores and retrieves strings
- DEL deletes keys, EXISTS checks if a key exists
- KEYS finds keys (caution in production), DBSIZE counts keys
- Use HELP for command help, Tab key for completion
- MONITOR watches commands, INFO shows server info
📝 Exercises
- Startup practice: Start the Redis server, connect with redis-cli, and run the PING command
- Basic operations: Use SET to store your name and age, then use GET to retrieve them
- Expiration practice: Set a key with expiration, wait for it to expire, then use GET to verify
- Batch operations: Create a text file with multiple Redis commands and execute them using pipeline mode
10. Next Lesson
In the next lesson, we will learn Redis Configuration, covering the Redis config file and common configuration options.