PHP Redis Interview questions and answers

Question: Why Redis is different as compared to other key-value stores?

  1. Redis values can contain more complex data types, with atomic operations.
  2. Redis is an in-memory but persistent on disk database.

Question: How to delete current database?

redis-cli flushdb

Question: How to remove all database?

redis-cli flushall

Question: How to check redis is running?

try {
    $redis = new Redis();
    $redis->connect('', 6379);
    echo "Redis is running.";
    echo "Server is running: " . $redis->ping();
} catch (Exception $e) {
    echo $e->getMessage();

Question: How to set value in redis?

$redis->set("name", "Set string in redis");

Question: How to get value from redis?

 echo $redis->get('name'); //Set string in redis

Question: How to set multiple values (Array) in redis?

$redis->lpush("tutorials", "PHP");
$redis->lpush("tutorials", "MySQL");
$redis->lpush("tutorials", "Redis");
$redis->lpush("tutorials", "Mongodb");
$redis->lpush("tutorials", "Mysql");

Question: How to get array data from redis?

$list = $redis->lrange("tutorials", 0 ,5);   
/*Array ( [0] => Mysql [1] => Mongodb [2] => Redis [3] => MySQL [4] => PHP [5] => Mysql ) */

Question: How to get all keys from redis?

$keyList = $redis->keys("*");
/*Array ( [0] => tutorials [1] => name ) */

Question: How to stop redis?

/etc/init.d/redis-server stop

Question: How to start redis?

/etc/init.d/redis-server start

Question: How to re-start redis?

/etc/init.d/redis-server restart

Question: How do I move a redis database from one server to another?

  1. use BGSAVE command to Save a spanshot of the database into a dump.rdb
  2. Copy this dump.rdb file into another server.