DEV Community

HILQIA KENDA
HILQIA KENDA

Posted on • Originally published at github.com

πŸš€ Building a Fuel Route Optimization API with Django, Redis & OpenRouteService

Last night, I challenged myself to solve a very practical problem:

How do you find the cheapest fuel stops along a driving route between two cities?

At first, it sounded simple.

Until I opened the fuel station dataset and realized…

It had no coordinates. πŸ˜…

That’s when this turned from a simple API into a deep dive into:

  • Routing
  • Geocoding
  • Spatial matching
  • Caching
  • Performance engineering

🧠 The Core Idea

Given:

Start Address β†’ End Address as a string or list of coordinates

The system should:

  1. Geocode both addresses using OpenRouteService
  2. Fetch the driving route polyline
  3. Build a route corridor using bounding boxes along the path
  4. Match fuel stations from a CSV dataset that fall inside this corridor
  5. Return the cheapest stations along the trip

No coordinates in the dataset? No problem. We make the map logic do the work.

⚑ The Performance Problem

Geocoding and routing APIs are expensive and slow.

So I introduced Redis caching:

  • Addresses are geocoded once and cached forever
  • Routes are cached
  • Fuel stations are loaded into memory using Pandas

And here’s the fun part:

❌ No database
❌ No GIS server
βœ… Pure spatial computation in memory

After the first request, response times drop to under 150ms.

πŸ› οΈ Stack Used

  • Django + DRF
  • Redis
  • Pandas
  • GeoPandas
  • OpenRouteService API
  • Spatial math (without PostGIS)

πŸ’‘ Key Engineering Lessons

  • Normalize messy data instead of bending your logic around it
  • Cache aggressively when dealing with external APIs
  • You don’t always need a database for spatial problems
  • Bounding boxes + in-memory data can be incredibly fast

This project genuinely felt like building a mini map engine from scratch.

Top comments (0)