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.
|Redis can cache data and also perform functionality like replication and persistence.||Memcached can only cache data.|
|Redis is single threaded.||Memcached is multi-threaded.|
|Redis can save max 2GB key length.||Memcache can save max 250 bytes length.|
|Check and Set (CAS) is not supported by Redis.||Memcached supports CAS.|
|Redis does not support LRU (Least Recently Used).||Memcached supports LRU.|
|In Redis we can set expiry time for everything. In case of full memory Redis will pick 3 keys randomly and will delete the only that is closest to expiry.||In case of Memcache if there is a full memory scenario then it will select the item that was least recently used (LRU) and will delete it to create space.|
We can create a key using the
SET command and then assign the value 10.
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.
127.0.0.1:6379> SETEX str 300 "hello" OK
For this we use the
In the following example we are checking the time to live for the key
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.
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.
To get the value stored in the hash we have to use the
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
To fetch value of a specific field we have to use the
127.0.0.1:6379> HGET myhash firstname "Jane"
In the above output we get the value of the field
firstname stored in
In the following example we are creating a queue
myqueue that will hold integer value.
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
127.0.0.1:6379> DEL myqueue (integer) 1