January 01, 2020

Part 1: Redis Interview Questions And Answers

What is in-memory Database?
The in-memory database is a database where it keeps the dataset in RAM. Means that for every interaction with database, you will only access the Main memory. No disk operations involved during this interaction. Hence the operation will be faster as it directly access main memory instead of any disk operation.

What is Redis?
Redis(REmote DIctionary Server) is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.

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 Twitter, GitHub, Weibo, Pinterest, Snapchat, Craigslist, StackOverflow, Flickretc.

Isn’t the Redis data lost if there’s a system crash?
To avoid data loss in case of system crash Redis provides persistence by Snapshotting and AOF(Append-only files).

What is the difference between Memcached and Redis?
  • Memcached only cache information. Redis also does cache information but has got additional features like persistence and replication.
  • Memcached supports the functionality of LRU (least recently used) eviction of values. Redis does not support 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. 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.
  • Memcached supports CAS (Check and Set). Redis does not support CAS ( Check and Set). It is useful for maintaining cache consistency.
  • 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 has got stronger data structures; it can handle strings, binary safe strings, list of binary safe strings, sorted lists, etc.
  • Memcached had a maximum of 250 bytes key length. Redis had a maximum of 2GB key length
  • Memcached is a multi-threaded, where as Redis is single threaded.
How Redis achieve persistence by Snapshotting?
Snapshot takes the data as it exists at one moment in time and writes it to the disk, will be written to the file referenced as 'dbfilename'. There are 5 steps to initiate snapshots.
a. BGSAVE: When redis client initiate BGSAVE, redis will create a fork so that child process will write a the snapshot to the disk where master interacts with commands.
b. SAVE: When redis client initiate SAVE, redis will stop responding any commands until snapshot completes
c. From Configuration Files:
Example: Configuration as SAVE 60 10000
BGSAVE will be called if 10000 writes occurs within 60 seconds
d. SHUTDOWN: When redis client sends a SHUTDOWN, redis will perform SAVE operations, blocks all operations, shutdowns later.
e. Sync with other Redis: If a Redis server connects to another server, it issues SYNC, then master Redis will start BGSAVE.

How redis achieve persistence by AOF?
AOF copies incoming write command to disk as they happen. It provides file synch options as follows.
a. always: Every writes to redis, writes to disk which usually affect redis’ performance.
b. everysec: Writes to disk every second
c. no: Let OS control syncing to disk

Files will keep on growing as long as AOF executed. Won’t it consume more memory? What is the solution provided by redis?
Redis provides command BGREWRITEAOF which rewrites the existing AOF with the shortest sequence of commands needed to rebuild the current dataset in memory.

Explain SET and GET command.
SET command used to set a value to the key and GET command to get a value of the key as follows, of data-type STRING. e.g:
> SET MY_KEY MY_VALUE
OK [MY_VALUE is being set to MY_KEY]

> GET MY_KEY
"MY_VALUE"

SET Command with NX|XX arguments
We can use NX or XX while calling SET command. NX argument indicates that SET a value to the key if the key is not exist. XX argument indicates that SET a value to the key only if the key already exists. e.g:

--value will not be updated if MY_KEY is present
> SET MY_KEY MY_VALUE_UPDATED NX
(nil)
> GET MY_KEY
"MY_VALUE"

--value will be updated
> SET MY_KEY MY_VALUE_UPDATED XX
OK

> GET MY_KEY
"MY_VALUE_UPDATED"

What are the advantages of using Redis?
  • 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).
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.
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.

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
List some advantages of using Redis?
  • Redis uses its hashing algorithm called Redis hashing to store the user details as key-value pairs.
  • Insertion of the mass volume of data into the Redis cache is easy.
  • Data replication in Redis is asynchronous, when a master node updates, all the slave node gets updated asynchronously.
  • Since Redis offers data replication, it can withstand failures more effectively and offer its service.
  • Redis commands can be executed as a bunch of queues instead of one at a time.
  • The memory required to install Redis is small, so it can be easily installed on arm devices or a raspberry pi.
  • You can store huge data of key-value pairs in Redis as big as 512 MB of data.
  • Redis offers API in programming languages like c, python, java, and Javascript. So development is made easy in Redis.
  • You can develop a messaging system easily using Redis as it offers a pub/sub messaging mechanism.
  • Redis offers a rich set of data types for key-value data stores.
  • Redis is exceptionally fast, atomic operations, and persistent database memory.
How to install Redis Ubuntu?
Redis can be installed on the Ubuntu machines by using the command:

sudo apt-get install Redis-server
If the user wants Redis to be installed as an object cache for Wordpress or any other PHP, then the following command can be used:-

sudo apt-get install php-redis
If the user wants to configure the Redis as a cache, /etc/Redis/redis.conf file needs to be updated, and the text editor nano can be used for this purpose.

sudo nano /etc/redis/redis.conf
Any text editor can be used.

How to install Redis windows?
Here is one of the way of installing the Redis on Windows:-

1.Install some packages
$ sudo apt-get install build-essential
$ sudo apt-get install tcl8.5

2. Download the source code of the latest version of Redis from http://download.redis.io/releases/
3. Extract the file that has been downloaded.
4.Compile the Redis source
$ sudo make distclean
$ sudo make –j

5.Test the compiled files ((optional))
$ sudo make test -j

6. After compilation, copy the Redis binaries under /usr/local/bin/ and then install the Redis server by the following instructions:-
$ sudo make install -j
$ cd utils
$ sudo ./install_server.sh

7. Installation Completed

-K Himaanshu Shuklaa..

No comments:

Post a Comment