Introducing a new way to review your PRs.

Read the announcement

maplibre / martin

Blazing fast and lightweight PostGIS, MBtiles and PMtiles tile server, tile generation, and mbtiles tooling.

View on GitHub

22 documents · Updated Nov 23, 2026

docs / tile-serving

Tile serving

Tile serving is the shared path that turns a map tile request into a response. It presents storage and compute backends through one source model, keeps tile bytes with their format and encoding metadata, and applies caching and processing before it returns data to a map client.

Source model

The source model gives each backend one contract for identity, client metadata, tile format, tile grid, zoom limits, versioning, query use, and tile retrieval (crates/…/source.rs• L12-67). This lets the request path treat a file, database, upstream server, or generated tile as the same kind of map source.

The shared source set includes archive, database, upstream, vector-generation, raster, and query backends. A declared-grid wrapper adds the grid that stored data does not carry while passing tile reads to the wrapped backend (crates/…/any.rs• L29-69, crates/…/declared_grid.rs• L11-15).

Related backend behavior is described in file and remote tile sources, PostgreSQL tile sources, and raster terrain and transcoding.

Raster source adapters

Raster adapters turn different image stores and image catalogs into the same tile source. A Cloud Optimized GeoTIFF source can read a local file, an object store, a mosaic, a STAC search, or a GeoPackage tile index. STAC and tile-index sources select and paint source images onto the Web Mercator tile grid (crates/…/source.rs• L20-26, crates/…/stac.rs• L11-66, crates/…/tile_index.rs• L12-66).

The adapter publishes bounds, zoom limits, tile size, and served format as source metadata. It can pass through supported JPEG and WebP tiles or encode pixels as PNG or another selected format. Unsupported image formats fail source creation instead of producing a misleading tile description (crates/…/source.rs• L78-106, crates/…/source.rs• L198-244, crates/…/errors.rs• L6-26).

Archive sources use byte-range reads for local and object-store PMTiles files. A local PMTiles source checks its file identity before each read, so a changed file causes the source to reload rather than read from an old memory map (crates/…/backend.rs• L13-18, crates/…/backend.rs• L34-52, crates/…/source.rs• L195-205).

References: crates/…/source.rs• L20-26 crates/…/source.rs• L78-106 crates/…/source.rs• L198-244 crates/…/stac.rs• L11-66 crates/…/tile_index.rs• L12-66 crates/…/errors.rs• L6-26 crates/…/backend.rs• L13-18 crates/…/backend.rs• L34-52 crates/…/source.rs• L195-205

Source catalog

The source catalog is the public view of the live source set. It maps source IDs to the metadata that clients need to select a source and decode its response, such as content type, content encoding, name, attribution, and non-default tile grid (crates/…/catalog.rs• L12-55).

Source IDs can also name an ordered combination of sources. The resolver expands aliases, filters sources by zoom, and requires matching advertised tile information and tile grids so that one z/x/y address has one clear meaning (crates/…/source.rs• L122-149, crates/…/source.rs• L159-316).

References: crates/…/catalog.rs• L12-55 crates/…/source.rs• L122-149 crates/…/source.rs• L159-316

Live source registry

The live source registry connects source IDs to source handles and resolved processing rules. Reloads update the source and invalidate its cached tiles before the new source is published. Removals also remove the source's cache entries, so stale bytes do not remain available under a retired ID (crates/…/tile_source_manager.rs• L16-34, crates/…/tile_source_manager.rs• L159-248).

Requests for sources that are still loading wait until the source set is ready or the requested IDs become available. This keeps startup and reload work from producing a false missing-source result (crates/…/tile_source_manager.rs• L74-99).

References: crates/…/tile_source_manager.rs• L16-34 crates/…/tile_source_manager.rs• L74-99 crates/…/tile_source_manager.rs• L159-248

Tile representation

A tile is the byte payload plus the information needed to serve it correctly. The representation carries the tile format, content encoding, and an ETag that identifies the payload or its known upstream identity (crates/…/tile.rs• L6-29).

Empty data has a special meaning. The request path returns it as 204 No Content, while a non-empty tile receives its content type, optional content encoding, ETag, and cache headers (crates/…/content.rs• L448-484).

Tile metadata

Tile metadata has two roles. TileJSON and catalog data describe what a source offers to clients. Tile information travels with each tile so later stages can merge, convert, decode, or encode the bytes without guessing their representation (crates/…/source.rs• L20-31, crates/…/catalog.rs• L31-55).

The source version can be added to tile URLs to invalidate client caches when source content changes. A source can also declare a non-Web-Mercator grid, and requests are checked against that grid before tile data is read (crates/…/source.rs• L28-37, crates/…/content.rs• L435-447).

Raster and archive sources keep this metadata aligned with the bytes they can return. For example, a COG source changes its reported format when it re-encodes pixels, while a PMTiles source reports both the tile type and the compression used by vector tiles (crates/…/source.rs• L227-244, crates/…/source.rs• L112-168).

References: crates/…/tile.rs• L6-29 crates/…/source.rs• L20-37 crates/…/catalog.rs• L31-55 crates/…/content.rs• L435-484 crates/…/source.rs• L227-244 crates/…/source.rs• L112-168

Request pipeline

The request pipeline resolves source IDs, checks the requested grid and zoom, obtains one or more source tiles, applies configured processing, merges composite layers when needed, negotiates the output format and encoding, and builds the HTTP response. A single source follows the same path as a composite source, but avoids the merge work when there is only one tile (crates/…/content.rs• L102-119, crates/…/content.rs• L435-546).

The following diagram shows the main flow from a client request to the returned tile response.

flowchart LR
    A[Tile request] --> B[Resolve IDs and aliases]
    B --> C[Check zoom and tile grid]
    C --> D{Response cache hit?}
    D -- Yes --> H[HTTP response]
    D -- No --> E[Read source tile or tiles]
    E --> F[Pre-cache conversion or terrain processing]
    F --> G[Merge and negotiate encoding]
    G --> I[Store eligible result]
    I --> H

Format negotiation prefers the source format when it is accepted. MVT and MLT can be converted only when every source in the request supports the selected conversion. A request that names no usable format fails instead of labeling one format as another (crates/…/content.rs• L303-425).

Composite requests fetch their member tiles concurrently, ignore empty members, and join compatible MVT or MLT layers. Incompatible formats or encodings cannot form one valid composite tile and produce an error (crates/…/content.rs• L531-537, crates/…/content.rs• L747-806).

References: crates/…/content.rs• L102-119 crates/…/content.rs• L303-425 crates/…/content.rs• L435-546 crates/…/content.rs• L747-806

Processing stages

Pre-cache processing changes a source tile before the cache stores it. Its main shared use is MVT-to-MLT or MLT-to-MVT conversion selected by the client's accepted format and the source's process settings (crates/…/mod.rs• L24-63).

Terrain processing reads a 3x3 neighbourhood of source tiles and creates a new tile. Contour output is vector data. Hillshade output is raster data. The shared neighbourhood reader follows the source tile grid: built-in wrapping grids wrap across the antimeridian, while non-wrapping edges have no neighbour (crates/…/neighbourhood.rs• L25-52). Missing neighbours degrade only the edge seam because the centre tile is extended over the gap (crates/…/neighbourhood.rs• L75-84, crates/…/neighbourhood.rs• L134-179).

When a requested elevation tile is absent, contour processing can use the nearest ancestor down to the source minimum zoom. If no centre data exists, both contour and hillshade return an empty tile so the response becomes 204 No Content instead of a blank derived tile that looks like real terrain (crates/…/neighbourhood.rs• L95-131, crates/…/contour.rs• L88-102, crates/…/hillshade.rs• L86-101).

Derived ETags depend on the input neighbourhood and processing settings. If any input lacks an identity, the derived tile is served without an ETag because an output-byte hash would not prove that the inputs remain unchanged (crates/…/contour.rs• L104-139, crates/…/hillshade.rs• L104-144). The gather and compute stages also limit concurrent work because neighbourhood reads and terrain generation can consume shared resources (crates/…/neighbourhood.rs• L16-20, crates/…/contour.rs• L56-60, crates/…/hillshade.rs• L58-62).

Post-cache processing is not treated as a normal response-cache hit because the final tile depends on nearby source tiles and derived settings. The terrain path can still use the shared tile cache while it gathers those inputs (crates/…/content.rs• L549-612).

References: crates/…/mod.rs• L24-63 crates/…/neighbourhood.rs• L16-20 crates/…/neighbourhood.rs• L25-52 crates/…/neighbourhood.rs• L75-84 crates/…/neighbourhood.rs• L95-131 crates/…/neighbourhood.rs• L134-179 crates/…/contour.rs• L56-60 crates/…/contour.rs• L88-139 crates/…/hillshade.rs• L58-62 crates/…/hillshade.rs• L86-144 crates/…/content.rs• L549-612

Tile cache

The tile cache stores complete Tile values in memory with byte-weighted capacity and optional time limits. A cache entry is request-shaped when the result depends on source IDs, coordinates, query parameters, accepted format, or negotiated encoding (crates/…/cache.rs• L6-43, crates/…/cache.rs• L31-63).

The request path uses a race-free read-or-compute operation for eligible source tiles. It skips the final response cache when post-processing can change the result, and it only caches zoom levels that the source allows. Re-encoded responses and composites can receive their own cache entries in the encoding sent to the client (crates/…/content.rs• L508-546, crates/…/content.rs• L549-683).

Cache invalidation follows source identity. A composite key lists its member IDs, so changing or purging one member also removes composites that include it. Invalidation is asynchronous and must be flushed before its effect is observed by maintenance or administrative work (crates/…/cache.rs• L89-117, crates/…/cache.rs• L107-159).

References: crates/…/cache.rs• L6-43 crates/…/cache.rs• L89-117 crates/…/cache.rs• L31-63 crates/…/cache.rs• L107-159 crates/…/content.rs• L508-683

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep source format, encoding, grid, zoom, and query contracts aligned between the Source trait, source resolution, and HTTP negotiation.
  • Keep every cache key dimension that can change response bytes, including composite member IDs and request-selected format or encoding.
  • Keep source update and removal paths coupled to cache invalidation so old tiles cannot survive a source change.
  • Keep composite validation aligned with merge rules. Sources that resolve together must still produce compatible tile representations.
  • Keep derived-tile ETags tied to all source inputs and processing settings that can change the result.
  • Keep neighbourhood coordinate rules aligned with each source's tile grid. Wrapping and non-wrapping grids have different edge behavior.
  • Keep missing-centre handling separate from missing-neighbour handling. No centre data must remain an empty response rather than a flat derived tile.
  • Keep terrain gather and CPU work within their concurrency limits so derived requests do not starve other tile requests.
  • Keep raster adapter metadata aligned with actual output when a source passes through or re-encodes image tiles.
  • Keep source reload behavior coupled to changed local archive or image data, so a changed file does not leave stale bytes in the shared cache.

Important failure modes

TriggerConsequenceGuard
Composite members use different tile gridsOne z/x/y address would have conflicting ground meaningSource resolution rejects mismatched grids
Composite members advertise different formats or encodingsThe response cannot be merged as one valid tileSource resolution and merge checks reject incompatible members
A client requests a format that no source or enabled conversion can provideThe request returns 406 Not AcceptableFormat negotiation checks source format and conversion support before fetching
A source changes without cache invalidationClients can receive stale bytes under the same source IDRegistry updates invalidate the source and related composites
A terrain source has no centre tileA blank derived tile could be mistaken for real data and cachedTerrain processors return an empty tile, which becomes 204 No Content
A terrain neighbour is outside the source grid or cannot be readOne edge can show a seam or clamp to the centre; a failed centre prevents the derived tileGrid-aware neighbour lookup omits out-of-bounds slots, and centre errors remain fatal
A source tile has no reliable input identity for a derived resultConditional requests could claim a false matchDerived processing omits the ETag
Terrain work runs without a concurrency boundCPU or source-read pressure can delay other tile requestsShared gather permits and per-operation CPU permits limit work
A COG reports an image format that the tile server cannot serveThe raster source cannot startSource creation rejects unsupported formats
A local PMTiles file changes while it is mappedA request could read bytes from an old file viewThe backend detects the change and requests a source reload
A STAC source serves below its search zoomThe source could request imagery at a level its index cannot describeSource creation rejects a minimum zoom below the search zoom

Key files

FileWhy to read it
crates/…/content.rsMain request flow, negotiation, cache use, composite assembly, and HTTP response behavior
crates/…/source.rsShared source contract and source-level metadata
crates/…/any.rsCommon dispatch across backend types and declared grids
crates/…/source.rsSource IDs, aliases, zoom filtering, and composite compatibility
crates/…/tile_source_manager.rsLive source publication, reload, removal, and cache invalidation
crates/…/tile.rsTile bytes, format metadata, and ETag representation
crates/…/cache.rsTile cache key shape and source-aware invalidation
crates/…/mod.rsPre-cache vector format conversion
crates/…/neighbourhood.rsGrid-aware 3x3 input gathering, ancestor fallback, and shared concurrency limits
crates/…/contour.rsDerived contour tile flow and input-based identity
crates/…/hillshade.rsDerived hillshade tile flow and input-based identity
crates/…/catalog.rsClient-facing source metadata
crates/…/source.rsShared raster source behavior for COG files and mosaics
crates/…/stac.rsSTAC-backed raster source selection
crates/…/tile_index.rsGeoPackage tile-index-backed raster source selection
crates/…/source.rsPMTiles metadata, tile reads, and source reload behavior
crates/…/backend.rsSafe local-file and object-store byte reads for PMTiles
crates/…/source.rsMBTiles metadata discovery and tile reads