Redis Unveiled: What a Memory‑Based Key‑Value Database Really Looks Like
This article walks through Redis fundamentals, explaining its definition, internal modules, the full processing path of a SET command, essential commands, and how Redis differs from Memcached and MySQL, while also covering common connection and performance pitfalls.
What is Redis
Redis (Remote Dictionary Server) is a memory‑based key‑value database that supports multiple data structures (String, List, Hash, Set, Sorted Set, …). It provides microsecond‑level latency because all operations run in memory, and it can persist data to disk using AOF or RDB to avoid loss on restart.
Internal modules
Network framework layer : event‑driven I/O that accepts client connections, reads commands and writes responses.
Command processing layer : parses the RESP protocol, maps a command to its handler function.
Index module : a global hash table ( dict) that stores key → value‑address mappings, enabling O(1) lookups.
Storage module : holds the actual value objects (String, List, Hash, …) using the appropriate internal data structures.
Additional feature modules : persistence (AOF/RDB), replication, clustering, transactions, etc.
Common commands
PING– test connectivity; returns PONG. SET key value – write a string key; most used write command. GET key – read a string key; returns (nil) if the key does not exist. EXISTS key – check key existence; returns 1 if present, 0 otherwise. TYPE key – show the data type of the value; useful for debugging. EXPIRE key seconds – set a TTL on a key; cache entries usually have an expiration. TTL key – show remaining TTL; -1 means no expiration, -2 means the key does not exist. DEL key – synchronously delete a key; large keys may block the server. UNLINK key – asynchronously delete a key; preferred for large keys. SCAN cursor [MATCH pattern] [COUNT n] – iterate keys in batches; use instead of KEYS * in production.
Example interaction:
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET name Tom
OK
127.0.0.1:6379> GET name
"Tom"
127.0.0.1:6379> TYPE name
string
127.0.0.1:6379> EXPIRE name 60
(integer) 1
127.0.0.1:6379> TTL name
(integer) 58Production‑grade key scanning:
127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 100SET command journey
Client sends SET name "Tom" over TCP using the RESP protocol.
Network framework event loop detects readable socket and reads the byte stream.
Command parsing extracts the command name SET and arguments name, Tom, then dispatches to the setCommand handler.
Index module creates or updates an entry for name in the global hash table, pointing to the new value object.
Storage module allocates a string object for "Tom" (using SDS) and links it to the hash entry.
Response sends +OK back to the client.
All steps execute in memory and are processed by a single thread, which explains Redis’s high throughput without locking.
Redis vs. Memcached
Data structures : Redis offers rich types (String, List, Hash, Set, Sorted Set, …); Memcached only stores simple strings.
Persistence : Redis supports AOF and RDB; Memcached has none – data is lost on restart.
High availability : Redis provides master‑slave replication, Sentinel and Cluster; Memcached has no built‑in HA.
Typical use case : Redis is used for caching plus complex data structures and distributed coordination; Memcached is a pure simple cache.
Redis vs. MySQL
Storage model : Redis is memory‑first, MySQL stores data on disk.
Speed : Redis operates in microseconds, MySQL in milliseconds.
Consistency / query model : Redis offers simple key access with weak consistency; MySQL provides strong consistency and full SQL support.
Position in architecture : Redis sits in front of MySQL as a cache layer for hot data; MySQL remains the primary persistent store.
Typical deployment: most read requests hit Redis; cache misses fall back to MySQL.
Common pitfalls
Cannot connect (Could not connect to Redis)
Symptoms: redis-cli hangs or reports connection refused.
Causes: server not started, wrong port, bind restriction, firewall.
Solution: verify connectivity with
redis-cli -h <ip> -p <port> -a <password> PING, ensure the redis-server process is running, check bind configuration and firewall rules.
Authentication required
Cause: requirepass is set but the client did not provide a password.
Solution: connect with redis-cli -a yourpassword or issue AUTH yourpassword after connecting.
KEYS * freezes production
Cause: KEYS is an O(N) operation that blocks the single thread, causing all requests to stall when the dataset is large.
Solution: replace with cursor‑based SCAN, which fetches small batches each time.
Summary
Redis is a memory‑based key‑value database composed of a network framework, command parser, global hash index, storage layer, and auxiliary modules (persistence, replication, clustering). Essential commands enable basic inspection, expiration, deletion, and safe iteration. The SET example shows the full internal flow from client to response, illustrating why Redis can handle high request rates with a single‑threaded, lock‑free design. Comparisons with Memcached and MySQL clarify Redis’s role as a fast, feature‑rich cache and coordination layer rather than a universal database.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Yumin Fish Harvest
A deep‑sea salvage fisherman sharing architecture insights, practical tips, and lessons learned.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
