Databases 15 min read

Master Redis: Core Concepts, Commands, and Data Types Explained

Redis is an open‑source, in‑memory data‑structure store that serves as a database, cache, and message broker; this guide introduces its fundamentals, explains why NoSQL emerged, details its five primary data types, common commands, and advanced features like transactions and pipelines.

Efficient Ops
Efficient Ops
Efficient Ops
Master Redis: Core Concepts, Commands, and Data Types Explained

What Is Redis?

Redis is an open‑source, in‑memory data‑structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes.

Redis also provides built‑in replication, Lua scripting, LRU eviction, transactions, and multiple persistence levels, and offers high availability through Sentinel and automatic sharding with Cluster.

What Is NoSQL?

Redis is a NoSQL (non‑relational) database. NoSQL emerged to address the limitations of single‑machine relational databases when traffic grows.

The Era of Single‑Machine Databases

When a website’s traffic is low, a single database can handle the load.

Cache + Sharding

As traffic increases, a single database becomes insufficient. Adding a cache layer and splitting the database into clusters, optimizing schema, and separating reads and writes improves performance.

The cache is a form of NoSQL; Redis, however, offers many more functions beyond caching, such as simple message queues, session sharing, counters, leaderboards, and friend‑relationship handling.

Redis General Commands

Below are common commands that are independent of data types.

Key‑Value Operations

Redis stores data as key‑value pairs, each key being unique.

For demonstration, string‑related commands are used.

keys pattern – returns all keys matching the pattern (O(n)); use scan in production.

dbsize – returns the number of keys stored.

exists key – checks if a key exists.

del key – deletes a key.

type key – returns the data type of a key.

rename key newkey – renames a key.

Expiration

Because many Redis values are used as cache, they often need an expiration time.

expire key seconds – set expiration.

ttl key – view remaining time; returns -1 for permanent, -2 for expired or non‑existent.

Redis’s Five Basic Data Types

Redis provides five fundamental data structures: strings, hashes, lists, sets, and sorted sets.

String

Strings are the most basic type and are essential in most programming languages.

set key value – set a value.

get key – retrieve a value.

mset … – atomic batch set.

mget … – atomic batch get.

incr key – increment.

decr key – decrement.

incrby key value – increment by a number.

decrby key value – decrement by a number.

incrbyfloat key floatvalue – increment by a floating‑point number.

setnx key value – set if not exists (used for distributed locks).

set key value xx – set only if key exists (update).

Hash

A hash is similar to a small Redis or a Java HashMap, implemented with an array of linked lists.

Redis hashes store only string values and use progressive rehashing.

During rehashing, both old and new hash tables are kept, and lookups check both until migration finishes.

Basic hash operations:

hset key field value – set a field.

hsetnx key field value – set if field does not exist.

hmset key field1 value1 … – batch set.

hget key field – get a field.

hmget key field1 field2 … – batch get.

hgetall key – get all fields.

hdel key field – delete a field.

hexists key field – check existence.

hlen key – number of fields.

hvals key – all values.

hkeys key – all fields.

hincrby key field increment – increment a numeric field.

hincrbyfloat key field float – increment by float.

List

Redis lists are implemented as doubly linked lists, offering fast insert/delete but slow index lookup.

Lists can be used as stacks or queues.

lpush key item1 item2 … – push to the left.

rpush key item1 item2 … – push to the right.

lpop key – pop from the left.

rpop key – pop from the right.

lindex key index – get element by index (O(n)).

lrange key start end – get a range of elements (O(n)).

linsert key BEFORE|AFTER pivot element – insert relative to an element.

lrem key count value – remove elements equal to value.

ltrim key start end – keep only a range.

lset key index newvalue – set element at index.

blpop key timeout – block until element available (0 = forever).

brpop key timeout – block from the right.

Set

Sets are unordered collections of unique elements, similar to Java’s HashSet.

sadd key value – add element.

srem key value – remove element.

sismember key value – check membership.

srandmember key count – get random elements without removal.

spop key count – pop random elements.

smembers key – get all members (O(n)).

scard key – get cardinality.

sinter set1 set2 … – intersection.

sdiff set1 set2 … – difference.

sunion set1 set2 … – union.

Sorted Set (ZSet)

Sorted sets store unique members with a score, enabling ordered queries such as leaderboards.

ZSets are implemented with skip‑lists, which provide fast ordered insertion and range queries.

Redis Transactions and Pipelines

Pipeline

Pipelines batch multiple commands into a single network round‑trip, reducing latency. Commands in a pipeline are not executed atomically and may be interleaved with other clients.

Transaction

Redis transactions guarantee atomicity and isolation but not full ACID; they lack rollback.

A transaction is essentially a pipeline with an atomic execution guarantee.

multi – start transaction.

exec – execute transaction.

discard – cancel queued commands.

watch key – monitor a key for changes before committing.

unwatch – stop monitoring.

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.

CachedatabaseredisData StructuresNoSQLcommands
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

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.