Introduction

I hope you are all doing well. In today's article, let's have a look at the Open Street Routing Machine, also called OSRM in short form. It is a high-performance routing engine designed to compute at street level using an OSM provider. The use cases of this OSRM are Navigation System, Logistics and Delivery, and Geospatial Analysis.

The agenda of this article.

  1. What is OSRM, why is it required, and why is it faster
  2. Initial Setup
  3. Maintenance
  4. Integration with Third-party Service
    • Real-time Traffic
    • EV Charging Station
  5. High-level Architecture
  6. Cost Comparision between another provider with OSRM
  7. Use cases example
  8. Conclusion

What is OSRM, why is it required, and why is it faster?

As everyone in today's trend, we don't know the exact location, and we used to plan our journey through a navigation system. We used to do this to avoid congestion and understand our timebound to reach the location. The same applies to commercial purposes. If we used to do commercial, it costs, that's where we need to see lots of pros and cons. and its benefits quick response, and others like (IaaS) or will it be PaaS.

Technically, OSRM uses a precomputed graph, which makes an extremely fast response engine. Also, it is primarily written in C++.

OSRM Routing Endpoints

  1. To get the routing durations
    • Request
      • Source and Destination co-ordinates
      • Sample input request: https://router.project-osrm.org/table/v1/driving/xx.xx,yy.yy(source); xx. xx, yy. yy(destination)
    • Response: waypoints and durations
  2. To get the route planner, for eg., step-by-step

Setup OSRM Environment

To set up an environment, we could classify it into two sections: one for development or lower environment, and the second is the higher or live environment.

Steps to follow

# Environment Ubuntu
1 Clone the OSRM Backend Repository git clone https://github.com/Project-OSRM/osrm-backend.git cd osrm-backend
2 Install Dependencies sudo apt update sudo apt install build-essential cmake git g++ libboost-all-dev libprotobuf-dev protobuf-compiler libosmpbf-dev libluajit-5.1-dev libtbb-dev libstxxl-dev libstxxl1v5 libxml2-dev libzip-dev libbz2-dev zlib1g-dev pkg-config libgdal-dev python3 python3-pip
3 Build OSRM mkdir build cd build cmake .. cmake --build.
4 Download the required map location from this link. https://download.geofabrik.de/. Use the below format to download country or continent data wget -O https://download.geofabrik.de//.osm.pbf
5 Configure STXXL cd ~/osrm nano .stxxl Paste the following line and save the file. disk=/tmp/stxxl,10G,syscall reference link : https://stxxl.org/tags/master/install_config.html
6 Extract the Map osrm-extract -p /path/to/your/profile.lua /path/to/your/map.osm.pbf -p profiles/.lua: This specifies the profile (e.g., car.lua, bike.lua, foot.lua). Profiles define how different routes (e.g., car, bicycle) are handled.
7 Partition the Graph osrm-partition /path/to/your/map.osrm This step divides the road network into regions, which helps to make routing queries more efficient.
8 Contract the Graph osrm-customize /path/to/your/map.osrm This step reduces the number of nodes by creating shortcut edges, speeding up long-distance routing queries
9 Finally, start the Routing Engine osrm-routed /path/to/map.osrm


Environment Setup or guidance

  1. To set a proof of concept, you could set it up using the docker desktop itself in a Windows machine, which runs in localhost:5000
  2. If you would like to set it up in the cloud, for example, I used EC2 with 16GB RAM in one of the European countries, which used only approximately 30% of the memory and disk, which won't be the same for all the countries and production-grade cases.
  3. To set the production-grade environment, it's based on the use case where you are going to use this routing service. however, I would like to add a few recommendations here
    • Memory
      • 64GB–128GB RAM for large region maps (e.g., Europe).
    • CPU
      • 8 to 16 cores for medium traffic, with the ability to scale as traffic grows.
      • High clock speed (3.0 GHz or higher) for fast query processing.
    • Disk Storage
      • Recommended SSD Storage since disk performance plays an important role while doing data processing. for eg., extracting, partitioning, and contracting.
      • At least 256GB for OSRM data storage and logs.
    • LoadBalancer
      • Nginx or HAProxy. Assume already exists or following in a live environment.
  4. The cost required to set up is approximately $12K- 18K per instance and $20-30K for a couple of instances per year basis. This is a ballpark calculation. It might increase or decrease.
  5. Please note that,
    • Simple Routing Queries (Local, Short Distance)
      • For routing within a city or a relatively small area, an 8-core machine with 64GB RAM running OSRM can handle 100 to 200 requests per second (RPS) under typical conditions. This number assumes
      • The machine is well-optimized.
      • The requests are for relatively short routes.
      • The OSRM graph has been properly preprocessed (partitioned and contracted).
      • Complex Routing Queries (Long Distance, Continental)
      • If you are calculating more complex, long-distance routes (e.g., inter-city or across countries), the number of requests per second will drop, potentially to 30 to 100 RPS, depending on the complexity of the queries.
  6. The key difference between simple, complex, and long routes in OSRM terminology
    • Simple Route: A short, straightforward route, often within a small region or city. OSRM can calculate these very quickly because it involves fewer nodes and roads.
      • Example: Routing within a small neighborhood or from one point to another within a small city.
    • Complex Route: A route that involves many decision points (intersections, waypoints), routing rules (turn restrictions, traffic), and dense networks.
      • Example: Routing through a densely populated city with many restrictions or many stops.
    • Long Route: A route that spans a large geographic distance, possibly over hundreds or thousands of kilometers.
      • Example: Cross-country or intercity routes covering a vast area.
  7. Performance factor
    • Simple routes are quick to compute because the search space is small, and the routing graph is limited to a smaller geographic area.
    • Complex routes take longer because OSRM must evaluate a larger number of potential paths, especially in dense, complicated networks.
    • Long routes take longer because the search space grows significantly with the distance, even though OSRM optimizes performance using partitioning and contraction hierarchies.

Maintenance

We need to consider a few things in the maintenance phase, such as data updates regularly, which we could bring automation to do this mechanical process, and this should be done during business offline hours.

Typically these are the following things involved in maintenance phases are

Integration with Third-party Service and High-level Architecture

Integrating OSRM (Open Source Routing Machine) with a third-party service like OpenEVCharge (a service that provides data on Electric Vehicle (EV) charging stations) can enhance routing services by allowing users to find optimized routes based on both road networks and the availability of charging points. This type of integration can be particularly useful for Electric Vehicle (EV) route planning, where charging stations must be factored into the route.

OpenEVCharge

Integration workflow

The integration can be broken down into several key steps.

Step 1. Initial Route Calculation (OSRM)

Step 2. Determine EV Charging Needs (OpenEVCharge)

Step 3. Incorporating Real-Time Traffic Data (TomTom)

Step 4. Final Route Calculation and Adjustments

Example API call for a route with waypoints

Step 5. Return the Final Route to the User

Cost Comparision between another provider with OSRM

Provider Estimated Latency Cost Structure Hosting & Maintenance
OSRM 0-50ms (short distance) 50-200ms (complex/long distance)

Hosting Cost: One-time setup cost for computing and storage (EBS, S3, etc.).

Monthly maintenance cost: $50–$200, depending on server size, storage needs, and data update frequency.

Self-hosted on a cloud provider (AWS)

Requires regular OSM data updates (e.g., monthly).

TomTom 30-100ms

Pay-as-you-go: - Free tier: 2,500 requests/month

Paid: $0.50 per 1,000 requests (up to 100K); $0.20 per 1,000 requests (beyond 100K)

Hosted and managed by TomTom.

No maintenance is needed from the user side.

Real-time traffic and updates are included.

HERE 20-100ms

Usage-based pricing: - Free tier: 250,000 transactions/month.

Paid: starts at $1 per 1,000 transactions.

Hosted and managed by HERE.

Advanced features (e.g., live traffic, route optimization).

Automatic data updates.

Loqate 50-100ms

Subscription and Pay-as-you-go: - Starts at $100/month with a set number of requests.

Pay-as-you-go options are available for higher volumes.

Hosted by Loqate.

Geocoding and address validation.

Regularly updated without user intervention.

Google Maps API 20-200ms

Pay-as-you-go: - Free tier: $200 monthly credit (about 28,000 requests)

Paid: $5–$10 per 1,000 requests (depending on API used)

Fully managed by Google.

Regular data updates and traffic data.

No maintenance is required on the user end.


Use cases example

Limitations

Conclusion

In summary, the Open Source Routing Machine (OSRM) presents a powerful and efficient solution for routing needs, particularly for applications requiring speed and flexibility. While OSRM excels in performance and is cost-effective as an open-source solution, it has certain limitations when compared to proprietary routing providers like Google Maps, TomTom, and HERE. Despite these limitations, OSRM can be a valuable tool, particularly in scenarios where cost and open-source flexibility are paramount. For applications requiring real-time responsiveness and extensive routing features, integrating OSRM with middleware that utilizes dynamic traffic data can bridge the gap, enhancing the routing capabilities of OSRM without extensive overhead.

In conclusion, the choice between OSRM and proprietary routing solutions ultimately depends on the specific needs of the application, including factors like budget, required features, scalability, and real-time data integration. Each approach has its strengths and weaknesses, so careful consideration is essential for optimal routing solutions.

Note. Let me create another article for full deployment infra architecture and CI/CD pipeline for the same.

Reference link

  1. https://github.com/Project-OSRM/osrm-backend/wiki/Running-OSRM
  2. https://github.com/Project-OSRM/osrm-backend/wiki/Demo-server