OSRM (Open Source Routing Machine) is an open-source routing engine written in C++ that computes shortest paths in road networks on OpenStreetMap data. You can run it on your own server and calculate distances, durations and routes without depending on an outside service.
This article covers what OSRM does and does not solve in logistics software, how to set it up, its services and what to watch in production. In the logistics management platform I built, distance calculations and intercity mileage reports come from an OSRM service we host ourselves on Docker; some of the decisions below come from there.
Two separate questions
Route planning is really two separate questions, and OSRM answers only the first:
- What are the distance and duration between two points, and which way does the route go? This is a road-network calculation on map data. OSRM does exactly this.
- Which vehicle should visit which stops, in what order? This is the Vehicle Routing Problem (VRP). Google’s open-source OR-Tools documentation describes it as finding optimal routes for a fleet of vehicles visiting a set of locations; with a single vehicle it reduces to the travelling salesperson problem.
OSRM’s Trip service gives a sequence for a single vehicle, but it knows nothing about time windows, vehicle capacity or multi-vehicle distribution. When those matter, the duration and distance matrix OSRM produces becomes input to an optimisation layer such as OR-Tools.
What each service does
OSRM’s HTTP API offers six services:
| Service | What it does | Use in logistics |
|---|---|---|
| Route | Finds the fastest route between the given coordinates | Total mileage and duration of a trip; drawing the route on a map |
| Table | Computes durations or distances between every pair of the given points | Distribution planning; input for optimisation |
| Trip | Solves the travelling salesperson problem with a heuristic | Stop sequence for one vehicle |
| Nearest | Snaps a coordinate to the nearest point on the road network | Correcting coordinates that fall inside buildings or off-road |
| Match | Snaps noisy GPS traces to the road network in the most plausible way | Recovering the road actually driven from vehicle-tracking data |
| Tile | Generates map tiles | Visual inspection of routing data |
Two details matter. First, the OSRM documentation states plainly that Table distances are not the shortest distance but the distance of the fastest route; durations are in seconds and distances in metres. Second, the Trip service uses a heuristic (farthest insertion) for 10 or more waypoints and brute force for fewer than 10; the documentation notes that the result is an approximation and the returned path need not be the fastest.
Setup: your own routing server in three steps
OSRM’s official guide recommends Docker. First download OpenStreetMap data for your region (regional extracts are published by sources such as Geofabrik), then pre-process the data and start the server. The example flow from the guide is below; the file name depends on your region:
# 1. Pre-process the data with the car profile
docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend \
osrm-extract -p /opt/car.lua /data/region-latest.osm.pbf
# 2. Partition and customise for the MLD pipeline
docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend \
osrm-partition /data/region-latest.osrm
docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend \
osrm-customize /data/region-latest.osrm
# 3. Start the HTTP server
docker run -t -i -p 5000:5000 -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend \
osrm-routed --algorithm mld /data/region-latest.osrm
The guide describes two pre-processing pipelines: Contraction Hierarchies (CH) and Multi-Level Dijkstra (MLD). MLD is recommended by default; for special cases such as very large distance matrices, CH is still the better fit for the time being. For CH, a single osrm-contract step replaces partitioning and customising, and the server is started with --algorithm ch.
A query looks like this; coordinates are written as longitude, latitude, and putting latitude first lands the point in the wrong place:
curl "http://127.0.0.1:5000/route/v1/driving/28.97,41.01;32.85,39.93?overview=false"
Why your own server?
We run OSRM on our own server in the logistics platform for two reasons: distance and route calculations should not depend on an outside service, and reports should be repeatable. The same query on the same data gives the same mileage; that matters for work with financial consequences, such as monthly mileage reports and intercity trip calculations.
Public servers’ usage rules are another reason. OSRM’s demo server policy does not allow excessive use, says clients affecting the service will be blocked, and requires clear attribution of the ODbL licence and of OSRM as the source of routes. The demo server is for trying things out, not for production load.
An address is not a coordinate
OSRM works with coordinates; it does not understand an address such as “Kadıköy, Istanbul”. Turning an address into coordinates (geocoding) is a separate step, and most errors in logistics start there: incomplete or misspelt addresses, coordinates that land in the wrong district. Public geocoding services such as OpenStreetMap’s Nominatim set strict limits; Nominatim’s policy allows an absolute maximum of one request per second and discourages bulk geocoding. So:
- Geocode an address once and store the coordinate with the record; do not look it up again for every route calculation.
- Addresses whose coordinate remains uncertain should go to a list for a person to check before they enter route calculations.
- The actual location captured at delivery can be used to correct an address’s coordinate for later shipments.
Licence and attribution
The OSRM software is licensed under BSD-2. The OpenStreetMap data it uses is licensed under the Open Database License (ODbL), which requires attribution to OpenStreetMap. Every screen that shows a route on a map should carry the “© OpenStreetMap contributors” credit. Even in a system that only produces mileage reports, documenting which data a report is based on is good practice.
What to watch in production
- Data freshness. Road networks change. Record when the map data was downloaded and set a regular update schedule; after an update, the same route can give a different mileage, so account for that in reports.
- Memory. Pre-processing needs considerable memory as the region grows; in the guide’s example, extracting a 550 MB country file took about 30 minutes. Size your server by testing with your own region.
- Access. The routing service should stay on the internal network, reachable only from the application server. An OSRM instance open to the internet means others computing on your server.
- Failure handling. The application must know what to do with points for which no route is found (coordinates on islands or not connected to the road network); silently writing zero kilometres is the most dangerous error a report can contain.
- Caching. If the same pairs of points are queried often, caching results improves both speed and consistency.
Where OSRM fits in a logistics system
OSRM is a mature, open-source way to calculate distances, durations and routes on your own server, repeatably. Its most valuable uses in logistics are trip mileage with Route and distribution-planning input with Table. Address-to-coordinate conversion, multi-vehicle optimisation and map-data freshness should be designed as separate layers. Where route calculation fits in a logistics system as a whole is covered in logistics management software features.