I am a Sr. Software Developer at Oracle Cloud. The opinions expressed here are my own and not necessarily those of my employer.
Redis and Rube Goldberg machines
Rube Goldberg machines are deliberately complex contraptions that require the designer to perform a series of excessively convoluted steps to accomplish a very simple task (turn on a light switch). In my career I worked on some applications that also were a little too complex.
For “fun” exercise we will build a system using Redis performing a simple task such as writing something to a log file. Except to make it more interesting we will push data through multiple Redis data structures.
If we did not need to push data through Redis we would have a simple class in our Ruby on Rails application that wrote to a log file.
To make out code work with Redis we will modify it like this. We will create a @uuid
which will be used as various Redis keys. Then we will call methods such as string
and list
to read/write data from different Redis data structures. At the end we will grab data from the last data structure (in this case sorted_set
) and actually write it to a log file.
String
Here we use a basic set
command to write data to a key @uuid
with value of input
. We have to pass input
to the method but in the future methods we will get it from Redis. We are also logging method name so we can verify our program execution through all the steps.
List
We use get
operation to read the data from Redis String. Then we remove the @uuid
key otherwise we will not be able to create a new record with the same key. And finally we do lpush
to insert data into a Redis List.
Hash
We are following similar pattern of getting data out of Redis only now we are using rpop
command. Once the last item is removed from Redis List that key will be deleted so we do not need to call REDIS.del
but there is no harm in doing it. Then we insert it into Hash specifying ‘data’ string as the field and data variable as the value.
Set
We extract data from Redis Hash using hget
, delete the key and add the same data to Set with sadd
command.
Sorted Set
Since we know that our Redis Set has only one member we can call .first
on the results of smembers
command. We use zadd
command and specify epoch time as the member score.
In the output
method we get data out of Redis with zrange
command and finally write it to a log file.
Obviously this is a crazy exercise but it does illustrate a point of how can we store data in Redis data structures. And now for something more fun here are YouTube videos of real Rube Goldberg machines.
Links
- https://en.wikipedia.org/wiki/Rube_Goldberg_machine
- https://www.rubegoldberg.com
- https://redis.io