Understanding GET, SET, Auto-Delete, and Eviction in Redis

Understanding GET, SET, Auto-Delete, and Eviction in Redis

In this article, we explore the essential Redis commands GET and SET, and delve into the internal workings of auto-delete and eviction policies that help manage memory efficiently.

GET and SET Commands

GET Command

The GET command retrieves the value of a key. If the key does not exist, Redis returns nil.

Example:

GET mykey        
"value"        

Explanation: This command retrieves the value associated with mykey. If mykey does not exist, it will return (nil).

SET Command

The SET command assigns a value to a key. You can also set options like expiration time for the key.

Example:

SET mykey "value"        
OK        

Explanation: This command sets the value of mykey to "value".

You can also set an expiration time for the key:

Example:

SET mykey "value" EX 10        
OK        

Explanation: This command sets the value of mykey to "value" and specifies that it should expire after 10 seconds.

Auto-Delete (TTL and Expiration)

Redis allows you to set a Time-To-Live (TTL) for keys, after which they will be automatically deleted. This feature is useful for caching scenarios where data should only be kept for a certain period.

Setting TTL

You can set the TTL when you set the key, or use the EXPIRE command to set it later.

Example with SET:

SET mykey "value" EX 10        
OK        
(integer) 1        

Explanation: Sets the expiration time of mykey to 10 seconds.

Checking TTL

You can check the remaining TTL of a key using the TTL command.

Example:

TTL mykey        
(integer) 5        

Explanation: Shows the remaining time to live for mykey in seconds. If mykey does not exist or has no expiration, it returns -1 or -2.

Eviction Policies

Redis uses eviction policies to manage memory usage when the max memory limit is reached. These policies determine which keys to remove to free up space.

Common Eviction Policies

  1. noeviction: Returns an error when the memory limit is reached.
  2. allkeys-lru: Removes the least recently used (LRU) keys among all keys.
  3. volatile-lru: Removes the least recently used keys with an expiration set.
  4. allkeys-random: Removes random keys among all keys.
  5. volatile-random: Removes random keys with an expiration set.
  6. volatile-ttl: Removes keys with the shortest time-to-live (TTL).

Configuring Eviction Policies

You can configure the eviction policy in your Redis configuration file or dynamically using the CONFIG SET command.

Example:

CONFIG SET maxmemory-policy allkeys-lru        
OK        

Explanation: Sets the eviction policy to allkeys-lru, which removes the least recently used keys when the memory limit is reached.

Example Workflow

Let's go through an example workflow that includes setting, getting, auto-deleting, and evicting keys.

  1. Set a Key with Expiration:

SET session:user1 "data" EX 5        
OK        

2. Get the Key:

GET session:user1        
"data"        

3. Wait for Key to Expire: After 5 seconds, the key will be automatically deleted.

4. Check the Key After Expiration:

GET session:user1        
(nil)        

5. Simulate Memory Limit Reached and Eviction: Configure Redis with a max memory limit and an eviction policy.

6. Set Memory Limit and Policy:

CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru        

7. Add Keys to Fill Memory:

SET key1 "value1"
SET key2 "value2"
# Repeat until memory is filled        

8. Observe Eviction: When the memory limit is reached, Redis will start evicting the least recently used keys to make space for new ones.

Conclusion

Understanding the GET and SET commands, as well as the internal workings of auto-delete and eviction policies, is essential for effectively using Redis and managing memory. These features ensure that your Redis instance performs optimally, even under heavy load.

Stay tuned for the next article in this series, where we will explore more advanced Redis concepts and use cases!


Praba Santhanakrishnan

Co-Founder & CEO @Cookr | Ex-Microsoft | Building a Gen-AI powered Food Tech Marketplace | Angel Investor | FoodTech Industry

7 个月

Simple yet powerful

要查看或添加评论,请登录

Raja R的更多文章

社区洞察

其他会员也浏览了