Redis Compass (The String Datastructure) — Part 3

We’re getting deeper into redis now, this time we’d be looking deeper into the String datastructure.

String Datastructure

The String data-structure has a limit of about 512MB(mega-bytes), it can hold any form of data from basic words to numbers to images thereby making it binary safe.

Let’s fire up our redis-cli to begin tinkling with the String datastructure ( in case you don’t have redis set-up, don’t worry the next post will walk you through the whole process of setting up redis on your development machine).

Remember we talked about redis being built on top of a key value mapping system.  In the above code by saying

we basically tell redis that we want to store a value “world” with the key “hello”, so any time we’d want to access that value we simply “get” the value by it’s key ( the key is basically the unique address used to identify the value).

The set operation will always replace whatever value the key held before, so assuming we have this:

surprisingly it did not throw any error when we set the key “name” to “scaleri” considering that the key “name” was already holding the value “covenant”, when we do this:

we discover that our former value “covenant” was overwritten.  In the event that you want a “setting” (technically this is called an assignment) operation to fail if the key already exist i.e set “name” to “scaleri” to fail since we had the key “name”already set to “covenant”.  We append the value “nx“:

we see that the operation set name scaleri nx gave a nil output (take it as unsuccessful).  When we try to get the value in key “name” we still get the initial value “covenant”:


Increment And Decrement Operations

In redis we have atomic commands such as INCR,  INCRBY, DECR, DECRBY which can do very much interesting things.   The INCR command increments a value by 1(one).

The INCRBY command increments the value by a specified value, so if we wanted to increment age by 5 we simply do this:

The DECR decreases the value by 1:

The DECRBY command decreases the value by the specified value:


That’s all for now, in our next post we will go through the process of setting up redis on our development machine.

Have any question? feel free to comment below.

2 comments on “Redis Compass (The String Datastructure) — Part 3

Leave a Reply

Your email address will not be published. Required fields are marked *