Skip to main content
ParityEdge
Java (Spring Boot)
Backend Runtimes

Java & Spring Boot Backend Integration

Resolve localized purchasing power parity pricing server-side in Spring Boot 3, Micronaut, and Quarkus enterprise APIs with sub-10ms Anycast edge resolution.

Spring Boot Service with Caffeine In-Memory Cache

Create a thread-safe Spring service that uses non-blocking WebClient and Caffeine cache for zero latency overhead:

package com.example.saas.service;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;

public record ParityResponse(
    boolean eligible,
    int discountPercentage,
    String countryCode,
    String countryName,
    String currency,
    String couponCode,
    int tier
) {}

public record PricingResult(
    BigDecimal basePrice,
    BigDecimal finalPrice,
    ParityResponse parity
) {}

@Service
public class ParityPricingService {

    private final WebClient webClient;
    private final Cache<String, ParityResponse> localCache;
    private static final String EDGE_URL = "https://api.parityedge.app/v1/lookup";
    private static final String PROJECT_ID = "prj_live_YOUR_KEY";

    public ParityPricingService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder
            .baseUrl(EDGE_URL)
            .build();

        this.localCache = Caffeine.newBuilder()
            .expireAfterWrite(Duration.ofMinutes(10))
            .maximumSize(50_000)
            .build();
    }

    public PricingResult calculatePrice(BigDecimal basePrice, String clientIp) {
        ParityResponse parity = localCache.get(clientIp, ip -> fetchFromEdge(ip));

        if (parity != null && parity.eligible() && parity.discountPercentage() > 0) {
            BigDecimal discountMultiplier = BigDecimal.valueOf(100 - parity.discountPercentage())
                .divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
            BigDecimal finalPrice = basePrice.multiply(discountMultiplier).setScale(2, RoundingMode.HALF_UP);
            return new PricingResult(basePrice, finalPrice, parity);
        }

        return new PricingResult(basePrice, basePrice, parity);
    }

    private ParityResponse fetchFromEdge(String clientIp) {
        try {
            return webClient.get()
                .uri(uriBuilder -> uriBuilder
                    .queryParam("projectId", PROJECT_ID)
                    .build())
                .header("x-forwarded-for", clientIp != null ? clientIp : "")
                .retrieve()
                .bodyToMono(ParityResponse.class)
                .timeout(Duration.ofMillis(800))
                .onErrorReturn(new ParityResponse(false, 0, "US", "United States", "USD", "", 1))
                .block();
        } catch (Exception e) {
            return new ParityResponse(false, 0, "US", "United States", "USD", "", 1);
        }
    }
}

Spring Boot REST Controller Endpoint

package com.example.saas.controller;

import com.example.saas.service.ParityPricingService;
import com.example.saas.service.PricingResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/pricing")
public class PricingController {

    private final ParityPricingService pricingService;

    public PricingController(ParityPricingService pricingService) {
        this.pricingService = pricingService;
    }

    @GetMapping
    public ResponseEntity<PricingResult> getPricing(HttpServletRequest request) {
        String clientIp = request.getHeader("X-Forwarded-For");
        if (clientIp != null && clientIp.contains(",")) {
            clientIp = clientIp.split(",")[0].trim();
        } else if (clientIp == null || clientIp.isEmpty()) {
            clientIp = request.getRemoteAddr();
        }

        PricingResult result = pricingService.calculatePrice(new BigDecimal("49.00"), clientIp);
        return ResponseEntity.ok(result);
    }
}

Production Architecture Highlights

Fail-Open Circuit Safety

Configured with reactive 800ms timeouts and default USD fallbacks to guarantee that checkout endpoints never block on transient network hiccups.

Edge ASN Threat Inspection

The ParityEdge Anycast engine inspects incoming datacenter autonomous system numbers in constant O(1) time to block VPN discount abuse.