Inside Glovo’s deliveries optimization

Intro

Bonus: what is efficiency?

Figure A
Figure B: a naive assignment
Figure C: an efficient assignment
Figure D: assignment matrix of the example

The p̶r̶o̶b̶l̶e̶m̶ idea

  • the maximum overall distance assignable to a courier
  • food getting cold due to the extra waiting time
  • meeting the promised delivery deadline
Figure 1: S=Start, P=Pickup, D=Delivery
Figure 2

The technical solution — Architecture

  • We publish ~20 million order assignment decisions per month.
  • All our calculations are based on estimations, like the two we saw in the introduction: time to prepare an order, and time required to travel. That’s why we ask our estimation services for ~5 billion estimations each month
  • We are expected to not delay the order assignment for more than 10 seconds with our processing, so we work with a time window of 10 seconds in each city.
  • The assignment matrix (free couriers against Order/Bundle to be assigned) can grow up to 1k*2k resulting in ~2M of potential combinations to consider in each time window (every 10 seconds).
Figure 3
Figure 4

Bundle Generator(s)

  1. load all the orders’ data of the city from a read replica, to avoid putting pressure on the main node;
  2. run the VRP algorithm with all the constraints applied to calculate the best combinations of orders into bundles;
  3. save the bundles in Redis temporary storage, eventually overriding the previous execution of the same city.

The Optimizer

  1. Loads the status of the city;
  2. Loads and validates the bundles proposed by the generators (validation is required as the bundle generation happens concurrently and based on the asynchronous read replica, so it could be out of date);
  3. Create the “matrix” of assignments (orders VS couriers available) checking all the eligibility constraints;
  4. Loads (or asks, if there aren’t) all the required estimations;
  5. Calculates the cost function for every eligible assignment;
  6. Translate the problems into a standard optimization formulation (Minimum cost flow or Mixed Integer Problem);
  7. Solve it, and translate it back to the service domain;
  8. Apply custom domain logic, like manual assignment requests;
  9. Save and publish decisions: This step involves the commitment of the Bundle chosen, the rest of the Glovo ecosystem will then know that this Bundle has been created (can still be modified in the future);

Conclusion: the hard reality

  • some stores may have dedicated couriers to them, they may be able to use both the dedicated ones and the others or even share the dedicated ones with the nearby stores, all depending on the specific store configuration;
  • the constraints mentioned on the eligibility of a courier to a particular Order are just a small subset of the real ones;
  • We have to deal with many different types of orders, food and non-food, with potentially different optimisation requirements (for example: “getting cold” is a food-only constraint);
  • the estimations can’t be always accurate, and so there are features in place, like hot-swapping of the order, to compensate for that;
  • a courier is free to reassign any order, so the same order can be considered in the optimization cycles many times, not just once;
  • a decision taken too early is likely suboptimal, estimations are trying to “predict the future” and the closer the future, the more accurate they are, so we always try to postpone as much as possible every decision;
  • Our system makes decisions, but there’s no guarantee that they’ll be applied to reality: a millisecond after a decision is taken, the courier’s phone may run out of battery, a bike tire can break or the customer could cancel the order… we have to keep following the “actual” status of the world and keep trying to make decisions to chase efficiency;

--

--

Read about our biggest technical challenges and what it´s like to work at Glovo. https://engineering.glovoapp.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ema

Passionate about writing performant, reliable, scalable and maintainable software and, as a consequence, I'm a functional programming enthusiast.