January 01, 2020

Part 2: Redis Interview Questions And Answers

Explain Redis Hashes data-type?
Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects like user-details. Every hash can store up to 2³² — 1 field-value pairs (more than 4 billion).

Explain the Replication feature of Redis?
Redis supports simple master to slave replication. When a relationship is established, data from the master is transferred to the slave. Once this is done, all changes to the master replicate to the slave.

List out the operation keys of Redis?
Operation keys of Redis include: TYPE key, TTL key, KEYS pattern, EXPIRE key seconds, EXPIREAT key timestamp, EXISTS key, DEL key.

What is the default port for Redis?
6379

What is Redis-cli?
redis-cli is command-line interface for Redis that allows sending commands to the Redis server directly from the terminal.

List Some useful Redis Commands?
  • redis-server: runs the Redis Server itself.
  • redis-sentinel: runs Redis Sentinel, a tool for monitoring and failover.
  • redis-cli: runs a command line interface utility to interact with Redis.
  • redis-cli --help: to check all the commands that can be used with redis-cli.
  • redis-benchmark: checks Redis performance.
  • redis-cli info: checks Redis Info.
  • info: to get server statistics
  • config get* : to change runtime configuration
  • select "datbase number" - to select the database by its number
  • client list – to list all the connections with the database
  • monitor – to track all the network traffic
How to check redis instance version?
  • redis-server --version
  • redis-server -v
  • redis-cli -v
How to check whether the Redis server is Running
We can use the redis-cli to send a command to it directly. To test that Redis is working properly. If everything is working well, we should get PONG as a reply in the shell.

redis-cli ping
//Output: PONG

How to stop Redis?
/etc/init.d/redis-server stop

How to run Redis in Background?
redis-server &

The limitation of above approach is that you can't run any 2nd command in the same bash command after the '&'. e.g you can't run redis-server & && do-something-else-in-redis

How can we move a redis database from one server to another?
We can use BGSAVE command to save a spanshot of the database into a dump.rdb
Then we need to copy this dump.rdb file into another server.

How to delete everything in Redis?
We can use Redis FLUSHDB command to delete all the keys of the currently selected database.
  • redis-cli FLUSHDB //Removes data from your connection's current database
  • redis-cli FLUSHALL //Removes data from all databases
  • redis-cli -n database_number FLUSHDB //Removes all keys of the specified Redis database
How to fetch all redis keys from database?
redis-cli keys "*"

Keys *pattern* will returns all the keys that has the related pattern as the word “pattern”.

Which command 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.
How to find all redis process?
ps aux | grep redis-server

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

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.

In which language Redis is written?
Redis is NoSql based Key-value Database, which is written in ANSI C

Explain the data types used in Redis?
1. Strings
2. Hashes:- They represent the objects.
3. Lists:- It is a collection of the string arranged randomly
4. Sets:- they are the set of lines that are sorted according to the insertion order
5. Sorted Sets:- similar like the sets but here with each member, there is a score which may get repeated while the members will be unique
6. bitmaps
7. Hyperloglogs
8. Geospatial indexes with radius queries

-K Himaanshu Shuklaa..

No comments:

Post a Comment