Designing a Filterable Product API (Spring Boot System Design)
Designing an API to Filter Products by productType
Section titled “Designing an API to Filter Products by productType”REST API
Section titled “REST API”GET /api/products?productType=ELECTRONICSController
Section titled “Controller”@GetMapping("/products")public List<Product> getProductsByType(@RequestParam String productType) { return productService.getProductsByType(productType);}Making the Filter Optional
Section titled “Making the Filter Optional”To return all products when productType isn’t supplied, make the parameter optional and branch on it:
@GetMapping("/products")public List<Product> getProducts( @RequestParam(required = false) String productType) {
return productType == null ? productService.getAllProducts() : productService.findByProductType(productType);}This way GET /products and GET /products?productType=ELECTRONICS are both handled by the same endpoint.
Service
Section titled “Service”public List<Product> getProductsByType(String productType) { return productRepository.findByProductType(productType);}Repository
Section titled “Repository”public interface ProductRepository extends JpaRepository<Product, Long> { List<Product> findByProductType(String productType);}Entity
Section titled “Entity”@Entitypublic class Product {
@Id @GeneratedValue private Long id;
private String name;
private String productType;}Optimizations
Section titled “Optimizations”- Pagination
GET /products?productType=ELECTRONICS&page=0&size=20- Database index
CREATE INDEX idx_product_type ON product(product_type);- Redis caching
@Cacheable("products")Interview Checklist
Section titled “Interview Checklist”- Explain the API design.
- Discuss database indexing.
- Mention caching.
- Explain load balancing.
- Cover asynchronous processing.
- Mention monitoring and observability.
Key Comparisons
Section titled “Key Comparisons”| Topic | Recommended Approach | Full Coverage |
|---|---|---|
| Product filtering | Repository query + index + pagination | This page |
| High traffic scaling | Horizontal scaling, caching, async processing | Performance Optimization and Scalability |
| Distributed transactions | Saga pattern | Saga Pattern and Compensation Failures |
| Entity tracking | Events or CDC (Debezium) | Spring Boot Auditing |
| Centralized logging | ELK stack + distributed tracing | Microservices Communication Patterns |
Key Takeaways
Section titled “Key Takeaways”- A filterable product API is a classic system-design warm-up: derived repository query, pagination, an index on the filtered column, and a caching layer on top.
- Combine
@RequestParamfiltering with pagination (page/size) so result sets stay bounded as data grows. - Cache read-heavy, rarely-changing lookups like product listings with
@Cacheable, backed by a database index on the filtered column.