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 interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
ruby
require "timeout" require "net/http" class RemoteInventoryClient
Bounding a slow HTTP call with Timeout in Ruby
timeout
error-handling
http
Intermediate
6 steps
java
@Entity @Table(name = "orders") @SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?") @Where(clause = "deleted = false")
Soft deletes with Hibernate in Spring
soft-delete
jpa
hibernate
Intermediate
9 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.