Designing a Proximity Service for Location-Based Searches

Roman Glushach
2 min readMay 27, 2023

--

Location-based services (LBS) have become ubiquitous in our daily lives, from food delivery apps to ride-hailing services. One of the key features of these services is the ability to find nearby businesses, such as restaurants, cafes, and shops.

Proximity service map

Two Key Services

There are two key services involved in proximity services: the Business Service and the Location-based Service. The Business Service is responsible for adding, deleting, and updating place information, while the Location-based Service returns a list of nearby places given a radius and location.

Storing Places Locations in the Database

Geohash Algorithm Diagram

One of the challenges of LBS is how to store place locations in the database so that the LBS can return nearby places efficiently. Storing the latitude and longitude of places in the database is not enough, as calculating the distance between the user and every place would be very inefficient. One way to speed up the search is by using the geohash algorithm. The geohash algorithm divides the planet into four quadrants along with the prime meridian and equator:

  • latitude range [-90, 0] is represented by 0
  • latitude range [0, 90] is represented by 1
  • longitude range [-180, 0] is represented by 0
  • longitude range [0, 180] is represented by 1

Each grid is then divided into four smaller grids, and each grid can be represented by alternating between longitude bit and latitude bit. When searching for nearby places, we can use SQL to query the geohash index. For example, to search for nearby places, we can write SQL like:

SELECT * FROM geohash_index WHERE geohash LIKE `01%`

While geohash is a simple and effective algorithm, it has some limitations. There can be a lot of places in one grid (downtown New York), but none in another grid (ocean). Therefore, there are other more complicated algorithms to optimize the process.

Conclusion

Proximity services are an essential component of location-based services, enabling users to find nearby businesses quickly and efficiently. By using algorithms such as geohash, businesses can store location data in a way that enables LBS to return nearby businesses with minimal computational overhead. As LBS continue to evolve, we can expect to see more sophisticated algorithms and data structures that further optimize the proximity service.

--

--

Roman Glushach
Roman Glushach

Written by Roman Glushach

Senior Software Architect & Engineer Manager at Freelance

No responses yet