Q-1). What does JPA stand for?
A-1). JPA Stands for Java Persistence API.
Q-2). What is the difference between Spring Data JPA and Hibernate?
A-2). This is a type of a tricky question and most of the interviewees answer it wrongly. Let’s clear the confusion.
Spring Data provides an abstraction. While Hibernate is a JPA provider. One can use the Spring Data to easily work with a JPA provider like Hibernate.
Q-3). What does @Id annotation do?
A-3). If we use @Id annotation over a field then that field will be treated as the primary key for that particular table. This is a unique identifier for each entry in the table. This annotation is typically used with @GeneratedValue to automatically generate an unique entry of that Id in the table.
Q-4). What does @Entity annotation do?
A-4). The @Entity annotation is used over a class. It indicates that class represents a relational table in the database.
Q-5). Is the CrudRepository or MongoRepository or Neo4jRepository interfaces are part of JPA?
A-5). No. All the above interfaces as mentioned above are provided by Spring Data framework for more easily connect with different JPA providers like Hibernate. While, the implementation of the above interfaces saves a lot of boilerplate code.
Q-6). Is Spring Data JPA an implementation of the JPA specification?
A-6). No. Spring Data makes easier to interface with a JPA providers like Hibernate.
Q-7). What is the difference between FetchType.EAGER and FetchType.LAZY?
A-7). FetchType.EAGER is used to load all the objects eagerly without verifying whether any one has requested or not. It will make the memory consumption heavy and processing slow. While in other hand, FetchType.LAZY is used to load the records at that time when any one has requested. As a result, it saves both memory and processing.
Q-8). What is the use of @Column Annotation?
A-8). @Column annotation is used to override the name of the column that the entity class filed maps. It is an optional one.
Q-9). What does the @EnableJPARepsoitories annotation do?
A-9). This annotation enables the automatic generation of JPA repositories. Any class which implements CrudRepository or MongoRepository or Neo4jRepository will generate a repository when this annotation is present.
Q-10). What is the difference between a CrudRepository and JPARepository?
A-10). CrudRepository extends Repository interface. While, JPARepository extends PagingAndSortingRepository interface. CrudRepsoitory handles the CRUD operation while JPARepository is an ideal candidate for Batch processing.
Q-11). What is @OneToMany mapping in Hibernate?
A-11). @OneToMany mapping annotation is used to indicate that a column of one table is associated with multiple records of another table. As for an Example – Employee and Department tables. An Employee can be assigned to more than one Departments.
Q-12). What is the use of mappedBy attribute in @OneToMany mapping?
A-12). Let’s take the above example of Employee and Department where an Employee can work in more that one Departments. Then how we will be able to define the relationship. By just simply using the mappedBy attribute. One would specify mappedBy=”employee” in the Department entity.
Q-13). How to enable Spring Data JPA Features?
A-13). Spring Data JPA features comes handy with the Spring Boot. To enable, Spring Data one needs to use one annotation as @EnableJpaRepositories. And need to use one attribute within this annotation as basePackages. So the implementation will be like this:
@EnableJpaRepositories(basePackages = “com.springcavaj.springdata.repository”)
The advantage of using the above annotation is to enable the Spring Data features on the customized repository.
Q-14). What are the advantages of enabling Spring Data features to an application?
A-14). The advantage of enabling Spring Data feature is reduce the boilerplate code. When anyone enable Spring Data features then by default Spring Data provides certain default methods as:
1. findById()
2. deleteById()
3. save()
Another feature of Spring Data is, one don’t need to define the methods of the direct properties used in the Entity. Let’s consider an example. I have defined one entity named as Use. Here, I have define certain direct properties as name, mobileNo, email, etc. Then by using Spring Data you need to just declare the methods in the Repository interface as
1. findByName()
2. findByMobileNo()
3. findByEmail()
And the best part is you don’t need any implementation for this. Spring Data will internally handle the implementations.
Q-15). How the save method works?
A-15). save() method is responsible to save a record in the respective table using the correspondence entity class. Concept is if the entry doesn’t exist in database then persist() is called, but if the entry is already present in database then merge() is called.
Q-16). How to link Repository with the Entity?
A-16). In Spring Data framework Repository is an interface which extends either Crud or Mongo or Neo4j or JPA based on the requirements. When we extend the parent interface it provides the concept of generics.
Let’s see an example:
public interface SpringDataRestRepository extends JpaRepository<User, Long> -> Here you can see, that the 1st Argument is the name of the Model and the 2nd argument is the data type of that property implicitly consider as the primary key of the table. And you have defined that property by using the @Id annotation.
This is the manner by which one Repository is linked with an Entity of an application.
Q-17). What is Redis and what are its common use cases?
A-17). Redis is an open-source in-memory data structure store used as a database, cache, and message broker. The common use cases where we Redis are as follows:
Caching, Session Storage, Real-time analytics, Leaderboard systems, Pub/Sub messaging, and Data Streaming.
Q-18). How do you connect to a Redis server using the Redis CLI?
A-18). You can connect to a Redis server using Redis CLI by running the below command as:
redis-cli -h <hostname> -p <port>
Example
redis-cli -h localhost -p 6379
Q-19). What is the difference between Redis and traditional relational database?
A-19). The differences between Redis and traditional relational databases are as follows:
Redis | Traditional Relational Database |
It is an in-memory, NoSQL database designed for high-performance and low-latency operations. | They are disk-based and use Structured Query Language (SQL) to manage data. |
It stores data in various data structures such as strings, lists, sets, and hashes. | They use tables with rows and columns to store data. |
Q-20). Explain Redis persistence mechanisms.
A-20). Redis provides two persistence mechanisms which are as follows:
- RDB (Redis Database) – It creates dataset snapshots at specified intervals. This is efficient in terms of performance but can lead to a data loss if Redis crashes between snapshots.
- AOF (Append Only File) – It logs every write operation the server receives. This is more durable as it can be configured to append to the log file at every command, but it is slower and can result in larger file sizes compared to RDB snapshots.
Q-21). How does Redis handle the data eviction, and what are some eviction policies?
A-21). Redis handles data eviction mechanism by various policies. Some common eviction policies include:
- noeviction – New write operations are rejected when the memory limit is reached.
- allkeys-lru – Removes the least recently used (LRU) keys.
- volatile-lru – Removes the least recently used keys with an expiration set.
- allkeys-random – Removes random keys.
- volatile-random – Removes random keys with an expiration set.
- volatile-ttl – Removes keys with the shortest time to live (TTL).
Q-22). Describe how the transaction works in Redis.
A-22). Transactions in Redis are handled using the MULTI, EXEC, DISCARD, and WATCH commands. A transaction is started with the MULTI command, which queues subsequent commands. The EXEC command executes all queued commands atomically. If a WATCH command is used on one or more key, the transaction will only execute if those keys have not been modified by other clients. The DISCARD command can be used to discard the commands in the queue.
Q-23). How does the Redis clustering work and what are the benefits?
A-23). Redis clustering provides a way to distribute data across multiple Redis nodes, allowing for horizontal scaling and high availability. A Redis cluster consists of multiple master nodes and replica nodes. Data is sharded across master nodes using a hash slot mechanism, where each key is assigned to a specific slot, and slots are distributed among master nodes.
Benefits include improved performance through load distribution, fault tolerance, and the ability to handle larger datasets by adding more nodes.
Q-24). Explain the Redlock algorithm and its use case in Redis.
A-24). The Redlock algorithm is a distributed lock implementation in Redis designed to safely acquire locks across multiple Redis nodes to ensure high availability. It works by attempting to acquire a lock on multiple Redis instances and only considers the lock acquired if it is successful on a majority of instances within a specified timeout. This ensures that even if some nodes fail, the lock can still be acquired and released safely, providing a robust solution for distributed locking in scenarios like preventing race conditions and ensuring data consistency in distributed systems.
Q-25). How can you implement a pub/sub messaging system using Redis?
A-25). Redis provides built-in support for the pub/sub messaging pattern using the PUBLISH, SUBSCRIBE, and PSUBSCRIBE commands.
- Publish Messages – PUBLISH command to publish/send messages to a specific channel.
Example
PUBLISH channel_name "message"
- Subscribe to channels – SUBSCRIBE command to listen for messages on a specific channel.
Example
SUBSCRIBE channel_name
- Pattern Subscribe – PSUBSCRIBE command is used to subscribe to a channel that matches a pattern.
Example
PSUBSCRIBE channel*
Subscribers receive messages in real-time as they are published to the channels they are subscribed to. This pattern is useful for real-time notifications, chat applications, and event-driven architectures.