Key operations and common uses of Redis

By April 1, 2015 August 18th, 2022 No Comments
ObjectRocket skyline

In Matthew Barker’s Getting started with Redis post, we covered the 5 data structures in Redis. This overview will cover some key operations and common uses, along with security options and a few simple example scripts.

Key Operations


redis-cli> set test key1


redis-cli> get test


redis-cli> del test
(integer) 1
redis-cli> get test

EXISTS (boolean response)

redis-cli> exists test
(integer) 0


So while there are some examples of the SMEMBERS command for search, below is an example using SSCAN which is a “cursor based iterator” rather than full out locking command.

redis-cli> SADD series anything
(integer) 1
redis-cli> sadd series everything
(integer) 1
redis-cli> sadd series allthethings
(integer) 1
redis-cli> sadd series somethings
(integer) 1
redis-cli> sadd series onething
(integer) 1
redis-cli> sscan series 0
1) "0"
2)  1) "anything"
    2) "onething"
    3) "somethings"
    4) "allthethings"
    5) "everything"

Searching for a thing

redis-cli> sscan series 0 match *allthe*
1) "0"
2) 1) "allthethings"

Practical Application

While some use cases are more common than others, keep in mind that Redis doesn’t have to work alone. You can choose to leverage its speed and flexibility in conjunction with other technologies to help build bridges and even new platforms, as well as to streamline communications between services using Redis.

Common use cases:

  • caching (most types)
  • queuing
  • session stores
  • gaming
  • metrics
  • pub/sub


While Redis is designed to be accessed by trusted clients, you can, however, implement security from multiple levels:

  • implement security around the application
  • SSL encrypt the pipeline your passing Redis from/to client<->server
  • set requirepass in redis.conf or config set
  • rename the config operation so that it’s not available

Depending on the needs of your application, you can choose to leverage all of these elements together, or on an individual basis.


Python Cache

One extremely useful script that I have used in several applications is redis-simple-cache.

I recommend this code because of its simplicity, ease-of-use and consistent performance, if you choose not to use the de-facto Werkzeug implementation.

from redis_cache import SimpleCache, cache_it

appcache = SimpleCache(limit=100,

def awesome_route():
    return render_template('cache_example.html', data=anydata)

And thats it… awesome, right?


While predis is widely seen as the Redis driver of choice, in my experience, it can occasionally have some scalability issues when streaming at high volumes. In this case, I’d recommend taking the time to install the C-based driver phpredis if you have the option.


class manage_object {                                                           

     private $expire = 3600;                                                    

     private function connect($key){                                            
         $r = new Redis();                                                      
         $r->connect('localhost', 6379);                                        
         return $r;                                                             

     public function store($mykey, $data, $expire){                             
         $r = $this->connect();                                                 
         $r->setex($mykey, $expire, $data);                                     
         return "saved";                                                        

     public function retrieve($mykey){                                          
         $r = $this->connect();                                                 
         return r;                                                              

$mt = new manage_object();                                                      
$mt->store("This test", "Save all this data!");                                 
$mt->retrieve("This test");                                                     
print $mt;                                                                      



Many other great options are generously provided on


There are many useful real-world examples of Redis being used to improve system intercommunication and to build platform scalability. However, as the technology continues to evolve, it’s important to keep a pulse on how its capabilities can benefit your business.