Databases 7 min read

Master Redis: Essential Commands for Data Management and Persistence

This guide walks through Redis fundamentals, covering how data persists after restarts, core key operations, database selection, serialization, expiration handling, key movement, random retrieval, renaming, and how to check the number of configured databases, all with practical command examples and visual illustrations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Redis: Essential Commands for Data Management and Persistence

Redis Restart and Cache Clearing

After a Redis restart the data remains because Redis uses persistence (RDB and optionally AOF). To log in use redis-cli -h IP -p port and clear all data with flushall.

Basic Key Operations

Set a key: set keyname "value". Retrieve with get keyname. Delete with del keyname. Example: set test 123.

Database Selection

Switch databases with select 7 or select 10. List keys in the current DB using keys *. The default DB is 0; return to it with select 0.

Numeric Increment

Increase a numeric key value by 1 using incr keyname (or incrby keyname increment).

Serialization

Serialize a key’s value with dump keyname. Deserialization is performed by Redis when loading data.

Key Expiration

Set expiration in seconds: expire keyname 10. Set expiration by Unix timestamp: expireat keyname 1639116230. Set expiration in milliseconds: pexpire keyname 2000. Set expiration by millisecond timestamp: pexpireat keyname 1639116950000. Remove expiration with persist keyname.

Checking Expiration

Get remaining time to live in seconds with ttl keyname and in milliseconds with pttl keyname. Return values: -1 (no expiration), -2 (key does not exist), positive integer (remaining time).

Key Existence and Pattern Search

Check if a key exists: exists keyname (returns 1 if exists, 0 otherwise). Find keys matching a pattern with keys test*. List all keys with keys *.

Moving Keys Between Databases

Move a key to another DB with move keyname db-index. Returns 1 on success, 0 if the target DB already has a key with the same name or the source key does not exist.

Random Key Retrieval

Get a random key from the current DB using randomkey. Returns nil (or null on Windows) if the DB is empty.

Renaming Keys

Rename a key with rename oldkey newkey. If newkey already exists it will be overwritten. Use renamenx oldkey newkey to rename only when newkey does not exist.

Number of Databases

Redis typically provides 16 databases. Check the configuration file ( /etc/redis/redis.conf) or run config get databases after logging in.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseredisPersistencekey management
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.