How codefarm0/price-aggregator works
Purpose and Runtime
This repository is a Java 25/Spring Boot price-aggregation microservice. It exposes a REST API that retrieves a product’s prices from Amazon, Walmart, and Flipkart, then returns an aggregated result with resilience, Redis caching, trace propagation, and structured logging.
Application bootstrap: src/main/java/in/codefarm/price/aggregator/PriceAggregatorApplication.java. It runs on port 8080; container deployment is defined by Dockerfile and docker-compose.yml.
HTTP Entry and Request Context
PriceController owns GET /api/prices/{productId} and accepts X-Refresh-Cache: true to bypass cache reads:
• src/main/java/in/codefarm/price/aggregator/controller/PriceController.java
• src/main/java/in/codefarm/price/aggregator/dto/PriceResponse.java
TraceIdFilter establishes or accepts X-Trace-Id, places it into MDC, and returns it in the response. This is the request boundary for end-to-end correlation:
• src/main/java/in/codefarm/price/aggregator/config/TraceIdFilter.java
• src/main/resources/logback-spring.xml
Orchestration and Response Model
PriceService is the main control-flow coordinator: controller request → cache decision → vendor aggregation → cache write → API response. The result DTOs model vendor-level outcomes and the aggregate response:
• src/main/java/in/codefarm/price/aggregator/service/PriceService.java
• src/main/java/in/codefarm/price/aggregator/dto/PriceResult.java
• src/main/java/in/codefarm/price/aggregator/dto/PriceSource.java
External Retailer Boundary
Vendor-specific WebClient adapters isolate HTTP integration with the three independently deployed mock retailer services. PriceAggregator is the shared aggregation abstraction/coordinator at this boundary:
• src/main/java/in/codefarm/price/aggregator/external/PriceAggregator.java
• src/main/java/in/codefarm/price/aggregator/external/AmazonClient.java
• src/main/java/in/codefarm/price/aggregator/external/FlipkartClient.java
Walmart follows the same adapter pattern in src/main/java/in/codefarm/price/aggregator/external/WalmartClient.java. External mocks are not implemented in this repository; their URLs and service topology are configured for local or Docker execution.
Resilience and Async Execution
Resilience4j patterns—circuit breaker, bulkhead, retry, and time limiter—protect vendor calls, preventing individual retailer failures from taking down aggregation. Request-scoped MDC context must cross asynchronous work; MdcTaskDecorator provides that propagation. Relevant configuration is centralized in:
• src/main/java/in/codefarm/price/aggregator/config/PriceConfig.java
• src/main/java/in/codefarm/price/aggregator/config/MdcTaskDecorator.java
• src/main/resources/application.yaml
Cache and Infrastructure
PriceCacheService encapsulates Redis-backed cached price responses, forming the persistence boundary between orchestration and Redis. RedisConfig configures the Redis integration:
• src/main/java/in/codefarm/price/aggregator/service/PriceCacheService.java
• src/main/java/in/codefarm/price/aggregator/config/RedisConfig.java
• docker-compose.yml
WebClientConfig defines the outbound HTTP client infrastructure:
• src/main/java/in/codefarm/price/aggregator/config/WebClientConfig.java
Included Local Mock Surface
MockVendorController exists within this service’s source tree, while the documented distributed mock vendors are separate repositories. Treat it as an internal/local mock surface rather than the production vendor integration path:
• src/main/java/in/codefarm/price/aggregator/mock/MockVendorController.java
• src/main/java/in/codefarm/price/aggregator/external/WalmartClient.java
Main entry points: PriceAggregator.java Price aggregator · PriceService.java Price service · WalmartClient.java Walmart client · docker-compose.yml Docker deployment
Components 19
HTTP boundary
- Trace ID filter · Establishes MDC trace context and returns X-Trace-Id.
src/main/java/in/codefarm/price/aggregator/config/TraceIdFilter.java - Price controller · Serves GET /api/prices/{productId} and cache-refresh requests.
src/main/java/in/codefarm/price/aggregator/controller/PriceController.java - API response
src/main/java/in/codefarm/price/aggregator/dto/PriceResponse.java
Aggregation core
- Price service · Coordinates cache lookup, vendor aggregation, cache write, and response creation.
src/main/java/in/codefarm/price/aggregator/service/PriceService.java - Aggregate price result · Models aggregate and vendor-level price outcomes.
src/main/java/in/codefarm/price/aggregator/dto/PriceResult.java - Price source
src/main/java/in/codefarm/price/aggregator/dto/PriceSource.java - Price cache service · Encapsulates Redis-backed cached price responses.
src/main/java/in/codefarm/price/aggregator/service/PriceCacheService.java
Vendor integration
- Price aggregator · Coordinates vendor-specific price retrieval.
src/main/java/in/codefarm/price/aggregator/external/PriceAggregator.java - Amazon client
src/main/java/in/codefarm/price/aggregator/external/AmazonClient.java - Walmart client
src/main/java/in/codefarm/price/aggregator/external/WalmartClient.java - Flipkart client
src/main/java/in/codefarm/price/aggregator/external/FlipkartClient.java - Retailer mock services · Independently deployed Amazon, Walmart, and Flipkart mock endpoints.
- Local mock vendor API · Internal development-only mock surface.
src/main/java/in/codefarm/price/aggregator/mock/MockVendorController.java
Platform services
- Spring Boot application · Runs the price aggregation service on port 8080.
src/main/java/in/codefarm/price/aggregator/PriceAggregatorApplication.java - Resilience and async execution · Applies circuit breaking, bulkheads, retry, time limits, and MDC propagation.
src/main/java/in/codefarm/price/aggregator/config/PriceConfig.java - Redis
- Redis configuration
src/main/java/in/codefarm/price/aggregator/config/RedisConfig.java - WebClient configuration
src/main/java/in/codefarm/price/aggregator/config/WebClientConfig.java - Docker deployment · Defines local service and Redis deployment.
docker-compose.yml
Connections 21
- Spring Boot application → Trace ID filter: starts HTTP pipeline
- Trace ID filter → Price controller: correlated request
- Price controller → Price service: productId + refresh flag
- Price service → Price cache service: read/write cached response
- Price service → Price aggregator: cache miss or refresh
- Price service → Aggregate price result: builds
- Aggregate price result → Price source: contains vendor outcomes
- Price service → API response: maps to
- Price aggregator → Resilience and async execution: protected async calls
- Price aggregator → Amazon client: delegates
- Price aggregator → Walmart client: delegates
- Price aggregator → Flipkart client: delegates
- Amazon client → Retailer mock services: Amazon endpoint
- Walmart client → Retailer mock services: Walmart endpoint
- Flipkart client → Retailer mock services: Flipkart endpoint
- Price cache service → Redis: cache operations
- Redis configuration → Redis: configures connection
- WebClient configuration → Price aggregator: provides HTTP clients
- Docker deployment → Spring Boot application: runs container
- Docker deployment → Redis: runs cache
- Local mock vendor API → Walmart client: local development surface