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)