Skip to content

A Spring Boot REST API proxy service that reduces backend response times by 99% through intelligent caching. Features include real-time cache hit/miss statistics, admin management endpoints, and Docker containerization. Built with Java 17, Spring Boot, Spring Cache abstraction, and Maven. Demonstrates production-ready caching patterns

Notifications You must be signed in to change notification settings

Shubham0699/smart-cache-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart Cache Proxy

A Spring Boot-based cache proxy service that forwards requests to backend APIs and caches responses to improve performance.

Features

  • ✅ REST API proxy with automatic caching
  • ✅ In-memory caching with Spring Cache abstraction
  • ✅ Cache hit/miss tracking and statistics
  • ✅ Admin endpoints for cache management
  • ✅ Configurable TTL (Time To Live)
  • ✅ Docker support

Tech Stack

  • Java 17
  • Spring Boot 3.4.12
  • Spring Cache (In-memory)
  • Maven
  • Docker

Project Structure

smart-cache-proxy/
├── src/
│   ├── main/
│   │   ├── java/com/shubh/cacheproxy/
│   │   │   ├── SmartCacheProxyApplication.java
│   │   │   ├── controller/
│   │   │   │   ├── HealthController.java
│   │   │   │   ├── ProxyController.java
│   │   │   │   └── CacheAdminController.java
│   │   │   ├── service/
│   │   │   │   ├── ProxyService.java
│   │   │   │   └── CacheStatsService.java
│   │   │   └── model/
│   │   │       └── ProxyResponse.java
│   │   └── resources/
│   │       └── application.yaml
├── Dockerfile
├── docker-compose.yml
└── pom.xml

Getting Started

Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • Docker (optional)

Running Locally

  1. Clone the repository
git clone <your-repo-url>
cd smart-cache-proxy
  1. Build the project
./mvnw clean install
  1. Run the application
./mvnw spring-boot:run

The application will start on http://localhost:8080

Running with Docker

  1. Build the JAR file
./mvnw clean package
  1. Build and run with Docker Compose
docker-compose up --build

API Endpoints

Health Check

GET /api/health

Returns the health status of the service.

GET /api/ping

Simple ping endpoint.

Proxy Endpoint

GET /proxy?path=<target-path>

Forwards the request to the backend API and caches the response.

Example:

curl "http://localhost:8080/proxy?path=/posts/1"

Response:

{
  "statusCode": 200,
  "body": "{ ... }",
  "responseTime": 703,
  "fromCache": false
}

Cache Management

Get Cache Statistics

GET /cache/stats

Response:

{
  "totalRequests": 5,
  "cacheHits": 3,
  "cacheMisses": 2,
  "hitRate": "60.00%"
}

Clear Cache

DELETE /cache/clear

Response:

{
  "message": "Cache cleared successfully"
}

Configuration

Configuration is managed in src/main/resources/application.yaml:

spring:
  application:
    name: smartCacheProxy
  cache:
    type: simple
    cache-names: api-cache

server:
  port: 8080

proxy:
  backend:
    base-url: https://jsonplaceholder.typicode.com

How It Works

  1. Client sends a request to /proxy?path=/posts/1
  2. ProxyController checks if the response is in cache
  3. If CACHE HIT: Returns cached response (fast)
  4. If CACHE MISS: Forwards request to backend API, caches the response, then returns it
  5. Subsequent requests to the same path are served from cache

Key Concepts Demonstrated

  • Spring Boot: REST API development
  • Dependency Injection: Service layer architecture
  • Spring Cache: @Cacheable annotation for caching
  • Response Wrapping: Custom response model with metadata
  • Statistics Tracking: Thread-safe atomic counters
  • Docker: Containerization

Performance Benefits

  • First Request: ~700ms (fetches from backend)
  • Cached Request: <10ms (served from cache)
  • Improvement: ~99% faster response time

Future Enhancements

  • Redis integration for distributed caching
  • Multiple cache eviction strategies (LRU, LFU)
  • Cache warming on startup
  • Request rate limiting
  • Response compression

Author

Shubh

  • Blockchain Developer
  • Backend Engineer

License

This project is open source and available under the MIT License.

About

A Spring Boot REST API proxy service that reduces backend response times by 99% through intelligent caching. Features include real-time cache hit/miss statistics, admin management endpoints, and Docker containerization. Built with Java 17, Spring Boot, Spring Cache abstraction, and Maven. Demonstrates production-ready caching patterns

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published