Lettuce: AWS Elasticache: Response times #3176
-
Hello there! I am using Lettuce java client to talk to AWS Elasticache (Valkey cache using serverless, IAM authentication). Below is my code to read from cache for a given key: public void setJsonObject(String key, Object value) {
log.info("Writing to Cache with key:{}", key);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
IAMAuthTokenRequest iamAuthTokenRequest = new IAMAuthTokenRequest(user, name, region, true);
RedisCredentialsProvider redisCredentialsProvider = new RedisIAMAuthCredentialsProvider(
user, iamAuthTokenRequest, awsCredentialsProvider);
RedisURI redisURI = RedisURI.builder()
.withHost(host)
.withPort(port)
.withSsl(true)
.withAuthentication(redisCredentialsProvider)
.build();
RedisAsyncCommands<String, String> async = RedisClient.create(redisURI).connect().async(); <--- Exception at this line
async.jsonSet(key, JsonPath.ROOT_PATH, async.getJsonParser().fromObject(value));
} catch (Exception e) {
log.warn("Exception while writing to Cache: " + e.getLocalizedMessage());
}
log.info("Exiting setJsonObject. Time Taken : {}", stopwatch);
} When the cache is up and all the required auth is setup correctly, it works fine by writing the new key value pair. And the response time is below: When the cache is not available (i.e, connection error), here are the logs: When the cache is not available, the method is taking too long to respond > 10 seconds, which is making my API to timeout!! Is there any specific config that I can use to make it less than 10sec in case there is any connection issue ? Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
Greetings @alekhya538 , Could you please share the entire stack trace? How do you know which call took this amount of time? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply @tishun . As I mentioned in my post, when the Cache is configured correctly, my code works perfectly. It is writing the key value pair. And I am able to retrieve it (jsonGet). But when the Cache is not available (I tweaked the security group so my API can't access the cache), I expected it to go to catch block, log exception and return quickly. But it is taking more than 10sec, which is impacting performance of the caller API. |
Beta Was this translation helpful? Give feedback.
-
Have moved the RedisClient creation code into Configuration class. Updated the method as below, private final RedisClient redisCacheClient;
public void setJsonObject(String key, Object value) {
log.info("Writing to Cache with key:{}", key);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
StatefulRedisConnection<String, String> connection = redisCacheClient.connect(); <--- exception at this line
log.info("after connect() time taken: {}", stopwatch);
RedisAsyncCommands<String, String> async = connection.async();
log.info("after async() time taken: {}", stopwatch);
async.jsonSet(key, JsonPath.ROOT_PATH, async.getJsonParser().fromObject(value));
log.info("after jsonSet() time taken: {}", stopwatch);
async.expire(key, ttl); //Set Time to Live
} catch (Exception e) {
log.info("Exception - Time taken: {}", stopwatch);
log.warn("Exception while writing to Cache : {}", e.getLocalizedMessage());
}
log.info("Exiting setJsonObject. Time Taken : {}", stopwatch);
} When there is a connection error, below are the logs: Writing to Cache with key:member-offer:600894
Exception - Time taken: 10.05 s
Exception while writing to Cache : Unable to connect to my-cache-fpheum.serverless.apse2.cache.amazonaws.com/<unresolved>:6379
Exiting setJsonObject. Time Taken : 10.05 s The line redisCacheClient.connect() throws error and it is taking 10seconds. |
Beta Was this translation helpful? Give feedback.
-
Configuration class: @Configuration
@RequiredArgsConstructor
public class ElastiCacheConfig {
@Value("${elasticache.config.host}")
private String host;
@Value("${elasticache.config.port}")
private int port;
@Value("${elasticache.config.name}")
private String name;
@Value("${elasticache.config.user}")
private String user;
@Value("${elasticache.config.region}")
private String region;
@Bean
public RedisClient redisCacheClient() {
AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
IAMAuthTokenRequest iamAuthTokenRequest = new IAMAuthTokenRequest(user, name, region, true);
RedisCredentialsProvider redisCredentialsProvider = new RedisIAMAuthCredentialsProvider(
user, iamAuthTokenRequest, awsCredentialsProvider);
RedisURI redisURI = RedisURI.builder()
.withHost(host)
.withPort(port)
.withSsl(true)
.withAuthentication(redisCredentialsProvider)
.build();
return RedisClient.create(redisURI);
}
} |
Beta Was this translation helpful? Give feedback.
-
Hey again, apologies, let me elaborate on why I asked the questions I asked:
log.error("Exception while writing to Cache", e);
|
Beta Was this translation helpful? Give feedback.
-
Here is the stacktrace:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for your time! I resolved it by configuring the timeout,
|
Beta Was this translation helpful? Give feedback.
Thanks for your time!
I resolved it by configuring the timeout,