1
votes

I'm using Hystrix async for the first time to make a set call to a third-party library for which I really don't care if data was posted successfully or not.

public class SetCacheDataCommand extends BaseHystrixCommand {

    public SetCacheDataCommand(CacheClient cacheClient, String cacheKey, Entry cacheValue, int timeToLive) {
    super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GROUP_NAME))
            .andCommandKey(HystrixCommandKey.Factory.asKey(COMMAND_NAME))
            .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
            .withExecutionTimeoutInMilliseconds(DEFAULT_HYSTRIX_TIMEOUT_MILLISECONDS)));
    this.cacheClient = cacheClient;
    this.cacheKey = cacheKey;
    this.cacheValue = cacheValue;
    this.timeToLive = timeToLive;
}

@Override
protected Object run() throws Exception {
    cacheClient.set(cacheKey, cacheValue, timeToLive);
    return null;
}

Set is a void method. Here is the command call:

....Doing something here

new SetCacheDataCommand(cacheClient, cacheKey, cacheValue, 10000).queue();

....Doing something here

I was hoping the above code will take care of making this call async. But I read the below from Hystrix documentation.

Note: Timeouts will fire on HystrixCommand.queue(), even if the caller never calls get() on the resulting Future. Before Hystrix 1.4.0, only calls to get() triggered the timeout mechanism to take effect in such a case.

They have also mentioned:

Note that there is configuration for turning off timeouts per-command, if that is desired (see command.timeout.enabled).

Couple of questions:

  1. If I Keep timeout flag enabled, Does it mean behavior would be synchronous? Do you see any problem if I turn off the timeout flag?
  2. What happens if there are let's say 20 thread in the pool and all these threads are busy making a Set call which is async?
1

1 Answers

0
votes

If I Keep timeout flag enabled, Does it mean behavior would be synchronous? Do you see any problem if I turn off the timeout flag?

Behaviour would not be blocking unless you call execute on command. Also, hystrix executions separated from calling thread pool (like Tomcat or any server thread pool), and executed in that context in a parallel way. You should keep thread isolation timeout for proper value. Also, I suggest to give it more than your cache client socket timeouts. If you do not adjust them properly, your thread pool, which you may need to set for that specific command, may saturate and you get rejections.

What happens if there are let's say 20 thread in the pool and all these threads are busy making a Set call which is async?

You get execution rejection exceptions. Therefore, I may suggest to adjust timeouts and thread pool specifically for your case. And ignore, failures and let circuit break kick in when needed.