Redis Interview Questions - Set 2

Redis Interview Questions


This page contains Redis interview questions and answers.

Q1: What is the default port for Redis?

Redis run on localhost port 6379.

Q2: What are the differences between RDBMS and Redis?

Following are some of the differences between RDBMS and Redis

  • Redis is NoSQL and RDBMS is SQL database.
  • Redis is extremely fast whereas, RDBMS is comparatively slower.
  • Redis stores data in primary memory whereas, RDBMS stores data in secondary memory.
  • Redis stores data in key-value form while RDBMS saves data in tables.
  • Redis provides high speed by compromising persistency of data. RDBMS provides persistence data.

Q3: Name some of the data structures supported by Redis

Following are some of the data strutures supported by Redis.

  • String
  • Hashes
  • Set
  • Sorted set with range queries
  • Geospatial indexes with radius queries
  • Bitmaps
  • Hyperloglogs

Q4: List some of the differences between Redis and Memcached

Following are some of the differences between Redis and Memcached.

Redis 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.

Q5: Create a key in Redis to save integer value 10

We can create a key using the SET command and then assign the value 10.> SET num 10

Q6: How will you create a key that expires after 300 seconds and hold a string value

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 str 300 "hello"

Q7: How will you find the time to live for a key in Redis?

For this we use the TTL command.

In the following example we are checking the time to live for the key str.> TTL str
(integer) 120

So, in the above output the key str will get removed after 120 seconds.

Q8: How will you save multiple values under one key in Redis?

We can use the HMSET command and it will create a hash that will store multiple field-value pairs under one key.> HMSET myhash firstname "Jane" lastname "Doe" score 10

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 HGETALL command.> 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 myhash firstname

In the above output we get the value of the field firstname stored in myhash key.

Q9: Create a queue in Redis?

In the following example we are creating a queue myqueue that will hold integer value.

Points to note!

  • We will push the value in the queue from the right.
  • We will pop the value from the queue from the left.

Pushing a new value.> RPUSH myqueue 1
(integer) 1

Pushing couple of more new values.> RPUSH myqueue 2 3 4 5
(integer) 5

Listing all the values in the queue.> LRANGE myqueue 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

Popping value from the queue.> LPOP myqueue

Popping couple of more values.> LPOP myqueue
"2"> LPOP myqueue

Listing the content of the queue.> LRANGE myqueue 0 -1
1) "4"
2) "5"

Q10: How will you delete a key from Redis?

To delete a key from Redis we use the DEL command.> DEL myqueue
(integer) 1