KatInterview

Build and Scale a Freight Quote Service with Slow Dependencies

System DesignModel answer
MediumModel answerSystem DesignAPI designdistributed systemscaching

Last updated

Implement service logic for a freight pricing API, then architect for high traffic when external services are slow.

You're building a pricing service for a rail freight company. The service exposes a REST API that calculates shipping quotes by combining data from multiple sources: a local database, a routing service, and a pricing surcharge service.

API Specification

POST /quotes accepts a JSON request:

{
  "origin": "CHI",
  "destination": "LAX",
  "commodity": "grain",
  "car_count": 15,
  "ship_date": "2026-12-15"
}

Pricing logic:

  1. Look up the tariff rate (dollars per car per mile) for the commodity in your database
  2. Call the routing service to get the rail distance between stations
  3. Call the surcharge service to get the date-based multiplier
  4. Calculate: base_price = tariff_rate × distance × car_count
  5. Calculate: total_price = base_price × (1 + surcharge_rate)
  6. Store the quote with an expiration timestamp and return it with a unique ID

Your Task

Part 1 (30 minutes): Write the service-layer implementation. Your code should handle validation, orchestrate the dependencies, compute prices accurately, and be testable in isolation. Include a few unit tests.

Part 2 (30 minutes): Design an architecture that keeps this endpoint fast and available under two pressures:

  • High request volume (thousands of requests per second)
  • Slow or unreliable dependency services (routing and surcharge services may take seconds to respond or fail)

Address caching strategies, timeout and failure handling, synchronous vs. asynchronous quote patterns, scaling approaches, and monitoring.

Expected Deliverables

  • Clean, testable service code with dependency injection
  • Input validation and clear error responses
  • Accurate decimal arithmetic for money
  • Architectural diagram and explanation covering reliability patterns, caching, and scaling
  • Discussion of trade-offs between price accuracy and availability

Hints

Hint 1

Think about which dependency calls can happen in parallel, and which data might be cacheable for minutes or hours without hurting accuracy.

Hint 2

Consider whether every quote request must block on all three dependencies, or if you can return a quote ID immediately and compute asynchronously.

Hint 3

Circuit breakers and timeouts protect your service from cascading failures when dependencies are slow, but you'll also need a fallback strategy for cached or degraded quotes.