28th Dec, 2019
Redis or Remote Dictionary Server happens to be an open-source and an in-memory data construction project executing a dispersed, in-memory key-value database along with discretionary durability. It allows various kinds of conceptual data structures like lists, maps, strings, spatial indexes, streams, Hyper Loglogs, bitmaps, sorted sets, sets, and streams. Redis offers a data model which is extremely unlikely for an RDBMS or relational database management system, it is also considered a cache and a store along with its being a system.
The Redis execution makes extreme utilization of the fork system call, to replicate the procedure possessing the data, while the parent procedure continues to obey clients, and the child procedure makes a replication of the data on the disks.
Below are few major features of Redis
Q1. What is Redis?
An open-source in-memory data structure store that is can be used as a Database. It stores data in key-value format. Redis supports a wide range of data structures like strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indexes
Q2. Enlist major applications of Redis?
Various applications of Redis.
Q3. List some popular companies using Redis?
Below is the list of some popular companies who are using Redis:
Q4. What is Redis-cli?
redis-cli is command-line interface for Redis that allows sending commands to the Redis server directly from the terminal.
Q5. Enlist major Redis commands?
Redis is mainly used by the system admins, and some of the major commands in Redis are,
Q6. List some advantages of using Redis?
Some of the significant advantages of using Redis are the following,
Q7. What is use of MGET in Redis?
MGET is a command in Redis that returns the value of the key. You pass the name of the key along with the MGET command, and the value gets returned as an array. MGET returns nil for keys that don’t exist or keys that don’t have any value.
//syntax > MGET keyname //example > SET key “keyValue” > MGET key 1) “keyValue”
Q8. How to get all keys in Redis cli?
KEYS command returns all the available keys in the database. The time complexity for this command is 0(n) where n is the number of available keys. It is fairly high so it shouldn’t be used only in the development time.
KEYS * // this command returns all the keys
KEYS *pattern* //returns all the keys that has the related pattern as the word “pattern”
>MSET keyName keyValue > MSET Var Val > KEYS * 1) keyValue 2) Val > KEYS *key?* 1) keyValue
Q9. How to list all databases in Redis?
To get a list of all the database connection (16 maximum database connection can be done by default) in redis, do the following command.
$ redis-cli INFO | grep db
The above command returns the output like the following,
db0:keys=50000,expires=3 db1:keys=3000,expires=15 db2:keys=4005,expires=1
Note: the command only works on redis command line interface.
Q10. Name the PHP module used with Redis?
In order to use PHP with Redis, you need to install PHP Redis client or module. The PHP module is “Phpredis”. To download the module, use the pecl repository. After downloading the module, you can use Redis in PHP in the following way.
$redis = new Redis(); //creates a new Redis object to be used in PHP //Connecting to Redis $redis->connect('hostname', port); $redis->auth('password');
You can create a Redis database connection in PHP and do your database operation in PHP using the Phpredis module.
Q11. How to remove keys in Redis?
To delete all the database keys at once, do the following command.
It deletes all the caches, database, and its keys. The time complexity is 0(n) where n is the number of keys.
$redis-cli –n FLUSHDB
It deletes all the keys of the database that is mentioned in the command. It also has a time complexity of 0(n).
Q12. What Redis HDEL do?
This command is used to remove the fields from the hash that is specified and stored at the key. If the key is not there, then the command returns null (0) or 1 if it is present. If the field is not there then the command is just ignored. The time complexity is 0(n) where n is the number of fields to be removed.
HDEL KEY_NAME FIELD1.. FIELDN // command syntax
>HDEL hashName field1 //command example > 1 //returns 1 so, the field1 is removed > HDEL hashName field2 >0 //returns 0 so, the field2 is not there
Q13. How to delete all the cache using pRedis in php?
Redis is one of the powerful PHP Redis clients that enables the user to work with Redis in PHP. To delete all the cache using pRedis in PHP, use the following command.
This command is the Redis equivalent of FLUSHALL that deletes all the cache of the database connected to the Redis.
Q14. How to create new database in Redis?
A single instance of a Redis supports 16 databases indexed from 0 to 15. By default, the Redis used database numbered 0 to work but you can change it at any time. To support more database, you configure it at redis.conf file.
To select a database, use the following command.
>select 2 //select database indexed 2 >ok
After selecting the database, you can do the database operation like storing, retrieving, deleting, etc. Each database has a unique key and it can be moved between the database.
Q15. Which commnad is used to count keys in Redis?
DBSIZE command in Redis is used to return the number of keys in the currently selected database. It returns the number of keys as an integer value.
INFO command returns all the statistics about the database that is connected to the Redis. Here the information like the number of keys is displayed.