Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redux Toolkit · Function

retry

RTK Query's retry utility wraps a base query and retries failed requests with exponential backoff. Configure maxRetries per API or endpoint, and use retry.fail to stop immediately when further attempts cannot help.

How it works

retry decorates an RTK Query baseQuery so failed requests are attempted again using exponential backoff. Configure the maximum retry count with maxRetries, disable retries for an endpoint with maxRetries: 0, or call retry.fail from the wrapped query to stop immediately when another attempt would be pointless.

Syntax

retry(baseQuery, options)

Parameters

NameTypeDescription
baseQueryBaseQueryFnThe base query function to wrap so its failed requests can be retried.
optionsRetryOptionsConfiguration options such as maxRetries that control retry behavior.

Returns

A wrapped base-query function with retry behavior and the retry.fail method.

Key points

  • Use retry as a wrapper around the baseQuery supplied to an RTK Query API.
  • Without configuration, the utility makes five attempts and spaces them with exponential backoff.
  • The default applies to every request handled by the wrapped base query.
  • An endpoint can disable the wrapper's retries by setting maxRetries to zero.
  • The attached fail method provides request-specific early termination.

Common mistakes

  • Retries remain enabled for wrapped endpoints unless an endpoint explicitly sets maxRetries to 0.
  • Use retry.fail when the error guarantees that subsequent attempts would be redundant.

Example: Retry a base query

import { fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'

const staggeredBaseQuery = retry(
  fetchBaseQuery({ baseUrl: '/' }),
  { maxRetries: 5 }
)

export const api = createApi({
  baseQuery: staggeredBaseQuery,
  endpoints: (build) => ({
    getPost: build.query({
      query: (id: number) => `posts/${id}`,
    }),
  }),
})

Wrap the base query passed to createApi to retry failed requests with exponential backoff.

When to use it

Recover from transient failures

A flaky network or intermittently unavailable service may succeed after a later request.

Disable retries selectively

Different endpoints need different retry policies.

Bail out on permanent failures

An authentication or validation error will not improve through repetition.

Interview questions

QWhat does retry do in RTK Query?

retry wraps the API's baseQuery and retries failed requests. Its default policy makes five attempts and applies exponential backoff with jitter between attempts.

Follow-up: How can one endpoint opt out? Set maxRetries: 0 in that endpoint's definition.

QHow do you configure the retry count?

Pass the wrapped function as createApi's baseQuery. The wrapper accepts an options object, including maxRetries, so the retry limit can be changed from its default.

Follow-up: What happens when maxRetries is zero? The endpoint is not retried.

QHow can retrying be stopped early?

The wrapper exposes retry.fail as a method. Calling it with the error, and optionally metadata, ends retrying immediately for the current request.

Follow-up: When is that useful? Use it for conditions such as unauthorized responses where repeating the request cannot succeed.

  • fetchBaseQuery
  • createApi
  • retry.fail
  • maxRetries
  • BaseQueryFn

Source: Customizing Queries. Checked 2026-10-15.