Building a Fuel Route Optimization API with Django, Redis & OpenRouteService

●4 ●7
calendar_today • schedule1 min read
— Originally published at dev.to

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.

Image description

Image description

Want more #webdevelopment & #backend deep dives?

Join 6,181+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

2 Comments

1 vote
0

More Posts

Merancang Backend Bisnis ISP: API Pelanggan, Paket Internet, Invoice, dan Tiket Support

Masbadar - Mar 13

Stop Turning Every REST Endpoint into an MCP Tool: A Better Approach to AI Tool Design

zeekwrites - Nov 30

The Zero-Net-Loss Fleet & The Mercenary Squad: A Live AI Economy

DEVPlank - Aug 4

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

From Prompts to Goals: The Rise of Outcome-Driven Development

Tom Smithverified - Apr 11
chevron_left
176 Points • 11 Badges
2Posts
1Comments
Full-stack Web developer with experience on: Python {Django} | JavaScript & Typescript | CSS | Git. ... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!