#1
In microservices architectures, API gateways handle a large number of requests before forwarding them to backend services. If every request reaches the service layer, systems can become slow and overloaded.
A common optimization is API gateway caching, where frequently requested responses are stored in Redis. This allows the gateway to return cached responses instantly without calling backend services.
This article explains how to design Redis-based caching at the API gateway layer using Spring Boot.

1. What Is API Gateway Caching

API gateway caching stores responses at the entry point of the system.
Typical architecture:
Client
   ↓
API Gateway
   ↓
Redis Cache
   ↓
Microservices
   ↓
Database
Workflow:
  1. Client sends request to gateway
  2. Gateway checks Redis cache
  3. If cached → return response
  4. If not → call service and cache response
This significantly reduces service load.

2. Benefits of Gateway-Level Caching

Advantages:
  • Faster API responses
  • Reduced microservice calls
  • Lower database load
  • Improved scalability
This approach is especially useful for read-heavy APIs.

3. Designing Cache Keys

Proper cache key design is critical.
Example key format:
api:products:page:1
api:user:profile:123
api:orders:user:42
Keys should uniquely represent the request.
Common key components:
  • Endpoint name
  • Query parameters
  • User ID (if needed)

4. Implementing API Caching Service

Example Redis caching service:
@Service
public class ApiCacheService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public Object getCachedResponse(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void cacheResponse(String key, Object value) {
        redisTemplate.opsForValue().set(key, value, Duration.ofMinutes(5));
    }
}
This service handles reading and writing cached API responses.

5. Applying Cache in a Gateway Controller

Example gateway controller:
@RestController
@RequestMapping("/gateway")
public class GatewayController {

    @Autowired
    private ApiCacheService cacheService;

    @Autowired
    private ProductService productService;

    @GetMapping("/products")
    public Object getProducts(@RequestParam int page) {

        String key = "api:products:page:" + page;

        Object cached = cacheService.getCachedResponse(key);

        if (cached != null) {
            return cached;
        }

        Object response = productService.getProducts(page);

        cacheService.cacheResponse(key, response);

        return response;
    }
}
This ensures repeated requests are served directly from Redis.

6. Cache Expiration Strategy

Cached API responses should expire automatically.
Example TTL:
Product list → 5 minutes
User profile → 10 minutes
Statistics → 1 minute
Shorter TTL values are recommended for frequently changing data.

7. Cache Invalidation for APIs

When backend data changes, related API cache entries must be removed.
Example:
redisTemplate.delete("api:products:page:1");
Advanced systems may use event-based invalidation with Redis Pub/Sub.

8. Handling Personalized Responses

Some APIs return user-specific data.
Example:
api:user:profile:1001
api:user:profile:1002
Including the user ID in cache keys prevents data leaks between users.

9. Monitoring Gateway Cache Performance

Key metrics:
  • Cache hit rate
  • Cache miss rate
  • API response time
  • Redis memory usage
High hit rates indicate effective caching.
Spring Boot Actuator can expose metrics:
management.endpoints.web.exposure.include=metrics
#ads

image quote pre code