DEV Community

Cover image for 4D Geospatial Search with DynamoDB Vectors
Ian
Ian

Posted on

4D Geospatial Search with DynamoDB Vectors

I’m a big fan of DynamoDB vector indexes. They fit a really good space when much of my data is already in DynamoDB.
See the Official Docs for more information.

Most of the time, when we talk about vector stores, we are talking about high-dimensional (e.g. 1,024) stores, used in AI for embeddings for use cases like RAG semantic search. This is a great use case.

I was keen to explore the idea of using a smaller number of dimensions (e.g. 4) to store geospatial and temporal data. This feels like a great use case for vector indexes where geospatial data in DynamoDB does not always feel like a natural fit. No AI involved.

I'm starting this blog under the assumption that you know DynamoDB basics and have a basic understanding of vector stores. Everything else I'll build on from there.

I’ve written before about Effective Handling of Geospatial Data in DynamoDB, using a geospatial hash. This approach is still absolutely valid, but I want to try and take it to the next level with vector indexes, allowing me to support truly high-scale geospatial (and temporal) searches within my DynamoDB table.

DynamoDB is a fantastic database, but as we know, you need to define your access patterns well to make the most of it. Seemingly unstructured queries such as "tell me things that are near this position" are not well supported. Vector queries have absolutely changed that.

What I’d like to prove:

  • Are DynamoDB vector indexes suitable for geospatial / temporal data?
  • Are they accurate?
  • Are they fast!?

The easiest way to prove this: a demo project. As always, everything is built with CDK, TypeScript and is fully serverless.


The Demo: Flight Proximity

I wanted to try that idea on a question where the dimensions really matter. Two flight paths can cross on a map even if the aircraft passed hours apart or at entirely different altitudes. Given one flight, which other aircraft passed close to it, at a similar altitude, at the same time? True four-dimensional queries.

The code for this article can be found here, with a fully deployable project:

👉 GitHub Repository – DynamoDB Geospatial Vectors

The example project uses 14,444 flights and 431,356 position reports over France on 1 October 2026. I've pre-processed the flight data from adsb.lol and included it in the example repo. ADSB.lol is a great open-source project for this type of data. Their data is made available under the Open Database License (ODbL), and so is the extract in the repo.

Flight Proximity App

What I built:

  • A CDK project
  • Backend: DynamoDB database, API Gateway and Lambda functions. No authentication
  • Loading script: A script to process the flight data, loading the DynamoDB records
  • Frontend: A React project, hosted on S3 and CloudFront, used to demo the functionality

The Architecture

Everything is pretty much standard CDK, with the exception of adding the vector index itself. The L2 Table construct does not expose vector indexes yet, so they are set on the L1 CfnTable underneath:

const cfnTable = table.node.defaultChild as CfnTable;
cfnTable.vectorIndexes = [
  {
    indexName: 'position-4d',
    vectorAttribute: { attributeName: 'vec' },
    dimensions: 4,
    distanceFunction: 'EUCLIDEAN',
    projection: { projectionType: 'ALL' },
  },
];
Enter fullscreen mode Exit fullscreen mode

EUCLIDEAN is the only one of the three distance functions that means separation. COSINE compares direction from the origin and ignores how far out a point is. The choice is permanent, so get it right the first time.

And there is a minor issue with the permissions. The intent-based permission grantReadData is not enough. Searching has its own IAM action, so this has to be added. I'm sure all this will get resolved in newer releases of CDK.

Grant.addToPrincipal({
  grantee: findNearbyFlights,
  actions: ['dynamodb:SearchVectors'],
  resourceArns: [`${table.tableArn}/index/position-4d`],
});
Enter fullscreen mode Exit fullscreen mode

The architecture looks like:

Architecture

Turning the position reports into vectors

Aviation is old-skool, measuring distances in nautical miles (NM) and altitude in feet. I'm oversimplifying here and standards vary, but in normal airspace, flights must be 5 nautical miles or 1,000 ft apart at any given time. Air Traffic Control (ATC) ensure this is the case, making sure everyone stays safe.

Aircraft do not report x, y and z. They report longitude and latitude in degrees, altitude in feet and a time. The first job is to turn this into something that I can use with a vector search.

I start with a series of position reports like this one:

[ 2.82609,   49.29648,  37025,         1790866322.2 ]
  longitude  latitude   altitude (ft)  time (epoch seconds)
Enter fullscreen mode Exit fullscreen mode

I want to use Euclidean distance, the familiar straight-line distance between two points. Giving the index that raw list would be a poor choice. A degree of longitude covers less ground than a degree of latitude over France, and changing altitude from feet to metres would change the ranking. I need to choose units that make differences across the four dimensions meaningful.

Component Calculation for this report Indexed value
x Project the coordinates onto the Lambert-93 grid. Measure kilometres east of the projection’s origin at 3° E, 46.5° N. -12.653
y Take the northward component of the same projection, in kilometres. 310.777
z Multiply 37,025 ft × 0.00926, so a 1,000 ft difference carries the same weight as 5 nautical miles horizontally. 342.852
t Multiply 53,522.2 seconds since midnight UTC by 0.25, roughly the distance in kilometres a jet travels in a second. 13380.55

The altitude and time scales are design choices. Two reports from the same location and altitude, one minute apart, are 15 units apart in this index. They rank about as far apart as two simultaneous reports at the same altitude taken 15 km apart on the map. For other use cases, this would be different.

Storing the data

Every position report is stored as one item, so a flight is about 30 items that share a partition key, with the report time as the sort key. The vector is an ordinary list attribute:

{
  "PK": "FLIGHT#IBE08FA#2026-10-01",
  "SK": "POS#2026-10-01T14:52:02.200Z",
  "lon": 2.82609,
  "lat": 49.29648,
  "alt": 37025,
  "ts": 1790866322.2,
  "vec": [-12.653, 310.777, 342.852, 13380.55]
}
Enter fullscreen mode Exit fullscreen mode

There is no separate API for writing vectors. This goes in with a normal BatchWriteItem and DynamoDB updates the index in the background. Items with no vec attribute, such as the one summary item per flight, are simply left out of the index.

There is a script that is used to load the data, converting and processing the data.

npm run load-data
Enter fullscreen mode Exit fullscreen mode

Querying the data

The whole encoding is one function:

export function toVector(space: VectorSpace, point: FourDPoint): Vector4 {
  const { x, y } = projectToKm(point.lon, point.lat);
  return [
    round3(x),
    round3(y),
    round3(point.alt * space.kmPerFoot), // 9.26 km per 1,000 ft
    round3((point.ts - space.epochSeconds) * space.kmPerSecond), // 0.25 km per second
  ];
}
Enter fullscreen mode Exit fullscreen mode

A query uses SearchVectorsCommand and takes four numbers for the four dimensions, converts them to the vector and returns the nearest stored reports:

const response = await client.send(
  new SearchVectorsCommand({
    TableName: tableName,
    IndexName: 'position-4d',
    SearchVector: toVector(space, point), // [x, y, z, t]
    TopK: 100,
    ProjectionExpression: '#pk, #lon, #lat, #alt, #ts',
    ExpressionAttributeNames: { '#pk': 'PK', '#lon': 'lon', '#lat': 'lat', '#alt': 'alt', '#ts': 'ts' },
  }),
);
Enter fullscreen mode Exit fullscreen mode

TopK is how many results to return, and 100 is the most DynamoDB allows. That covers one point. A flight is a line, so the Lambda runs one search for each point along it, all in parallel, and notes every other flight that turns up.

The Limitations

The flight's own reports turn up too, since nothing is nearer to a flight than itself. Filters on a vector index only support equality, so there is no way to say "not this flight". They are dropped in code.

For my example use case, there is one big limitation. The aircraft are moving. And moving pretty fast. So with a report only every 75 seconds or so, it is not going to catch the exact closest approach.

So the search is widened and given a second step. It also runs from points interpolated between the reports, at least one every 30 seconds, and uses a generous radius to produce a shortlist of flights. For each one, a single Query loads its reports, both tracks are joined up as straight lines, and the exact closest approach is calculated. The index does the narrowing and a little geometry gives the answer.


Try it out

The project has the CDK stacks, the day of flight data, a loader, the evaluation script and the map application from the screenshot, which exists so that you can pick a flight and see the answer.

You need Node.js 22.13 or later:

git clone https://github.com/Crockwell-Solutions/cdk-geospatial-dynamodb-vectors.git
cd cdk-geospatial-dynamodb-vectors
npm ci && npm ci --prefix frontend
Enter fullscreen mode Exit fullscreen mode

To deploy it, you need credentials for an account that has been bootstrapped for CDK.

1. Deploy the three stacks and note the SiteUrl output:

npm run deploy
Enter fullscreen mode Exit fullscreen mode

2. Wait for the index. A table can be ACTIVE while its vector index is still being created, so the API has a health endpoint. Wait until vectorIndex.status is ACTIVE:

curl https://YOUR_SITE_URL/api/health
Enter fullscreen mode Exit fullscreen mode

3. Load the data. This writes 445,801 items and takes a few minutes:

npm run load-data
Enter fullscreen mode Exit fullscreen mode

4. Verify. Ask for the flight in the screenshot. You should get five encounters back, the closest being JAF6LD at 0.19 NM and 986 ft:

curl "https://YOUR_SITE_URL/api/flights/EWG9GB%232026-10-01/nearby?horizontalNm=5&verticalFt=1000"
Enter fullscreen mode Exit fullscreen mode

Then open the SiteUrl, search for a callsign or click a track, and press Analyse.

5. Clean up. This removes everything, including the table:

npm run destroy
Enter fullscreen mode Exit fullscreen mode

It costs very little. Vector indexes are billed per byte written and per byte searched, with a 1 KB minimum for each. At us-east-1 prices, loading the data is about 446,000 write request units ($0.28) plus 0.41 GB of vector writes at $0.52 per GB ($0.21), so around fifty cents. An analysis is a couple of hundred searches at $0.002 per GB, which is less than the Lambda invocation that runs them. Check the pricing page for your region.


Accuracy

A vector index is approximate, and each search only returns 100 results, so it is fair to ask whether anything gets missed. I measured it. For 1,000 flights I worked out the true answer by brute force, comparing each one against all 14,443 others, and then checked how many of those encounters the search found.

At 5 NM and 1,000 ft there were 1,002 real encounters, and the search found every one of them. It only starts to miss when I cut TopK right down and make "close" very wide. At 20 NM and 5,000 ft with just 10 results per search, it finds 82%. With 100, it is back to finding everything.

npm run evaluate -- --flights 1000            # the result above, about a minute
npm run evaluate -- --flights 1000 --sweep    # also tries lower TopK values, about ten minutes
Enter fullscreen mode Exit fullscreen mode

Performance

The performance is lightning fast. Querying for a flight makes parallel vector searches for each point along the flight, then returns and processes the results in the Lambda. All of this takes ~500ms.

With DynamoDB, given the way we have structured the queries and by not using scans, this will scale up with almost zero impact. This is the real value.


Other Options

PostGIS. A GIS engineer would reach for this, with good reason. Store each flight as a single 4D line with time as the fourth ordinate, and ST_ClosestPointOfApproach and ST_DistanceCPA return when and how close two tracks came. It is exact and it works on paths, so the sparse-report problem disappears. The price is a database server to run and the associated overhead.

No index at all. One day over France is 431,356 points. That fits in memory, where comparing one flight against all the others takes milliseconds. The index earns its place when the data does not fit in memory, is being written continuously, or already lives in DynamoDB for other reasons.

Geohashes. My earlier approach is still the a good tool for "what is in this area?". It struggles here because every extra dimension multiplies the work: finding what was near a single point means querying 9 neighbouring cells × 3 altitude bands × 3 time buckets, so 81 queries, and then throwing most of the results away.

DynamoDB vector index PostGIS DynamoDB geohash keys
"What is nearest to this point?" Built in, approximate Built in, exact Many queries, then filter
"How close did two tracks come?" Your own code Built in Your own code
"What is inside this area?" Not supported Built in What it is designed for
Filtering on a range, such as a time window No, exact matches only Yes Yes, on the sort key
What you have to operate Nothing: no servers, and it scales on demand A PostgreSQL server, or Aurora Serverless Nothing: no servers, and it scales on demand
Cost when nobody is using it Storage only The server, unless it can pause Storage only
Use it when The data is already in DynamoDB and you need nearest neighbours You need real spatial analysis You need area queries at scale

Conclusion

So, back to the three questions.

  • Are DynamoDB vector indexes suitable for geospatial / temporal data? 100%. It is four numbers on an ordinary attribute, with nothing extra to run. The thinking goes into choosing the scales, and into remembering that the index sees points, not paths.
  • Are they accurate? 100%. Every one of the 1,002 real encounters was found.
  • Are they fast? 100%. Around 500ms for a whole flight, and it will scale.

Given most of my workloads are serverless, this adds a significant capability to DynamoDB for my geospatial projects.


About Me

I’m Ian, an AWS Serverless Specialist, AWS Community Builder and AWS Certified Cloud Architect based in the UK. I work as an independent consultant, having worked across multiple sectors, with a passion for Aviation.

Let's connect on LinkedIn, or find out more about my work at Crockwell Solutions.

Top comments (0)