Prev Next

Database / REDIS

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is Redis?

Redis is an open source in-memory data structure store which can be used as a database and/or a cache and message broker.

NoSQL Key/Value Store.

Supports Multiple data structures.

Built in Replication.

2. List few Redis Datatypes.

Redis supports,

  • Strings,
  • Lists,
  • Sets,
  • Sorted Sets,
  • Hashes,
  • Bitmaps,
  • Hyperlogs,
  • and Geospatial indexes.
3. Advantages of Redis.
  • Very Flexible.
  • No Schema and column names.
  • Very fast, can perform around 110K Set per second and 81K GETS per second.
  • Rich Datatype support,
  • command level Atomic Operation,
  • Caching & Disk Persistence.
4. What programming languages does Redis support?

REDIS supports most of the programming languages including Java, C#, Python, Scala, C++, R, PHP and many more.

5. Explain Replication in Redis.

Redis supports simple master to slave replication. When a relationship is established, data from the master is replicated to the slave.

6. Explain about REDIS security.

Redis is designed to be accessed by trusted clients.

REDIS can be restricted to certain interfaces.

Data encryption not supported and hence do not allow external access/internet exposure.

7. Expand REDIS.

Redis stands for REmote DIctionary Server.

8. In which language Redis is developed?

Redis is developed using ANSI C and mostly used for cache solution and session management. It creates unique keys for store values.

9. Difference between SET and MSET command in REDIS.

SET command creates one key-value pair while using MSET command, multiple key-value pairs can be created.

10. Explain LPUSH command in REDIS.

LPUSH inserts all the specified values at the head of the list stored at key. If the key does not exist, it is created as an empty list before performing the push operations. When key holds a value that is not a list, an error is returned.

Usage: LPUSH key value [value ...]

redis> LPUSH mylist "World"
(integer) 1
redis> LPUSH mylist "Hello"
(integer) 2
redis> LRANGE mylist 0 -1
1) "Hello"
2) "World"
redis> 
11. Limitations of REDIS.

REDIS is single threaded.

It has got limited client support for consistent hashing.

It has significant overhead for persistence.

It cannot be deployed widely.

12. REDIS 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.

13. Difference between Memcached and REDIS.

Memcached.REDIS.
Memcached is multi-threaded. Redis is single threaded.
Memcached only does caching information. Redis does caching information and also supports persistence and replication.
Memcached supports the functionality of LRU (Least Recently Used) eviction of values.Redis does not support the functionality of LRU 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). It is useful for maintaining cache consistency. Redis does not support CAS ( Check And Set).

14. What is SADD command in REDIS?

Add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.

An error is returned when the value stored at key is not a set.

Usage:

SADD key member [member ...]
Example:

redis> SADD myset "Hello"
(integer) 1
redis> SADD myset "World"
(integer) 1
redis> SADD myset "World"
(integer) 0
redis> SMEMBERS myset
1) "World"
2) "Hello"

15. Mention few LIST operations in REDIS.

LPUSH adds an element to the beginning of a list.

RPUSH add an element to the end of a list.

LPOP removes the first element from a list and returns it.

RPOP removes the last element from a list and returns it.

LLEN gets the length of a list.

LRANGE gets a range of elements from a list.

16. Mention Spring Boot Drivers for REDIS.

Spring Boot primarily supports two main Redis drivers (clients): Lettuce (which is the default) and Jedis. It abstracts these clients using the Spring Data Redis framework, allowing developers to switch between them easily.

Supported Redis Drivers

Lettuce

This is the default Redis client used in Spring Boot applications when you include the spring-boot-starter-data-redis dependency.

  • It is built on Netty and is an advanced, thread-safe Redis client.
  • Its thread-safe nature means a single connection instance can be shared across multiple threads, which can lead to more efficient resource usage and fewer physical connections to the Redis server.
  • Lettuce supports both synchronous and reactive (non-blocking) APIs, making it a good fit for modern, reactive Spring applications.

Jedis

This client is also fully supported by Spring Data Redis, although it requires explicitly excluding the default Lettuce dependency and including the Jedis one instead in your pom.xml or build.gradle file.

  • It provides a straightforward, synchronous API that closely mirrors the original Redis commands.
  • Unlike Lettuce, Jedis is generally not thread-safe for shared instances, so it relies on connection pooling in multi-threaded environments to manage connections effectively.
17. Explain about redisson client for redis.

Redisson is a popular, open-source Java client for Redis (and Valkey) that offers a simplified, thread-safe way for Java developers to interact with the Redis data store. It abstracts the complexities of the Redis API by providing familiar Java objects and data structures, such as Map, List, Queue, and Lock, as distributed objects.

Key Features and Benefits of Redisson

Familiar Java Interfaces
Redisson provides implementations of common Java collections and data structures, allowing developers to use Redis with a minimal learning curve.
Distributed Objects and Services
It extends Redis's capabilities with distributed implementations of services like locks, semaphores, and Remote Procedure Calls (RPC).
Integration with Java Ecosystem
Redisson offers seamless integration with various popular Java frameworks, including Spring Cache, Spring Session, Hibernate Cache, and JCache API (JSR-107).
Asynchronous and Reactive APIs
It supports synchronous, asynchronous, reactive streams, and RxJava APIs, catering to different application needs and performance requirements.
High Performance Caching
Redisson enhances caching performance with features like "near cache," which stores frequently accessed data on the Java heap for faster retrieval.
Support for Various Redis Deployments
It supports different Redis configurations, including standalone, master-slave, and cluster modes.
18. What is a Cache Penetration Problem?

Cache penetration is a performance issue where requests for non-existent data repeatedly bypass the cache and hit the backend database, causing overload, often due to malicious attacks or data deletion. Solutions involve caching null/empty results with short TTLs, using a Bloom filter to pre-check for non-existent keys, or implementing logical checks to filter invalid IDs before database queries.

19. How I/O Multiplexing Works in Redis?

Redis uses I/O multiplexing to enable its single-threaded core to efficiently handle thousands of concurrent client connections. This technique allows the Redis server to monitor multiple sockets simultaneously and process data on those that are ready, without blocking on any single connection.

Single-threaded core
Redis processes all commands sequentially in a single main thread, which eliminates the overhead of thread creation, context switching, locks, and race conditions.
Non-blocking I/O
All client sockets are set to non-blocking mode. This means that an I/O operation (read/write) returns immediately if no data is available, rather than pausing the entire thread.
Event Loop
The main thread runs an event loop that uses a specific I/O multiplexing system call provided by the operating system (e.g., epoll on Linux, kqueue on macOS/BSD, evport on Solaris).
Kernel Notification
Instead of constantly polling all connections (which wastes CPU), the event loop blocks in a single, efficient system call (like epoll_wait). The operating system kernel is responsible for monitoring all the file descriptors (sockets) and notifying Redis when one or more of them are ready for an I/O operation.
Event Handling
When the system call returns, Redis's event loop processes only the "ready" sockets one by one. It reads the command, executes it from memory, and writes the response back to the client.
«
»
Apache Cassandra Interview Questions

Comments & Discussions