Redis Interview Questions
This page contains Redis interview questions and answers.
Redis run on localhost port 6379.
Following are some of the differences between RDBMS and Redis
Following are some of the data strutures supported by Redis.
Following are some of the differences between Redis and Memcached.
We can create a key using the SET command and then assign the value 10.
SET
127.0.0.1:6379> SET num 10 OK
We can use the SETEX command to create a key that will hold a string value and will get auto deleted after 300 seconds.
SETEX
127.0.0.1:6379> SETEX str 300 "hello" OK
For this we use the TTL command.
TTL
In the following example we are checking the time to live for the key str.
str
127.0.0.1:6379> TTL str (integer) 120
So, in the above output the key str will get removed after 120 seconds.
We can use the HMSET command and it will create a hash that will store multiple field-value pairs under one key.
HMSET
127.0.0.1:6379> HMSET myhash firstname "Jane" lastname "Doe" score 10 OK
The above command creates a hash key myhash and saves three field-value pairs.
myhash
To get the value stored in the hash we have to use the HGETALL command.
HGETALL
127.0.0.1:6379> HGETALL myhash 1) "firstname" 2) "Jane" 3) "lastname" 4) "Doe" 5) "score" 6) "10"
In the above output we get all the fields and their values for the key myhash.
To fetch value of a specific field we have to use the HGET command.
HGET
127.0.0.1:6379> HGET myhash firstname "Jane"
In the above output we get the value of the field firstname stored in myhash key.
firstname
In the following example we are creating a queue myqueue that will hold integer value.
myqueue
Points to note!
Pushing a new value.
127.0.0.1:6379> RPUSH myqueue 1 (integer) 1
Pushing couple of more new values.
127.0.0.1:6379> RPUSH myqueue 2 3 4 5 (integer) 5
Listing all the values in the queue.
127.0.0.1:6379> LRANGE myqueue 0 -1 1) "1" 2) "2" 3) "3" 4) "4" 5) "5"
Popping value from the queue.
127.0.0.1:6379> LPOP myqueue "1"
Popping couple of more values.
127.0.0.1:6379> LPOP myqueue "2" 127.0.0.1:6379> LPOP myqueue "3"
Listing the content of the queue.
127.0.0.1:6379> LRANGE myqueue 0 -1 1) "4" 2) "5"
To delete a key from Redis we use the DEL command.
DEL
127.0.0.1:6379> DEL myqueue (integer) 1