Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an option to retry a failed cache insert or read #361

Open
debrutal opened this issue Apr 3, 2023 · 1 comment
Open

Provide an option to retry a failed cache insert or read #361

debrutal opened this issue Apr 3, 2023 · 1 comment

Comments

@debrutal
Copy link

debrutal commented Apr 3, 2023

Feature description

I would like to be able to retry reading from and writing into Redis, when the connection to redis is lost.

Our current problem is the following:

@Cacheable // (1) InterceptPhase = -100
@Retryable // (2) InterceptPhase = -60
public String getValue(String someParameter){
 // calling remote service that can fail (3)
  return "value"
}
(4)

Explaination:

  1. @Cacheable checks if this method with the given parameter is in our Cache (Redis) If it is found, the cache hit will return immediatly our desired value. This interceptor kicks in first, since its intercept Phase is lower than @Retryable
  2. @Retryable will retry in case of failures of computing our desired value (i.e. calls a remote service). For simplicity reasons it just shall retry on any problem.
  3. Calling a remote service that fails is covered by retryable
  4. When this method return as value @Cacheable will store the result within our cache. Unfortunatly this cache is a remote service, which can also fail. We could see a few Connection reset issues.

Problem / Feature 1: Reading from Redis can fail and should be retried
Problem / Feature 2: Writing into Redis can fail and should be retried

Having this scenario, i would love to see something like

micronaut:
   redis:
      retry: 
        read: 2 # Retry twice on failure
        insert: 2 # This triggers insert retry 

The given example solution is just for inspiration purposes!

@debrutal debrutal changed the title Provide an option to retry a failed cache insert. Provide an option to retry a failed cache insert or read Apr 3, 2023
@debrutal
Copy link
Author

debrutal commented Apr 3, 2023

While not saying it is a sane Solution, but it still can work, assuming you have a more fine grained Exception identification.
One workaround is to wrap the @Cacheable in a Retry method.

The following sample, calling doSomething() shows how the execution order is.

@Singleton
 public static class MyService {
        AtomicInteger inner = new AtomicInteger(0);
        AtomicInteger outer = new AtomicInteger(0);

        @Retryable(includes = NullPointerException.class)
        @Cacheable
        public void doit() {
            inner.addAndGet(1);
            System.out.println("inner " + inner.get());
            throw new NullPointerException();
        }

        @Retryable(includes = NullPointerException.class) // (1)
        public void doSomething() {
            outer.addAndGet(1);
            System.out.println("outer " + outer.get());
            doit();

        }
    }
  1. This retryable can fetch Exceptions, which are thrown in the @Cacheable processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant