[go: up one dir, main page]

0% found this document useful (0 votes)
43 views33 pages

Spring Boot Interview Questions

The document provides guidelines for implementing caching in Spring Boot applications using various cache providers like EhCache, Redis, and Caffeine. It includes examples of how to add dependencies, enable caching, and configure cache managers, along with different caching strategies such as Cache Aside, Write-Through, and TTL-based caching. Additionally, it discusses the differences between in-memory and distributed caches and offers recommendations based on specific use cases.

Uploaded by

ArchanaMitnala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views33 pages

Spring Boot Interview Questions

The document provides guidelines for implementing caching in Spring Boot applications using various cache providers like EhCache, Redis, and Caffeine. It includes examples of how to add dependencies, enable caching, and configure cache managers, along with different caching strategies such as Cache Aside, Write-Through, and TTL-based caching. Additionally, it discusses the differences between in-memory and distributed caches and offers recommendations based on specific use cases.

Uploaded by

ArchanaMitnala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Spring boot interview questions

Add the required caching dependencies to your pom.xml (if you’re using EhCache, Redis, or Caffeine
as the cache provider)
For basic caching with ConcurrentMapCacheManager, you don’t need any additional dependencies.
Spring Boot provides an in-memory caching option by default.
If you want to use a specific cache provider, add the necessary dependency:
EhCache Example
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency

Redis Example
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

In main Spring Boot application class, enable caching by using the @EnableCaching annotation.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

Spring Boot uses a simple in-memory ConcurrentMapCacheManager by default.


you’ll need to configure a custom CacheManager If you want to use a different cache provider (like
EhCache, Redis, or Caffeine),
ehcache.xml:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<cache name="myCache"
maxEntriesLocalHeap="1000"
timeToLiveSeconds="3600">
</cache>
</ehcache>

import org.ehcache.jsr107.Eh107Configuration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public JCacheManagerFactoryBean cacheManager() {
JCacheManagerFactoryBean cacheManager = new JCacheManagerFactoryBean();
cacheManager.setCacheManagerUri(getClass().getResource("/ehcache.xml").toURI());
return cacheManager;
}
}

For Redis or Caffeine, similar configuration classes can be created with respective cache manager
beans.
Use caching annotations like @Cacheable, @CachePut, and @CacheEvict on service methods to
define caching behavior.
configure Redis properties such as spring.redis.host, spring.redis.port, etc.
# application.yml
spring:
cache:
type: ehcache # or redis, caffeine, etc.

Strategy Use Case Advantages Disadvantages

application first checks the cache.


If the data is not in the cache (a
cache miss), it retrieves the data Memory efficient,
Cache Aside from the database, stores it in the Read-heavy, Risk of stale data,
suitable for sparse
(Lazy Loading) cache, and then returns it to the infrequent updates reads initial cache miss
requester.

application writes data to both the


database and cache Consistent data
Always up-to-date Slower writes,
Write-Through simultaneously. When data is across cache and
cache unnecessary writes
updated, it is immediately written database
to the cache as well.

Read-Through application always reads from the Delegates read Simpler Increased
Strategy Use Case Advantages Disadvantages

cache. If data is not present in the


cache, the cache itself fetches it
from the database, stores it, and
then serves it to the application. handling to cache complexity in
application code
This is useful if you want to provider cache setup
offload the responsibility of
caching to the cache provider
(e.g., Redis).

data is written to the cache first,


and then asynchronously written
High write volume, Weaker
to the database. This can improve Better write
Write-Behind tolerant to delayed consistency, risk of
performance by deferring the performance
updates data loss
database writes and combining
multiple write operations.

TTL-based caching involves


setting an expiration time for Data changes
Automatic Choosing the right
cached entries. After the TTL periodically,
TTL/Expiration removal of old TTL can be
expires, the data is removed from eventual
data complex
the cache and reloaded when consistency
needed.

Hybrid Strategy (Combining Multiple Strategies)


For instance, you might use:
 Cache Aside for read-heavy operations with infrequent writes.
 Write-Through for consistent data that needs immediate updates.
 TTL for temporary data that can expire after a fixed period.
In-Memory Cache vs. Distributed Cache
 In-Memory Cache (e.g., EhCache, Caffeine): Stores cache data in the memory of a single
application instance, which is fast but limited to the local instance.
 Distributed Cache (e.g., Redis, Memcached): Stores cache data across multiple nodes,
making it accessible by multiple instances, thus supporting horizontal scaling and improving
fault tolerance.
Criteria Recommended Provider(s) Notes

Single instance, local Fast, in-memory caching, suitable for


Caffeine, EhCache
cache single-node apps.

Distributed, multi- Suitable for microservices or multi-


Redis, Hazelcast, Memcached
instance instance apps.

High read/write Redis for distributed, Caffeine for in-


Redis, Caffeine
throughput memory local.

Redis Cluster provides strong


Data consistency Redis (clustered), Hazelcast
consistency.

Persistence Use Redis persistence options or


Redis (AOF/RDB), Hazelcast
requirement Hazelcast's persistence.

Complex data Native support for lists, sets, sorted


Redis
structures sets, etc.

Managed Redis, Memcached (e.g., Available as a managed service on


Cloud-Native
AWS ElastiCache) most cloud providers.
 Tells Spring Boot to automatically configure your application based on the
dependencies
 Spring Boot uses a clever technique called "Condition Evaluation." examines your
project's classpath, existing beans, and configuration properties to figure out what you
need. Based on this information, it automatically configures various components like
data sources, security, web servers, and more.
Communicate and react to events without being directly connected, keeping the code clean and
organised
s

You might also like