A note from 2026: This article was published in 2016. Hybris is now SAP Commerce Cloud, and Google Maps Platform pricing and quotas changed in 2018—the old 2,500 free Geocoding requests/day limit no longer applies; App Engine Memcache is also a legacy service, with Memorystore commonly used for new GCP designs.

A geofence is a virtual perimeter, or a fence, around a geographic, real-world area of interest. In e-commerce, these areas can be used for different purposes, from personalization to delivery cost or time calculation.

One real example from my previous e-commerce projects was related to warehouse selection strategy. It used customer address data to determine the most cost-efficient way to source a product and move it to the point of sale for local pickup or deliver it to the customer address.

Map showing warehouse shipping and sourcing zones

When the product is available at warehouse A, there is no problem: that warehouse is closest to the customer. However, if the product is only available at another warehouse, for example, warehouse B, the cost of moving the product to the customer location or a local store for pickup may be too high for the seller. In that project, the delivery price was always fixed for all products, all customers, and all regions served.

The simplest way to deal with customer and warehouse locations is to use postal codes.

Postal-code warehouse table

However, this approach is not good for the following reasons:

Geofencing helps with this task. Using a simple tool, you can mark areas on the map and process these areas differently. All customer locations inside the marked areas will be treated similarly. If you need to add an exception, you can create a new polygon and put it on top of the existing one. For any single address, you will have a list of areas. In the example above, there are two areas: red and black. All warehouses linked to the red area are primary for all customers inside this area. All warehouses linked to the black area are secondary for red area customers and primary for black area customers.

Solution

There are three components in the system:

Geofencing solution architecture

Storefront module

The storefront geofencing module is integrated into the customer address form. When the customer creates or updates the address, the following operations are performed:

The list can be used in various components, such as:

Geofence editor

Geofence editor screenshot

The geofence editor is a Google Maps-based web application that uses the Geofencing API for storing and retrieving data. Using the geofence editor, you are able to:

The editor is front-end-only; it uses the Geofencing API for uploading fences to the server and reading them from there.

Geofencing API

The architecture involves storing, indexing, and serving complex fences. Each fence is represented as a polygon along with some metadata. The key function of the API is finding all fences a certain point is in.

This process is not actually straightforward and, depending on the complexity of the fences, can include some intense calculations. That is why Google App Engine is used for the API. The following components of Google App Engine are used in the module:

The code itself and the data are hosted in Google Cloud. Depending on the complexity of the fences, more or fewer resources are needed for the calculation. For most e-commerce-related cases, the basic configuration is enough.

These are the interfaces in the API:

For the last three operations, the Java Topology Suite is used. Memcached is used for fast querying in a spatial index. The solution uses an STR-Tree, part of JTS, which is stored as a Java object in memcache for fast access.

The API is based on REST. Requests and responses are JSON.

Video

© Rauf Aliev, October 2016