Redis - Keys
Redis - Managing Keys
Section titled “Redis - Managing Keys”Keys are the fundamental unit of the Redis data model. All data is stored and retrieved via a key. This section covers the essential commands for managing the lifecycle of keys in your database.
Best Practice: Key Naming Conventions
Section titled “Best Practice: Key Naming Conventions”While Redis allows almost any binary sequence as a key, adopting a consistent naming convention is crucial for maintainability. A common pattern is object-type:id:field.
- Example:
user:1001:profile - Benefits: Improves readability, helps organize keys, and prevents naming collisions.
Basic Key Operations: A Walkthrough
Section titled “Basic Key Operations: A Walkthrough”Let’s walk through a common sequence of key operations using redis-cli.
# 1. Set a key for a user's session ID. The 'EX' option sets an expiry of 60 seconds.127.0.0.1:6379> SET session:user:42 'a_long_session_string' EX 60OK
# 2. Check if the key exists.127.0.0.1:6379> EXISTS session:user:42(integer) 1
# 3. Check its remaining time to live (in seconds).127.0.0.1:6379> TTL session:user:42(integer) 55
# 4. We can make the key permanent by removing its expiration.127.0.0.1:6379> PERSIST session:user:42(integer) 1
# 5. Now, its TTL is -1, indicating it will not expire.127.0.0.1:6379> TTL session:user:42(integer) -1
# 6. Let's rename the key for archival purposes.127.0.0.1:6379> RENAME session:user:42 archived:session:user:42OK
# 7. Verify the old key no longer exists.127.0.0.1:6379> EXISTS session:user:42(integer) 0
# 8. Finally, delete the key.127.0.0.1:6379> DEL archived:session:user:42(integer) 1Common Key Management Commands
Section titled “Common Key Management Commands”Here is a list of essential commands for key management.
| Command | Description |
|---|---|
| DEL key [key …] | Deletes one or more specified keys. Returns the number of keys deleted. |
| EXISTS key [key …] | Checks if one or more keys exist. Returns the count of existing keys. |
| EXPIRE key seconds | Sets a key’s time to live (TTL) in seconds. |
| PEXPIRE key milliseconds | Sets a key’s time to live (TTL) in milliseconds. |
| TTL key | Returns the remaining time to live of a key in seconds. |
| PTTL key | Returns the remaining time to live of a key in milliseconds. |
| PERSIST key | Removes the expiration from a key, making it permanent. |
| RENAME key newkey | Renames a key. Overwrites newkey if it already exists. |
| RENAMENX key newkey | Renames a key, only if newkey does not already exist. |
| TYPE key | Returns the string representation of the data type stored at the key (e.g., ‘string’, ‘list’, ‘hash’). |
| RANDOMKEY | Returns a random key name from the database. Use with caution on large databases. |
| TOUCH key [key …] | Updates the last access time of a key. Does not modify the value. Useful for certain eviction policies. |
Searching for Keys: SCAN vs. KEYS
Section titled “Searching for Keys: SCAN vs. KEYS”The Danger of the KEYS Command
Section titled “The Danger of the KEYS Command”The KEYS command, e.g., KEYS user:*, finds all keys matching a pattern. WARNING: KEYS is a blocking operation that scans the entire keyspace. Running it on a production database can cause it to become unresponsive for a long time. It should be avoided in production environments.
The Modern Solution: SCAN
Section titled “The Modern Solution: SCAN”SCAN is the recommended, non-blocking alternative. It allows you to incrementally iterate over the keyspace, returning a small batch of keys with each call along with a new cursor. This prevents the server from freezing.
# Start a scan from cursor 0. MATCH finds keys with the 'user:' prefix.127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 1001) "1720"2) 1) "user:1001:profile" 2) "user:42" 3) "user:99"
# The next call uses the new cursor '1720' returned by the previous one.127.0.0.1:6379> SCAN 1720 MATCH user:* COUNT 1001) "0"2) 1) "user:500"
# The scan is complete when the returned cursor is '0'.