Filtering Patterns
Query Parameter Filtering
GET /products?category=electronics
GET /products?minPrice=100&maxPrice=500
GET /products?brand=Apple&inStock=true
Filter Types
| Type | Example | Use Case |
|---|---|---|
| Equality | ?status=active |
Exact match |
| Range | ?minPrice=100&maxPrice=500 |
Numeric ranges |
| Contains | ?name=laptop |
Text search |
| List | ?category=phone,tablet |
Multiple values |
| Date range | ?from=2025-01-01&to=2025-12-31 |
Time periods |
| Boolean | ?inStock=true |
True/false flags |
Advanced Filtering
# Nested filters
GET /products?category.name=electronics&category.level=2
# Multiple values (OR)
GET /products?status=active,pending
# Negation
GET /products?status=!cancelled
# Array contains
GET /products?tags=wireless,bluetooth
Filtering Implementation
@GetMapping("/products")
public ResponseEntity<List<Product>> getProducts(
@RequestParam(required = false) String category,
@RequestParam(required = false) BigDecimal minPrice,
@RequestParam(required = false) BigDecimal maxPrice,
@RequestParam(required = false) Boolean inStock,
Pageable pageable) {
return ResponseEntity.ok(
productService.filter(category, minPrice, maxPrice, inStock, pageable)
);
}
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding Filtering is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Filtering in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Filtering implementation
// Key aspects: validation, error handling, logging, testing
public class Filtering {
// Production-ready implementation
}Identify and handle edge cases for Filtering. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Filtering. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. How should multiple filter values for the same field be handled?
2. Which filtering pattern is most RESTful?
3. What is the primary purpose of Filtering?
4. What is a common mistake when implementing Filtering?
Flashcards
Question
RESTful filtering pattern?
Click to reveal answer
Answer
GET /resource?field=value (query parameters)
Question
How to handle multiple filter values?
Click to reveal answer
Answer
Comma-separated: ?status=active,pending
Question
What is Filtering?
Click to reveal answer
Answer
Filtering is a key concept in backend development.
Question
When to use Filtering?
Click to reveal answer
Answer
Use Filtering when building production systems that require reliability, scalability, and maintainability.
Question
Filtering best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Use query parameters for filtering: /products?category=electronics
- 2.Support equality, range, list, and boolean filters
- 3.Combine filtering with pagination and sorting
- 4.Make all filter parameters optional
Interview Tips
- •Design filtering for a given scenario
- •Know how to handle complex filter combinations
Cheat Sheet
Filtering
- Equality:
?status=active - Range:
?minPrice=100&maxPrice=500 - List:
?category=phone,tablet - Boolean:
?inStock=true - Rule: All filters optional, GET with query params