wasAsked.

How would you design a Spring Boot API for a high-traffic product catalog while keeping latency predictable?

Zoho · Full Stack Developer · 3-4-years · technical

Published

Year not specified · Java, Spring Boot, System Design

Community answers

Posted by wasAsked editorial collection

Predictable latency comes from making the common path cheap and isolating anything expensive. Caching layers - CDN caching for anonymous catalog pages and images. - HTTP caching: Cache-Control and ETags (Spring's ShallowEtagHeaderFilter or explicit ETags) so clients and proxies reuse responses. - Application caching: Caffeine in memory for hot items plus Redis for shared caching, with event-driven invalidation when products change and TTLs as a safety net. Read-optimised data - A search engine (Elasticsearch or OpenSearch) for search, filters and facets, fed by change events from the primary database. - Denormalised product summary tables or documents for listing pages. - Read replicas for heavy read traffic. Efficient persistence - DTO projections instead of loading entity graphs; avoid N+1 queries; keyset pagination with maximum page sizes. - Proper indexes and query plans checked with EXPLAIN. - Tuned HikariCP pool sizes (not too large), plus statement and query timeouts. - Disable open-in-view to avoid lazy loading during serialization. Concurrency and threads - Virtual threads (spring.threads.virtual.enabled=true on Java 21+) for high concurrency with blocking I/O, or WebFlux where backpressure and streaming are needed. - Bulkheads and timeouts around dependencies such as pricing or inventory services, with circuit breakers and fallbacks (show cached price or "price unavailable"). Keep work off the request path - Price recalculation, image processing and search reindexing run asynchronously via Kafka consumers or scheduled jobs. - Precompute "top sellers" and recommendations. Payload efficiency - Response compression (server.compression.enabled), small DTOs, and fields selection for large objects. Protection and scalability - Rate limiting per client to stop scrapers. - Stateless instances with horizontal autoscaling; warm-up before receiving traffic (readiness probes). JVM tuning - Choose a suitable GC (G1 default; ZGC for very low pause requirements on large heaps), set container-aware memory limits, and watch allocation rates. Measure - Track p95 and p99 latency per endpoint with Micrometer, set SLOs, use distributed tracing to locate slow dependencies, and load test (Gatling, k6, JMeter) including cache-cold and dependency-failure scenarios.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics