Redis - Connections
Redis - Connection Management
Section titled “Redis - Connection Management”Connection management commands handle the lifecycle and security of client connections to the Redis server. Proper authentication and client identification are crucial for a secure and observable system.
Authentication
Section titled “Authentication”Protecting a Redis instance is a critical first step. Redis offers two primary models for authentication.
Legacy Authentication: AUTH
Section titled “Legacy Authentication: AUTH”The traditional method involves a single server-wide password configured in redis.conf with the requirepass directive. Clients must then authenticate using the AUTH command before they can execute any other commands.
127.0.0.1:6379> AUTH "your-secret-password"OK127.0.0.1:6379> PINGPONGIf the wrong password is provided, Redis will return an error and close the connection.
Modern Authentication: Access Control Lists (ACL)
Section titled “Modern Authentication: Access Control Lists (ACL)”Introduced in Redis 6, Access Control Lists (ACLs) are the modern, recommended way to handle security. ACLs allow you to create multiple users, each with different passwords and fine-grained permissions (e.g., read-only access, access to specific commands, or access to specific key patterns).
Here’s a conceptual example of creating a limited-access user via redis-cli:
# Connect as an admin user first127.0.0.1:6379> ACL SETUSER analytics_user >somepassword ON +@read -@all ~user:*OKThis command creates a user named analytics_user with a password. This user can only perform read commands (+@read) on keys that start with the user: prefix (~user:*) and is denied all other commands (-@all). A client would then authenticate with AUTH analytics_user somepassword.
Key Connection Commands
Section titled “Key Connection Commands”Here is a list of fundamental commands for managing connections.
| Command | Description & Best Practices |
|---|---|
| AUTH | Authenticates the client using ACLs (Redis 6+). The older AUTH <password> syntax is for legacy authentication. |
| PING [message] | Checks server responsiveness. A fundamental health check. Returns PONG or the optional message. |
| ECHO | Returns the provided message. Useful for simple connection tests or debugging scripts. |
| CLIENT LIST | Lists all active client connections. Essential for monitoring and identifying problematic clients. |
| CLIENT SETNAME | Assigns a name to the current connection, which appears in CLIENT LIST. Best practice for identifying application connections. |
| SELECT | Selects a logical database (numbered 0-15 by default). Best Practice: This is often discouraged in modern applications. For data separation, prefer using separate Redis instances (e.g., in different Docker containers) or a consistent key-prefixing naming scheme (e.g., orders:123, sessions:xyz). This avoids hidden dependencies and simplifies scaling. |
| QUIT | Closes the current connection gracefully. |