Skip to content

Redis - 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.

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.

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 60
OK
# 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:42
OK
# 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) 1

Here is a list of essential commands for key management.

CommandDescription
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 secondsSets a key’s time to live (TTL) in seconds.
PEXPIRE key millisecondsSets a key’s time to live (TTL) in milliseconds.
TTL keyReturns the remaining time to live of a key in seconds.
PTTL keyReturns the remaining time to live of a key in milliseconds.
PERSIST keyRemoves the expiration from a key, making it permanent.
RENAME key newkeyRenames a key. Overwrites newkey if it already exists.
RENAMENX key newkeyRenames a key, only if newkey does not already exist.
TYPE keyReturns the string representation of the data type stored at the key (e.g., ‘string’, ‘list’, ‘hash’).
RANDOMKEYReturns 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.

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.

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 100
1) "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 100
1) "0"
2) 1) "user:500"
# The scan is complete when the returned cursor is '0'.