Deploying Open Source Routing Machine (OSRM)
Introduction
Open Source Routing Machine (OSRM) is a high-performance routing engine designed to work with OpenStreetMap (OSM) data. Written in C++, OSRM can calculate the fastest route between coordinates in milliseconds, making it ideal for applications requiring real-time routing such as logistics, ride-sharing, delivery optimization, and navigation apps.
OSRM uses a technique called Contraction Hierarchies for extremely fast shortest path queries. The engine preprocesses the road network data into a compact graph representation, enabling routing queries that are orders of magnitude faster than graph-based algorithms on raw data.
Key features of OSRM include:
- Blazing Fast Performance: Sub-millisecond routing queries for continental-scale networks
- Multiple Routing Profiles: Support for car, bicycle, and foot routing with customizable profiles
- HTTP API: RESTful API compatible with many mapping libraries
- Table Service: Calculate distance/time matrices for many-to-many routing
- Trip Service: Solve traveling salesman problems with optimal waypoint ordering
- Match Service: Snap noisy GPS traces to road network
- Tile Service: Vector tiles for route geometry visualization
- Open Data: Uses freely available OpenStreetMap data
- Self-Hosted: Full control over your routing infrastructure
This guide walks through deploying OSRM on Klutch.sh using Docker.
Why Deploy OSRM on Klutch.sh
Deploying OSRM on Klutch.sh provides routing infrastructure for your applications:
Privacy and Control: Keep routing queries private rather than sending them to third-party services.
No Rate Limits: Unlimited routing queries without API quotas or throttling.
Persistent Storage: Store preprocessed map data with persistent volumes.
Scalable Resources: Allocate CPU and memory based on your geographic coverage and query volume.
Cost Effective: Avoid per-query pricing from commercial routing APIs.
Prerequisites
Before deploying OSRM on Klutch.sh, ensure you have:
- A Klutch.sh account
- A GitHub account with a repository for your deployment
- OpenStreetMap data extract for your region (from Geofabrik)
- Significant storage for preprocessed data (varies by region size)
Deploying OSRM on Klutch.sh
Prepare Your Map Data
Download an OSM extract from Geofabrik. For testing, start with a small region. The data must be preprocessed before deployment.
Create Your Dockerfile
Create a Dockerfile in your repository:
FROM osrm/osrm-backend:latest
# Copy preprocessed data (or download and process during build)WORKDIR /data
# For a pre-processed deployment, copy your .osrm files# COPY ./data /data
# Alternatively, download and process during build (for small regions)RUN apt-get update && apt-get install -y wget \ && wget https://download.geofabrik.de/europe/monaco-latest.osm.pbf -O /data/map.osm.pbf \ && osrm-extract -p /opt/car.lua /data/map.osm.pbf \ && osrm-partition /data/map.osrm \ && osrm-customize /data/map.osrm \ && rm /data/map.osm.pbf
EXPOSE 5000
CMD ["osrm-routed", "--algorithm", "mld", "/data/map.osrm"]Push to GitHub
Commit and push your Dockerfile to your GitHub repository.
Create a New Project on Klutch.sh
Navigate to the Klutch.sh dashboard and create a new project.
Create and Configure the App
Create a new app and connect it to your GitHub repository.
Configure HTTP Traffic
Set the traffic type to HTTP with an internal port of 5000.
Allocate Resources
OSRM is CPU and memory intensive. Allocate resources based on your region size:
| Region Size | Recommended Memory |
|---|---|
| Small (city) | 2 GB |
| Medium (country) | 8 GB |
| Large (continent) | 32+ GB |
Attach Persistent Volumes
Add persistent storage:
| Mount Path | Recommended Size | Purpose |
|---|---|---|
/data | 10-100+ GB | Preprocessed map data |
Deploy Your Application
Click Deploy to build and launch your OSRM instance.
Test Your Routing API
Query your routing API:
https://your-app.klutch.sh/route/v1/driving/7.4195,43.7333;7.4185,43.7284