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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Retry only errors that might succeed next time — server and network faults — and never client errors like 4xx that will always fail.
- 2Randomized exponential backoff spreads retries out and prevents a thundering herd against a struggling downstream service.
- 3Distinct @Recover methods let you shape different outcomes — a safe fallback versus a rethrown exception — based on which failure exhausted the retries.
Related explainers
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) { return orders.stream() .collect(Collectors.groupingBy(Order::getCustomerId)); }
Grouping streams with Java Collectors
streams
grouping
collectors
Intermediate
5 steps
javascript
const RETRIABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]); function sleep(ms, signal) { return new Promise((resolve, reject) => {
Retrying fetch with exponential backoff
retry
exponential-backoff
abort-signal
Advanced
8 steps
java
public final class Debouncer<T> { private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
How a debouncer collapses bursts in Java
debounce
concurrency
scheduling
Intermediate
9 steps
php
<?php namespace App\Support;
Retry with exponential backoff in PHP
retry
exponential-backoff
error-handling
Intermediate
7 steps
java
package com.example.logs; import java.io.IOException; import java.io.UncheckedIOException;
Counting HTTP statuses with Java streams
streams
regex
file-io
Intermediate
6 steps
java
@Service public class GitHubClient { private final WebClient webClient;
Calling the GitHub API with Spring WebClient
reactive
http-client
dependency-injection
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/resilient-payment-calls-with-spring-retry-explained-java-67fb/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.