java 38 lines · 7 steps

Resilient payment calls with Spring Retry

A Spring service retries transient payment failures with exponential backoff and falls back gracefully when retries run out.

Explained by highlit
1@Service
2public class PaymentGatewayClient {
3 
4 private static final Logger log = LoggerFactory.getLogger(PaymentGatewayClient.class);
5 
6 private final RestTemplate restTemplate;
7 
8 public PaymentGatewayClient(RestTemplate restTemplate) {
9 this.restTemplate = restTemplate;
10 }
11 
12 @Retryable(
13 retryFor = { ResourceAccessException.class, HttpServerErrorException.class },
14 noRetryFor = HttpClientErrorException.class,
15 maxAttempts = 4,
16 backoff = @Backoff(delay = 500, multiplier = 2.0, maxDelay = 5000, random = true)
17 )
18 public ChargeResult charge(ChargeRequest request) {
19 log.debug("Submitting charge for order {}", request.orderId());
20 ResponseEntity<ChargeResult> response = restTemplate.postForEntity(
21 "/v1/charges", request, ChargeResult.class);
22 return response.getBody();
23 }
24 
25 @Recover
26 public ChargeResult recoverCharge(RemoteAccessException ex, ChargeRequest request) {
27 log.error("Charge for order {} failed after retries: {}", request.orderId(), ex.getMessage());
28 return ChargeResult.pending(request.orderId(),
29 "Gateway unavailable, charge queued for reconciliation");
30 }
31 
32 @Recover
33 public ChargeResult recoverCharge(HttpServerErrorException ex, ChargeRequest request) {
34 log.error("Gateway returned {} for order {}", ex.getStatusCode(), request.orderId());
35 throw new PaymentProcessingException(
36 "Payment gateway rejected charge for order " + request.orderId(), ex);
37 }
38}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Retry only errors that might succeed next time — server and network faults — and never client errors like 4xx that will always fail.
  2. 2Randomized exponential backoff spreads retries out and prevents a thundering herd against a struggling downstream service.
  3. 3Distinct @Recover methods let you shape different outcomes — a safe fallback versus a rethrown exception — based on which failure exhausted the retries.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Resilient payment calls with Spring Retry — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code