Databases 10 min read

Redis Basics: Overview, Installation, and Common Commands

This article introduces Redis as a high‑performance, in‑memory NoSQL database, explains why learning it is valuable, provides step‑by‑step installation instructions, and presents essential commands for keys, strings, lists, sets, sorted sets, and hashes with practical code examples.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Redis Basics: Overview, Installation, and Common Commands

Table of Contents

What is Redis?

Redis Installation

Basic Command Overview

What is Redis?

Redis is a C‑written, distributed, high‑performance, non‑relational (NoSQL) database that offers extremely high throughput, often reaching tens of thousands of operations per second. Because it stores data primarily in memory, it is best suited for scenarios with high concurrency and relatively small data size, unless the cost of memory is not a concern.

There are two main reasons to learn Redis: to solve real‑world business problems and to improve job prospects at large technology companies.

Redis Installation

Download URL: http://download.redis.io/releases/

$ ls
redis-4.0.10.tar.gz
# Decompress
$ tar -zxvf redis-4.0.10.tar.gz
# Run tests
$ sudo make test
# Compile and install
$ sudo make install
# Start Redis in the background
$ ./redis-server ~/software/redis-4.0.10/redis.conf &
# Check Redis process
$ ps -ef | grep redis
# Connect to Redis (default port 6379)
$ ./redis-cli -h localhost -p 6379

Basic Command Overview

Key Operations

# Check if a key exists
$ localhost:6379> EXISTS hash1
(integer) 1
$ localhost:6379> EXISTS jjj
(integer) 0
# Delete a key
$ localhost:6379> DEL hash1
(integer) 1
# Set a key with expiration (5 seconds)
$ localhost:6379> set t1 value ex 5
OK
# Get remaining TTL
$ localhost:6379> TTL t1
(integer) 7
# Get key type
$ localhost:6379> type t1
string

String (KV) Operations

# Set a key
$ localhost:6379> set key1 value1
OK
# Get the value
$ localhost:6379> get key1
"value1"
# Set with expiration (seconds)
$ localhost:6379> set key2 value2 EX 5
OK
# Set only if key does not exist
$ localhost:6379> set key3 value3 NX
OK
# Attempt to set existing key with NX (returns nil)
$ localhost:6379> set key3 value3 NX
(nil)

List Operations

# Push to the left
$ localhost:6379> LPUSH list1 1
(integer) 1
# Push to the right
$ localhost:6379> RPUSH list1 2
(integer) 2
# Retrieve all elements
$ localhost:6379> LRANGE list1 0 -1
1) "1"
2) "2"
# Get list length
$ localhost:6379> LLEN list1
(integer) 2
# Get element by index
$ localhost:6379> LINDEX list1 0
"2"
# Pop from left
$ localhost:6379> LPOP list1
"5"
# Pop from right
$ localhost:6379> RPOP list1
"1"
# Move element from right of one list to left of another
$ localhost:6379> RPOPLPUSH list1 list2
"one"
$ localhost:6379> LRANGE list2 0 -1
1) "one"

Set Operations

# Add members to a set
$ localhost:6379> SADD set1 'one' 'two' 'three'
(integer) 3
# Retrieve all members
$ localhost:6379> SMEMBERS set1
1) "one"
2) "three"
3) "two"
# Remove a member
$ localhost:6379> SREM set1 'one'
(integer) 1
# Pop a random member (without deletion)
$ localhost:6379> SRANDMEMBER set1 1
1) "one"
# Check membership
$ localhost:6379> SISMEMBER set1 'one'
(integer) 1
$ localhost:6379> SISMEMBER set1 '4'
(integer) 0

Sorted Set Operations

# Add a member with a score
$ localhost:6379> ZADD zset1 1 'one'
(integer) 1
# Get members in score order
$ localhost:6379> ZRANGE zset1 0 -1
1) "one"
# Remove a member
$ localhost:6379> ZREM zset1 'one'
(integer) 1
# Get number of members
$ localhost:6379> ZCARD zset1
(integer) 2
# Increment a member's score
$ localhost:6379> ZINCRBY zset1 2 "one"
"4"
# Get rank of a member (higher score = higher rank)
$ localhost:6379> ZRANK zset1 'one'
(integer) 2
# Get reverse rank (lower index = higher score)
$ localhost:6379> ZREVRANK zset1 'one'
(integer) 0
# Get a member's score
$ localhost:6379> ZSCORE zset1 'one'
"4"

Hash Operations

# Set a field
$ localhost:6379> HSET hash1 field1 1
(integer) 1
# Set multiple fields
$ localhost:6379> HMSET hash1 field2 2 field3 3
OK
# Get a field's value
$ localhost:6379> HGET hash1 field1
"1"
# Get multiple fields
$ localhost:6379> HMGET hash1 field1 field2 field3
1) "1"
2) "2"
3) "3"
# Get all fields and values
$ localhost:6379> HGETALL hash1
1) "field1"
2) "1"
3) "field2"
4) "2"
5) "field3"
6) "3"
# Check if a field exists
$ localhost:6379> HEXISTS hash1 field1
(integer) 1
# Delete fields
$ localhost:6379> HDEL hash1 field1 field2
(integer) 2
# Increment a field's value
$ localhost:6379> HINCRBY hash1 field1 2
(integer) 3
# Get all keys in the hash
$ localhost:6379> HKEYS hash1
1) "field1"
2) "field2"
3) "field3"
# Get hash length
$ localhost:6379> HLEN hash1
(integer) 5

Summary

The article covered the most frequently used Redis commands for production environments and provided links to the official command reference (https://redis.io/commands) and the Chinese documentation site (http://www.redis.cn/commands.html#). The next article will explore Redis's underlying implementation to explain why it is so fast.

DatabaseRedisIn-MemoryInstallationNoSQLCommands
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

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