Making a Hard-to-Cache Map Query Cacheable: From Viewport Coordinates to Tiles

Hello, I’m Jeong Jeong-il, a backend developer working at a PropTech platform.
This time I’d like to talk about adding a cache to the map query in our service. Our map has an API that looks up the apartment complexes inside the viewport every time the user moves the map. Because this API took the viewport coordinates as they were, every request was different, and it was hard to use a cache. So we changed the unit of the request itself to make caching possible, and I want to write down what I had to think through along the way.
The approach in this post is not new in itself. Splitting map images into tiles has been around for a long time. But applying it to our query API meant deciding quite a few things ourselves, such as the tile size, the cache TTL, and when to start the query. So this post focuses on those decisions and also covers what we confirmed after rolling it out to production.
Why Is It Hard to Cache a Query by Viewport Coordinates?
On our map, apartment complexes are shown as markers with their size and price.

The map on our production site.
Previously, this query used the coordinates of two corners of the viewport. You send the southwest and northeast corners, and the server finds the complexes between them and returns them.
| |
I think this is the first design that comes to mind when building a map query API. It queries exactly what is in the viewport, so nothing is wasted.
The problem was that every request is different.
Even when users look at the same neighborhood, their screen sizes differ, and moving the map by a single pixel changes the decimals of the coordinates. A cache only helps when the “same request” comes again, and with viewport coordinates the same request almost never came up twice. So even the common case of moving the map a little and coming back went all the way down to the DB every time.
I had been aware that markers on our map were appearing with a delay, and after wrapping up a large project, the integrated real estate AI agent, I came to look into it.
So how long was it actually taking? On our production site, I measured the time from letting go of the map to the new markers being drawn, five times each, and ours took about 0.7β1 second. I measured other real-estate map services the same way and they were under 0.2 seconds. This time included not only the round trip to the server but also the wait before the query even started, which I’ll come back to later.
Changing the Request Unit to Tiles
I decided that instead of fitting the request to the user’s viewport, we had to align it to a predefined grid of cells.
When map services serve map images, they divide the world into equally sized cells per zoom level and have clients request images by a cell number, z/x/y. These cells are called tiles.
But tiles are not the only way to define cells. These are the options I looked at.
| Option | Upside | Downside |
|---|---|---|
| Round viewport coordinates to a fixed step | The existing API can be kept almost as it is | You have to define the cell size and the per-zoom levels yourself, and levels don’t nest |
| Geohash | A cell is a single string, and stores like Redis support it natively | Each character splits a cell into 32, so the gap between levels is large, and the cell’s aspect ratio changes with the string length |
| Tiles (z/x/y) | Each zoom level splits a cell into exactly 4, and it is already widely used for maps | You have to compute which tiles the viewport covers, and one request becomes several |
Geohash is convenient for storing a location as a string or finding nearby places. But in a case like ours, where the query unit has to change with the map’s zoom, I thought a cell size that changes 32-fold per character would be hard to work with.
I chose tiles for three reasons.
First, the tile number is the request address, so the browser, the CDN, and Redis can all cache under the same key.
Second, one larger tile contains exactly four tiles of the next, more detailed zoom level. When we already have the larger tile, a zoomed-in viewport can be drawn from it, and our in-memory cache on the page uses it this way.
Third, map image servers and vector tile servers already use the same scheme, so there was plenty of material to refer to.
We decided to use this grid as it is for the complex query.
| |
The frontend computes which tiles the viewport covers, requests only those, and merges the returned tiles and draws them on the map. The server computes the actual range each tile number covers.

Two users looking at roughly the same area.
With this change, people looking at the same neighborhood send the same address for every tile their viewports share. Even with a different screen size, or after moving the map a little, the requested tiles stay the same as long as you don’t cross a tile boundary.
Of course there are downsides. Since we fetch whole tiles that the viewport touches, we also receive complexes outside the viewport, and one request becomes several. So I thought quite a bit about how big the tiles should be.
Deciding the Tile Size
Smaller tiles mean less off-screen data, but more requests per viewport. Larger tiles mean fewer requests but a lot of data nobody looks at. Looking at the cache alone, larger tiles reduce the number of distinct tiles and raise the chance of a cache hit, but that didn’t mean we could just make them as large as possible.
Each time the zoom drops by one level, a tile covers four times the area. Dividing the same viewport by tiles at three zoom levels looks like this.

The same viewport divided by three tile sizes.
Here I had to decide which to reduce: the number of requests, or the weight of each request. Several tiles are fetched at the same time, so the time until the viewport is fully filled is set by the one tile that arrives last. The more tiles there are, the more likely it is that one of them is late. But larger tiles hold more complexes each, which makes every request heavier. When I measured in our development environment, a request for a tile about the size of the viewport took 0.1β0.2 seconds, while a tile covering a much wider area took 0.6β1 second.
So at the zoom levels where complex markers are shown, we use tiles one zoom level lower than the map. The same zoom means too many tiles, and two levels lower we would fetch several times the viewport’s area every time it straddles a tile boundary. One level lower fills one viewport with about 2β6 tiles in most cases. And when a large viewport would need too many tiles, we skip the tiles and query the whole viewport range in one request, as before.
| |
The map zoom moves in fractions like 14.3, so we floor it to an integer for the tile zoom. As a result, map zoom 14.0 and 14.9 use tiles of the same zoom, which is also why the number of tiles per viewport varies between 2 and 6.
Filters and the Cache Key
Our map has filters such as size, price, and move-in year. Where to apply the filters was another thing to decide. I also considered fetching only unfiltered tiles and filtering them on the page, which would let everyone share the same cache regardless of filters. But that would push the filtering work onto the user’s device, where it depends on how fast the device is, and I wasn’t comfortable with that. So we apply filters on the server, attach them as query parameters, and include them in the cache key as they are.
The cache key is built by joining the deal type (sale, jeonse lease, and so on), the tile number, and the filter values in a fixed order, because the same filters have to give the same key even when the query parameters arrive in a different order. For the browser and the CDN the address itself is the key, so the frontend also always attaches the parameters in the same order. We also put a version at the front of the key, so that if the response format changes later, bumping the version keeps old cache entries from mixing in.
What worried me here was that the number of cache keys grows with the number of filter combinations. Keys could multiply to the number of tiles times the number of filter combinations, so I wondered whether the cache would be split so finely that hits would almost never happen.
We could only confirm this after rolling out to production, and when we looked at the real requests, 90% of them had no filter at all. There were 151 filter combinations over two days, but most requests were concentrated on the default query without filters. At least in our service, filters didn’t seem to fragment the cache much.
Putting the Cache in Four Places
We put the cache in four places, starting with the one closest to the user.
flowchart LR
A["Map page<br/>in-memory cache<br/>5 min"] --> B["Browser cache<br/>60 s"]
B --> C["CDN cache<br/>5 min"]
C --> D["Redis<br/>5 min (+0β30 s)"]
D --> E[("DB")]
The setup when we first rolled it out.
- In-memory cache on the map page: the page holds the tiles it received for up to 5 minutes. Moving the map a little and coming back is served from here, so no request is sent at all.
- Browser cache: used when the page is reloaded or reopened. A browser cache can’t be purged from the server once it has been delivered, so we keep it for only 60 seconds.
- CDN cache: if someone else recently viewed the tile, the CDN answers without reaching our server.
- Redis: even when the CDN doesn’t have it, the server answers without querying the DB.
The first two only reuse what the same user received, while the last two (CDN and Redis) are shared caches used by many people together.
The TTLs for the browser and the CDN are set by the server through a response header.
| |
For Redis, we added 0β30 seconds of jitter (a random amount of extra time) to the TTL. The several tiles of one viewport are stored at almost the same moment, so the jitter shifts their expiry times slightly, to keep them from expiring together and sending queries to the DB all at that instant. And since I thought the map should still work even if Redis has a problem, a failed Redis read or write falls back to reading from the DB.
How Stale Can the Data Get Across Several Layers?
A cache delays how soon data changes show up. And with several layers, it turned out this delay did not end at 5 minutes.
Here is the worst case. Redis may hold data that is just about to expire, which is up to 5 minutes 30 seconds old. If the CDN picks up that data, it keeps it for another 5 minutes, and the page holds what it received for 5 more. The browser cache counts its 60 seconds including the age the CDN reports, so we didn’t add it separately. Adding these up, in the worst case the data could be 15 minutes 30 seconds late.

The worst case, where each layer picks up data that is just about to expire.
I didn’t think the prices shown on map markers need to be accurate to the minute, so I decided to accept this. The detail screen you open by selecting a complex is queried separately and isn’t affected by this cache.
When Should the Query Start?
After adding the tile cache, I measured again. But even when I moved a little, came back, or zoomed in within tiles we already had, and no request was sent at all, markers still took 0.2β0.7 seconds to appear.
I hadn’t planned to look at the query timing at first, but since it was taking time without even going to the server, I started looking at the time before the query starts as well.
The moments when a query can start on a map are summarized in the table below.
| When | Upside | Downside |
|---|---|---|
| After the map has fully stopped | The viewport is final, so no wasted requests | If the idle signal arrives late, the query starts that much later |
| The moment you let go | No waiting for the idle signal | If the map keeps gliding with inertia after you let go, the viewport changes |
| While dragging | Data can be ready before you let go | May fetch data the user never sees |

The three moments are marked with the moment of letting go at 0. The position of the idle signal is what we measured on the map SDK we use.
We originally used the first approach. The 0.2β0.7 seconds above also included actions such as coming back and zooming in, so I measured separately, for drags only, when the idle signal arrives. On the map SDK we use, it only arrived 0.36β0.46 seconds after letting go. For a drag, most of the time until the markers appeared was this wait.
Of course, if the map keeps gliding with inertia after you let go, waiting for the idle signal is the right thing to do. But when I checked, our map had no inertial movement. The viewport at the moment of letting go was already the final one. So I decided there was no need to wait for the idle signal and moved the query to the moment of letting go.
We also added the third approach, because switching the request unit to tiles made it possible to prefetch the tiles that will be needed next. When querying by viewport coordinates, we had to know where the user would stop before we could build the request. But tile numbers are fixed, so we can compute in advance which tile is to the right of the current viewport, or which tile we’d need after zooming out one level.
The wider the prefetch range, the more likely the next move lands inside what we already have, but the more tiles we fetch that nobody looks at. So we set the range case by case.
While dragging, we know the direction of movement, so every 0.1 seconds we fetch only the tiles for the current viewport and for one viewport ahead in that direction. After the map stops and the markers are drawn, we can’t know where the user will go next, so we fetch only a margin of half a viewport around it. Long moves are already covered by prefetching during the drag, so I decided it was enough to fetch for small moves after stopping.
For zoom, we fetch only the tiles for one zoom level out. As mentioned earlier, zooming in can be drawn from a larger tile we already have, but when zooming out changes the tile zoom, nothing we have fetched can be used. It was the slowest case in my measurements, too.

The blue outline is the current viewport, the dashed orange outline is the range we want to prefetch, and the shaded cells are the tiles actually requested. Tiles for one zoom level out are not drawn.
If we drew prefetched data right away during a drag, markers would flicker on a moving map. That didn’t look good, so during a drag we only store what we receive and draw only after letting go.
Limits on Prefetching
That said, tiles fetched by prefetching may never be seen by the user. It uses more of the user’s data and our server resources, so we put a few limits in place.
- The number of prefetch requests running at the same time is capped; the rest wait in a queue.
- The queue length is capped as well; when it overflows, the oldest are dropped first, because if the map has already moved elsewhere the old requests are no longer needed.
- When a request’s turn comes, it is skipped if the tile has already been received.
- If the browser is in data-saver mode or on a slow connection, off-screen tiles are not fetched.
What We Saw in Production
After the rollout I measured again the same way on the production site. The time from letting go of the map to the markers appearing went from 0.7β1 second to 0.01β0.12 seconds. It was 0.01β0.04 seconds for drags of 0.5 seconds or longer, and 0.06β0.12 seconds for very short drags. These are values for moving by dragging; when there are no fetched tiles, such as jumping to a distant place by search, we still have to wait for the server as before.

Each range is the minimum and maximum over several measurements.
Before the rollout, what worried me most was the number of requests. One request was now split into several tiles, with prefetching on top. Since one viewport usually needs up to 6 tiles, I first assumed requests would grow sixfold with no cache hits at all and calculated whether the server and the DB could take it. Over a whole day there was headroom, but I thought moments when requests pile up at once could be a burden, and that is why we added the concurrency limit I mentioned above, so that prefetching doesn’t pile on at the same moment.
Comparing a single day, three days after the rollout, with a single day before it, map queries reaching the server grew by only 17%, and DB CPU usage did not go up. Requests must have grown far more than that from the tile split, so the rest seems to have been absorbed by the in-memory cache on the page.
Hit Rate of the Shared Caches
The shared caches, however, had a lower hit rate than I expected. Of the tile requests that reached the server, the share answered straight from Redis was 2β3%. The CDN was similar: even the tiles requested most the day before were rarely answered straight from the CDN.
Why was it this low? Looking at the request logs, of the distinct tile-and-filter combinations requested over two days, nearly half were requested only once. Even the 20 most requested ones together made up only 8% of all requests. Counted by requests, 31% of all requests over the two days were the first request for that tile-and-filter combination, and such requests cannot hit no matter what the TTL is.

How many times the same tile-and-filter combination was requested over two days.
With requests spread this widely, it seems there just weren’t many cases where another person requested the same tile within the 5-minute TTL.
The client-side caches, which reuse what the same user already received, seem to have been a different story. A single user looking at the map keeps moving around the same area. I think the time to show markers now comes down to starting the query earlier, the in-memory cache on the page, and prefetching.
The time saved by a cache hit also differed by layer.
| Where the cache hit | Time to response |
|---|---|
| In-page memory | No request |
| CDN (measured by requesting the same tile back to back) | 41β60 ms |
| Redis (server processing time) | median 9β10 ms |
| Miss everywhere, DB query (server processing time) | median 22β26 ms |
| Full response when everything misses | 128β268 ms |
The DB query itself is already fast, so answering from Redis only saved a little over 10 ms. The time to travel to the server and back was far larger, so a hit in a cache closer to the user, like the CDN or the page, made a bigger difference.
Thinking About TTL
Then how much would the hit rate rise with a longer TTL?
We could calculate this from the real request logs. We lined up two days of tile requests in time order and counted a hit whenever the same tile-and-filter combination came again within the TTL after being stored in the cache. The first day was used to warm the cache and only the second day was counted. With 5 minutes the result was 2.5%, almost the same as what we observed in production, so I ran the calculation for other TTLs.
Of course, 5 minutes is the only point I checked against the production value. The calculation also only covers requests that reached the server, so the longer the TTL, the more these numbers should be taken as a rough guide.

Estimated hit rate by TTL, calculated from real request logs (the horizontal axis is not evenly spaced). This is for a single central cache like Redis; a CDN split by region would likely be lower.
Even at 1 hour it was about 15%, and it took a 12-hour TTL for half of the requests to hit.
So should we just make the TTL long? A longer TTL would reduce the requests reaching the server and the DB, and with more answers coming from the CDN, even places a user visits for the first time would respond faster. However, price changes would show up on the map that much later. As we saw earlier, delays add up across layers, so with 6 hours each on Redis and the CDN the data could be more than 12 hours late in the worst case. The time during which a marker on the map and the detail screen show different values would grow just as much.
I went back to what the TTL should be based on in the first place.
We started with a TTL of 5 minutes. But after looking at the request logs, I came to think we should first look at how often the data changes. The price data used for map markers is refreshed once a day. As long as the data hadn’t changed, there was no reason to throw the cache away.
So instead of discarding entries after a fixed time, we changed direction so that the cache is refreshed together with the price data. But the layers differ in how easily they can be purged, so the same approach couldn’t apply to all of them.
| Cache | Used by | Purge from the server | Approach |
|---|---|---|---|
| In-page memory | One user | Not possible | Keep short (5 min) |
| Browser | One user | Not possible | Keep short (60 s) |
| CDN | Many users | Needs a separate request | Long TTL, purged when prices are refreshed |
| Redis | Many users | Immediate | Long TTL, purged when prices are refreshed |
This way the shared caches build up over the day, while a price change takes at most the few minutes the client-side caches last to reach the map.
Then the role of Redis changes too. With a 5-minute TTL, Redis almost never answered, but once entries build up over a day, more of the requests that miss the CDN and reach the server can be answered without a DB query. And since Redis is the only shared cache the server can purge right away, I thought it was right to keep this layer.
To purge Redis, instead of deleting keys one by one, we chose to put a data version in the cache key. When the price data is refreshed the version changes, so later requests go to new keys and the old keys simply expire with their TTL. It is the same approach as the version we put in the key earlier, in case the response format changes. For the browser and the CDN, however, the address itself is the key, so changing a key inside the server can’t purge them. Putting a version in the address as well is possible, but then the page first has to know what the current version is. So for the CDN we separately request a purge of the tile paths once the price data refresh has finished.
But if the cache is purged all at once on refresh, won’t queries pile onto the DB? I didn’t expect it to be a burden at our current request volume. Even now, most requests reaching the server are not in Redis and go on to query the DB, and DB CPU usage did not go up in that state. Right after a price refresh, we simply go back to that same state for a while. Of course, if requests grow well beyond today’s level, I think this is something to look at again.
In the earlier calculation, keeping entries for 24 hours gave 61%. Since the cache is purged on every refresh, I think the number will come out lower than that. I plan to adjust the exact TTL values after running this in production a while longer and looking at real user requests.
Closing
That was a look at what I had to think through while switching our map query to tiles.
In this work, I think aligning requests to predefined tiles instead of viewport coordinates was the starting point that made caching possible. Even outside maps, an API that is hard to cache because each request has slightly different values might be able to use a similar approach.
Looking back, the query timing, the client-side cache, and prefetching did more to make the markers appear quickly than the shared caches did. But the client-side cache and prefetching were only possible because the request unit was fixed to tiles.
I also think it mattered that, after adding the cache, we checked in the request logs whether it was actually getting hits. I learned from the logs that the shared caches had a lower hit rate than expected and that a 5-minute TTL didn’t fit our data.
I hope this post is of some help to those working through similar problems.
Thank you for reading.