Lunar Flow | Google Maps API Response Caching for Speed and Cost

The Google Maps API is one of the most commonly used APIs for handling location-based information in countless applications. It offers a wide gamut of location based services including geocoding, route planning, POI lookup, and map previews. However, as your app scales, the Google Maps API can become expensive - with each request you make driving up your API bill.
Caching is a great strategy to reduce the load of requests you are making to the Google Maps API - enabling you to keep the results of common queries and reduce your costs, especially with repeated requests. It can also improve your apps performance by serving data out of a fast cache instead of making a higher latency API call. 

Relevant APIs:

1. [REQUEST] Cache Read - This processor accesses your Lunar cache to check if there’s an existing cached response for the API. If there is one, it’ll pass a response in the ‘cache_hit’ stream, otherwise it’ll trigger a ‘cache_miss’. The two parameters for it are:

  • Cache Group - allows you to create different groups of segregated cached data.

  • Cache Key - this is the key for looking up responses in the cache. Here we just pass the endpoint + all the query parameters. You can customize this behavior to control the cache.

2. [RESPONSE] Cache Write - If we don’t get a cache hit - but rather get a response from the downstream API, this processor will save that request to the cache. The cache group and key parameters correspond to those ones in the cache read processor, and must be the same for the cache mechanism to work. Additionally, we introduce an expires_in parameter which dictates how long the response should be cached for, in seconds (24 hours in this example).

Prerequisites

  • Have the Lunar proxy installed and configured in your environment - tutorial

  • You have the Lunar interceptor installed at the service from which you are consuming the Google Maps API.

When to Use This Flow:

This flow can help reduce the cost for any application relying heavily on the Google Maps API. It can have a significant impact on applications where duplicate or similar requests are made often. This can be especially useful for apps where users are likely to make repeated requests - for instance every time they open the app - with the same data. Because address / physical information rarely changes, you can keep the cache alive for longer periods of time and thus maximize your cache ROI.

You should spend some time customizing the cache expiration based on your app’s use case and the type of information you get from the API. For instance Geocoding and address validation requests can be cached for a long time given addresses rarely move around. But route planning - which takes road closures and traffic into account - could become stale and out of date if cached for too long.

About The Google Maps API:

The Google Maps API is a powerful suite of services offered by Google that allows developers to incorporate complex mapping and location-based functionalities into their websites and applications. Leveraging the comprehensive and continually updated maps and data from Google, the API provides a wide range of capabilities designed to facilitate navigation, visualization, and understanding of geographic information. Here's an overview of some of the key features and capabilities the Google Maps API offers:

Google Maps API Pricing:

The Google Maps API charges based on the number of requests you make. As you can see in the table below, many requests have significant costs - especially if you scale your application to thousands or millions of users, all of whom could be triggering multiple API requests every time they use the app.

API

Aerial View API

Static Maps API (Image URLs)

Street View API

Nearest Roads

Compute Routes - Basic

Compute Routes - Advanced

Compute Routes - Preferred

Address Validation

Geocoding

Geolocation

Places Details - Baic

Places Details - Advanced

Places Details - Preferred

Cost per 1,000 Requests

$16

$2

$7

$10

$5

$10

$15

$17

$5

$5

$17

$20

$25

You can use the interactive pricing calculator on the Google Maps API website to estimate your future Google Maps API costs.

Route Planning API

One of the most prominent features of the Google Maps API is its ability to plan routes. This functionality enables users to get directions and plan routes between two or more locations.

The API supports various modes of transportation, including driving, walking, cycling, and public transit, providing estimated travel times and distances. It can also account for real-time traffic conditions to offer the fastest route.

Static Map URLs API

The API allows for the generation of static map images through simple URLs. These maps can be customized in various ways, including setting the zoom level, center point, size of the image, and the inclusion of markers or paths. This feature is particularly useful for applications that require a simple map presentation without the overhead of a dynamic map interface.

Elevation Data API

Google Maps API provides elevation data, allowing developers to determine the elevation of specific points on the Earth's surface. This feature can be used for a wide range of applications, from analyzing geography and planning construction projects to enhancing fitness apps that track cycling and hiking routes.

Geocoding API

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (latitude and longitude), which can be used to place markers on a map or position the map.

The Google Maps API offers robust geocoding capabilities, making it easy to integrate address-based searches and location plotting in applications.

Reverse Geocoding API

Reverse geocoding, the opposite of geocoding, involves converting geographic coordinates into a human-readable address or place name.

This is particularly useful in applications that track location through GPS coordinates and need to display location information in a format understandable to users.

Geolocation Based on Cellular and WiFi Networks API

The Google Maps API can determine a device's location using cellular and WiFi network information. This feature enhances location accuracy, especially in urban environments where GPS signals may be weak or obstructed by buildings.

It's instrumental in location-based services and apps that need to offer personalized content or directions based on the user's current location.

Roads Data API

The API includes access to detailed data about roads, including names, types, and conditions. This information can enhance mapping solutions that require detailed road data, such as logistics and delivery services apps, driving direction services, and more.

About Lunar.dev:

Lunar.dev is your go to solution for Egress API controls and API consumption management at scale.
With Lunar.dev, engineering teams of any size gain instant unified controls to effortlessly manage, orchestrate, and scale API egress traffic across environments— all without the need for code changes.
Lunar.dev is agnostic to any API provider and enables full egress traffic observability, real-time controls for cost spikes or issues in production, all through an egress proxy, an SDK installation, and a user-friendly UI management layer.
Lunar.dev offers solutions for quota management across environments, prioritizing API calls, centralizing API credentials management, and mitigating rate limit issues.

Table of content

Talk to an Expert

Got a use case you want to get help with?

By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.