A list of frequently asked Redis interview questions and answers are given below:
1. What is Redis?
Redis, which stands for Remote Dictionary Server, is a fast, open-source, in-memory key-value data store for use as a database, cache, message broker, and queue.
You can run atomic operations, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set intersection, union, and difference; or getting the member with the highest ranking in a sorted set.
In order to achieve performance, Redis works with an in-memory dataset. Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while or by appending each command to a log. Persistence can be optionally disabled if you just need a feature-rich, networked, in-memory cache.
Redis is a popular choice for caching, session management, gaming, leaderboards, real-time analytics, geospatial, ride-hailing, chat/messaging, media streaming, and pub/sub-apps.
2. What are the main features of Redis?
Following are the main features of Redis:
- Redis is very simple to install set up and manage.
- Redis is very fast. It can execute 100000 queries per second.
- Redis is fast because data is persistent in memory as well as stored on the disk.
- Redis is very fast because it loads the whole dataset in primary memory.
- Redis operations working on different data types are atomic so these operations can be accomplished safely i.e. to set or increase a key, add or remove elements from a set or increase a counter.
- It supports various types of data structures such as strings, hashes, sets, lists, sorted sets, etc.
- Redis supports a variety of languages i.e. C, C++, C#, Ruby, Python, Twisted Python, PHP, Erlang, Tcl, Perl, Lua, Java, Scala, etc.
- If your favorite language is not supported yet, you can write your own client library, as the Protocol is pretty simple.
- Redis supports simple to master to slave replication.
- Redis is portable.
3. What are the advantages of using Redis?
Following is a list of some important advantages of Redis:
- Redis is very fast.
- It supports server-side locking.
- It has a rich client-side library.
- It is a good counter.
- It supports Atomic Operation.
4. Does Redis persist data?
Redis supports so-called “snapshots”. This means that it will do a complete copy of what’s in memory at some points in time (e.g. every full hour). When you lose power between two snapshots, you will lose the data from the time between the last snapshot and the crash (doesn’t have to be a power outage..). Redis trades data safety versus performance like most NoSQL-DBs do.
Redis saves data in one of the following cases:
- automatically from time to time
- when you manually call
BGSAVE
command - when Redis is shutting down
But data in Redis is not really persistent, because:
- crash of Redis process means losing all changes since last save
BGSAVE
the operation can only be performed if you have enough free RAM (the amount of extra RAM is equal to the size of Redis DB)
5. Is Redis just a cache?
Like a cache Redis offers:
- in-memory key-value storage
But unlike a cash Redis:
- Supports multiple data types (strings, hashes, lists, sets, sorted sets, bitmaps, and hyperloglogs)
- It provides an ability to store cache data into physical storage (if needed).
- Supports pub-sub model
- Redis cache provides replication for high availability (master/slave)
- Supports ultra-fast lua-scripts. Its execution time equals C commands execution.
- Can be shared across multiple instances of the application (instead of in-memory cache for each app instance)
6. What is the usage of Redis?
Redis is a special key-value store database that can function as a NoSQL database or as a memory-cache store to improve performance when serving data that is stored in system memory.
7. What is the difference between Redis replication and sharding?
- Sharding, also known as partitioning, is splitting the data up by key. Sharding is useful to increase performance, reducing the hit and memory load on any one resource.
- While replication, also known as mirroring, is to copy all data. Replication is useful for getting a high availability of reads. If you read from multiple replicas, you will also reduce the hit rate on all resources, but the memory requirement for all resources remains the same.
Any key-value store (of which Redis is only one example) supports sharding, though certain cross-key functions will no longer work. Redis supports replication out of the box.
8. What is the difference between Memcached and Redis?
Redis | Mem-cache |
---|---|
Redis also does cache information but has got additional features like persistence and replication | Memcached only cache information. |
Redis does not support the functionality of LRU (least recently used) eviction of values | Memcached supports the functionality of LRU (least recently used) eviction of values |
In Redis you can set a time out on everything when memory is full, it will look at three random keys and deletes the one which is closest to expiry | In Memcached when they overflow memory, the one you have not used recently (LRU- least recently used) will get deleted |
Redis does not support CAS ( Check and Set). It is useful for maintaining cache consistency | Memcached supports CAS (Check and Set) |
Redis has got stronger data structures; it can handle strings, binary-safe strings, list of binary | In Memcached, you have to serialize the objects or arrays in order to save them, and to read them back you have to un-serialize them. |
Redis had a maximum of 2GB key length | Memcached had a maximum of 250 bytes in length |
Redis is single threaded | Memcached is a multi-threaded |
9. List the data structures supported by Redis.
Redis supports the following Data Structures
- Strings
- Hashes
- Lists
- Sets
- Sorted sets with range queries
- bitmaps
- Hyperloglogs
- Geospatial indexes with radius queries
10. In which language Redis is written?
Redis is NoSQL
based Key-value Database, which is written in ANSI C
11. What Do You Mean By “Redis Is Binary Safe”?
Binary safe means that it has a known length but is not limited by any special character. You can store any value up to the given size. A string value can be 512 MB in length.
12. Why Redis Is Different As Compared To Other Key-value Stores?
- Redis values can contain more complex data types, with atomic operations.
- Redis is an in-memory but persistent on-disk database.
Practical Questions
13. How to interact with Redis?
After the installation of the server, you can run the Redis Client provided by the Redis installation or you can open the command prompt and use the following command:
redis-cli
14. How To Re-start Redis?
You can restart Redis by using the following path:
/etc/init.d/redis-server restart
15. How To Start Redis?
You can start Redis by using the following path:
/etc/init.d/redis-server start
16. How To Stop Redis?
You can stop Redis by using the following path:
/etc/init.d/redis-server stop
17. How To Set Value In Redis?
To set the value in Redis use the following command:
$redis->set("name", "Set string in redis");
18. How To Check Redis Is Running?
To check Redis is running try out the following code :
try
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Redis is running.";
echo "Server is running: " . $redis->ping();
}
catch (Exception $e)
{
echo $e->getMessage();
}
19. How To Remove All Database?
To remove all databases use the following code:
redis-cli flushall
20. How To Delete Current Database?
To delete the current database use the following code:
redis-cli flushdb