Databases 39 min read

Mastering Redis: Core Data Structures, Commands, and Performance Tuning

This comprehensive guide introduces Redis fundamentals, explores its diverse data structures and essential commands, and delves into advanced topics such as persistence, memory management, pipelining, transactions, clustering, and Java client choices, offering practical tips for deployment, optimization, and architectural decisions.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
Mastering Redis: Core Data Structures, Commands, and Performance Tuning

Redis is an open‑source, in‑memory data store that can serve as a database, cache, or message broker. It supports multiple data structures—including strings, hashes, lists, sets, sorted sets, bitmaps, and HyperLogLog—and provides features such as LRU eviction, transactions, and high‑availability via replication, Sentinel, and Cluster.

Overview

Redis operates on a single‑threaded, non‑blocking I/O model, making all operations atomic and thread‑safe. Commands are generally O(1) or O(N) in complexity, so using high‑cost commands (e.g., KEYS) in production is discouraged.

Data Structures and Common Commands

Key

Avoid excessively long keys; the maximum key size is 512 MB.

Prefer readable, namespaced keys such as object-type:id:attr.

String

SET – set a value with optional expiration and existence flags (O(1)).

GET – retrieve a value (O(1)).

INCR / INCRBY / DECR / DECRBY – atomic integer increment/decrement (O(1)).

Example: Inventory control

SET inv:remain "100"
DECR inv:remain

If DECR returns a non‑negative number, stock is sufficient; a negative result indicates depletion.

Example: Sequence generation

SET sequence "10000"
INCR sequence
INCRBY sequence 100

List

LPUSH / RPUSH – insert elements at the head or tail (O(N) for multiple inserts).

LPOP / RPOP – remove and return an element from head or tail (O(1)).

LRANGE – retrieve a range of elements (O(N)). Use with caution on large lists.

Hash

HSET / HGET – set or get a field (O(1)).

HMSET / HMGET – batch set/get fields (O(N)).

Avoid HGETALL on large hashes; use HSCAN instead.

Set

SADD / SREM – add or remove members (O(N)).

SISMEMBER – test membership (O(1)).

Avoid full scans with SMEMBERS on large sets; prefer SSCAN .

Sorted Set

ZADD / ZREM – add or remove members with scores (O(log N)).

ZRANK / ZREVRANK – get rank of a member (O(log N)).

Do not issue range queries that traverse the entire set; use ZSCAN or limit the result set.

Bitmap & HyperLogLog

Bitmaps treat strings as bit arrays for compact true/false storage. HyperLogLog provides cardinality estimation with minimal memory usage.

Persistence

Redis offers two persistence mechanisms: RDB snapshots and AOF logs.

RDB

Snapshots are created by forking a child process; impact on performance is minimal.

Configuration example: save 60 10000 (snapshot if 10 000 changes occur within 60 s).

Pros: fast recovery, low runtime overhead.

Cons: possible data loss between snapshots.

AOF

Every write operation is appended to a log file.

Three appendfsync policies: always (safest, slowest), everysec (balanced), no (fastest).

AOF rewrite ( BGREWRITEAOF) compacts the log.

Pros: higher durability; Cons: larger files and slower recovery.

Memory Management & Eviction

Set maxmemory to limit Redis RAM usage.

Choose an eviction policy (e.g., volatile-lru is recommended).

Be aware of memory consumption by replication buffers.

Pipelining

Pipelining batches multiple commands in a single network round‑trip, reducing latency. It works only for independent commands; dependent commands require scripting.

Transactions & Scripting

Use MULTI / EXEC for atomic command groups. Redis does not support rollback; syntax errors abort the transaction, runtime errors do not.

Optimistic locking can be achieved with WATCH + EXEC.

Lua scripts ( EVAL / EVALSHA) provide a more powerful alternative to transactions.

Performance Tuning

Avoid long‑running commands (e.g., KEYS, large HGETALL, SUNION).

Use pipelining and long‑lived connections.

Disable Transparent Huge Pages on the OS.

Monitor latency with SLOWLOG and INFO latest_fork_usec.

Prevent swap usage; ensure sufficient RAM.

Replication & Cluster

Master‑Slave Replication

One master handles writes; slaves replicate data and can serve read traffic. Sentinel provides automatic failover.

Redis Cluster

Data is sharded across 16384 hash slots. Clients can connect to any node; the cluster redirects requests to the correct slot.

Transactions, pipelining, and Lua scripts must operate on keys that reside in the same slot (use hash tags to enforce this).

Java Client Selection

Officially recommended clients: Jedis, Redisson, and Lettuce.

Jedis – lightweight, supports connection pooling, pipelining, transactions, scripting, Sentinel, and Cluster; lacks built‑in read‑write splitting.

Redisson – Netty‑based, asynchronous, high‑performance, supports read‑write splitting, pipelining, scripting, Sentinel, and Cluster; does not support transactions (Lua is recommended).

Choose the client based on required features and complexity.

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.

redisperformance tuningPersistenceData StructuresJava Client
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

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.