Wednesday, July 31, 2019

Redis Interview Questions & Answers

1) What is Redis?

Redis is an advanced key-value data store and cache. It has is also referred to as a data structure server as such the keys not only contains strings, but also hashes, sets, lists, and sorted sets. Companies using Redis includes StackOverflow, Twitter, Github, etc.

2) 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

3) What is the difference between Memcached and Redis?

                           Redis                           Memcached
  • Redis also does cache information but has got additional features like persistence and replication
  • Redis does not support 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
  • Redis does not support CAS ( Check and Set). It is useful for maintaining cache consistency
  • Redis has got stronger data structures; it can handle strings, binary safe strings, list of binary safe strings, sorted lists, etc.
  • Redis had a maximum of 2GB key length
  • Redis is single threaded
  •  Memcached only cache information.
  • Memcached supports the functionality of LRU (least recently used) eviction of values
  • In Memcached when they overflow memory, the one you have not used recently (LRU- least recently used) will get deleted
  • Memcached supports CAS (Check and Set)
  • 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.
  • Memcached had a maximum of 250 bytes length
  • Memcached is a multi-threaded

4) What are the advantages of using Redis?

Advantage of using Redis are

  • It provides high speed
  • It supports a server-side locking
  • It has got lots of client lib
  • It has got command level Atomic Operation (tx operation)

5) What are the limitations of Redis?

  • It is single threaded
  • It has got limited client support for consistent hashing
  • It has significant overhead for persistence
  • It is not deployed widely

6) 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

7) Which PHP module can be used with Redis?

In PHP module, PRedis is more preferable than Redid PHP binding or Resident

8) Does Redis give speed and durability both?

No, Redis purposely compromises the durability to enhance the speed. In Redis, in the event of system failure or crash, Redis writes to disk but may fall behind and lose the data which is not stored.

9) How can you improve the durability in Redis?

To improve the durability of Redis “append only file” can be configured by using fsync data on disk.
  • Fsync () every time a new command is added to the append log file: It is safe but very slow
  • Fysnc() one time every second: It is fast, but you may lose 1 second of data if system fails
  • Never fsync(): It is an unsafe method, and your data is in hand of Operating System

10) Mention what are the things you have to take care while using Redis?

While using Redis one must take care of
  • Select a consistent method to name and prefix your keys. Manage your namespace
  • Create a “Registry” of key prefixes that maps each of your internal documents for that application which “own” them
  • For every class you put through into your Redis infrastructure: design, implement and test the mechanisms for garbage collection or data migration to archival storage
  • Design, implement and test a sharding library before you have invested much into your application deployment and make sure that you keep a registry of “shards “replicated on each server
  • Separate all your K/V store and related operations into your own library/API or service
11) How Can You Use Redis With .net Application?

To use Redis in .Net applications, follow these steps:
  • First, Download Redis Server.
  • Install Redis Server.
  • Download Redis Client.
  • Set Configuration into Web.config File.
  • Use Redis Client Class.
Question 12) We All Know That Reds Is Fast, But Is It Also Durable?
No. Redis compromises with durability to enhance the speed. In Redis, in the case of system failure or crash, it writes to disk but may fall behind and lose the data which is not stored.
Question 13) What Are The Main Features Of Redis?
Following are the main features of Redis:
  • Redis is very simple to install setup and manage.
  • Redis is very fast. It can execute 100000 queries per second.
  • Redis is fast because data is being persistent in memory as well as stored on the disk.
  • 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.
  • 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 master to slave replication.
Question 14) In Which Language Redis Is Written?
Redis is written in ANSI C and mostly used for cache solution and session management. It creates unique keys for store values.
Question 15) What Are The Things You Must Have To Take Care While Using Redis?
While using Redis, you must have to take care of following instructions:
  • Select a consistent method to name and prefix your keys. Manage your namespace.
  • Create a “Registry” of key prefixes that maps each of your internal documents for those applications which “own” them.
  • For every class you put through into your Redis infrastructure: design, implement and test the mechanisms for garbage collection or data migration to archival storage.
  • Design, implement and test a sharding library before you have invested much into your application deployment and make sure that you keep a registry of “shards” replicated on each server.
  • Separate all your K/V store and related operations into your own library/API or service.
Question 16) What Is The Difference Between Overriding A Value By Using Set Vs Append?
A value set by using SET command will set the value for the key.Doing it over again will override the value. However , using append has the effect of set only when the key has no value earlier, but if there is a value already assigned to the key , then doing append on the key would lead to an appended value to the existing value of the key.

Question 17. What Do You Mean By Data Modeling In Redis?
Answer :
Just like any other database, data modeling represents the storage pattern of how and which data structures are used to store the data to achieve a domain requirement. For example in relational database world, we use primary key to establish a relationship between two entities. Data stored in relational databases are in table format, where as in Redis there are set of data structures available, which are used to represent the domain data. There is certainly different design mindset needed to convert the relational data to a Redis dataset.
Question 18. Why Redis Is Different As Compared To Other Key-value Stores?
Answer :
  • Redis values can contain more complex data types, with atomic operations.
  • Redis is an in-memory but persistent on disk database.
Question 19. How Do I Move A Redis Database From One Server To Another?
Answer :
  • use BGSAVE command to Save a spanshot of the database into a dump.rdb
  • Copy this dump.rdb file into another server.
Question 20. How To Re-start Redis?
Answer :
You can restart Redis by using following path:
/etc/init.d/redis-server restart
Question 21. How To Start Redis?
  1. Answer :
    You can start Redis by using following path:
    /etc/init.d/redis-server start
  2. Question 22. How To Stop Redis?
  3. Answer :
    You can stop Redis by using following path:
    /etc/init.d/redis-server stop
  4. Question 23. How To Get All Keys From Redis?
    Answer :
    Use the following commands:
    $keyList = $redis->keys("*");
    print_r($keyList);
    /*Array ( [0] => tutorials [1] => name ) */

Question 24. How To Get Array Data From Redis?
Answer :
To get the array data from Redis use the following command :
$list = $redis->lrange("tutorials", 0 ,5);  
print_r($list);
/*Array ( [0] => Mysql [1] => Mongodb [2] => Redis [3] => MySQL [4] => PHP [5] => Mysql ) */
Question 25. How To Set Multiple Values (array) In Redis?
Answer :
To set multiple values in Redis use the following commands:
$redis->lpush("tutorials", "PHP");
$redis->lpush("tutorials", "MySQL");
$redis->lpush("tutorials", "Redis");
$redis->lpush("tutorials", "Mongodb");
$redis->lpush("tutorials", "Mysql");

Question 26. How To Get Value From Redis?
Answer :
To get value from Redis :
echo $redis->get('name'); //Set string in redis

Question 27. How To Set Value In Redis?
Answer :
To set the value in Redis use the following command:
$redis->set("name", "Set string in redis");

  1. Question 28. How To Check Redis Is Running?
    Answer :
    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();
    }
  2. Question 29. How To Remove All Database?
    Answer :
    To remove all database use the following code:
    redis-cli flushall
  1. Question 30. How To Delete Current Database?
    Answer :
    To delete current database use the following code:
    redis-cli flushdb
  2. Question 31. What Do You Mean By "redis Is Binary Safe"?
    Answer :
    Binary safe means that it has a known length but not limited by any special character. You can store any value upto the given size. A string value can be 512 MB in length.
  3. Question 32. How Does Redis Differ From Mongodb? Is There A Use Case When For Redis If Using Mongodb?
    Answer :
    MongoDB is an unstructured (at least, in definition) distributed document storage. For clearing purpose you can think about it as a high scalable and not so high performance JSON storage (though actually it uses BSON). It has a simple but effective multitype index system, replication and sharding, and a lot of more nice features.
    Redis is a simplest key/value store, with transient data caching. It can works as an efficient queue system.

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...