Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Redis Core Commands and Underlying Storage Architecture

Tech 2

Redis functions as a remote dictionary service, operating as an isolated node within distributed environments via a request-response paradigm, indexing information through dictionary structures. As an in-memory datastore, it guarantees data resides strictly within RAM, delivering rapid access speeds—memory operations take approximately 100 nanoseconds compared to 10 milliseconds for disk I/O. Unlike relational databases relying on B+ tree indexing, Redis employs a key-value model for data retrieval. Furthermore, it serves as a data structure server, supporting diverse types: dynamic byte strings (length-prefixed rather than null-terminated), linked lists, hashes, sorted sets (uniqueness by member, ordering by score), standard sets, streams (often compared to Kafka), and probabilistic structures like HyperLogLog.

In Redis, keys are always user-defined byte strings, while the underlying data typpe of a value is dictated by the specific command used to manipulate it.

Compilation and Setup

git clone https://github.com/redis/redis.git --branch 6.2 --depth 1
cd redis
make -j$(nproc)
sudo make install

Execution

mkdir -p /tmp/redis-instance
cd /tmp/redis-instance
redis-server redis.conf
# In a separate terminal:
redis-cli
127.0.0.1:6379> AUTH your_secret_password

Command Examples

SET employee:role manager
# OK
KEYS employee:*
# 1) "employee:role"
GET employee:role
# "manager"
LPUSH employee:team alice bob charlie
# (integer) 3
LRANGE employee:team 0 -1
HSET employee:1001 username alice
HSET employee:1001 age 28
HSET employee:1001 department engineering
HGETALL employee:1001
SADD employee:ids 1001 1002 1003
SMEMBERS employee:ids
ZADD employee:scores 85 alice 92 bob 98 charlie
ZRANGE employee:scores 0 -1 WITHSCORES
ZADD employee:scores 99 alice

Storage Architecture

Redis hashes keys into 64-bit integers. These integers are mapped to an array whose length is a power of two, using modulo arithmetic to determine the bucket index. Collisions are resolved via linked lists chaining entries at the same bucket.

Internally, lists are implemented as double-linked circular lists. For smaller datasets, lists, hashes, and sorted sets may utilize compressed encodings like ziplists (or listpack in Redis 7.0+, which omits the back-offset while maintaining similar time complexity). Sorted sets rely on skip lists for efficient ordering. Sets containing only integers are stored as sorted integer arrays. Strings are managed as dynamic byte arrays.

OBJECT ENCODING employee:role  # Inspect internal value encoding
INCR visitor_count  # Implicitly creates the key at 0, then increments to 1
INCRBY visitor_count 5
Tags: Redis

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.