0

How may we implement caching for a database Driven Map-Tile Server?

The ideas i have are:

1) cache responses (bytes) for unique z-x-y requests in memory (this will not scale well, if we have a large map area)

2a) render requested tiles to file system as /site/tiles/z/x/y.png and server from there subsequently,

2b) apply suitable caching headers to ensure HTTP 304 for re-requests

I had implemented this basic map-tile server with Leaflet.js and a simple .aspx page - see here.

Each request for a map-tile hits the db, pulls out a binary-tile-image and returns a .png file like so:

MapTest.html

<div id='map'></div>

<script>
var map = L.map('map').setView([8.2, 6.95], 7);

L.tileLayer('**http://localhost/tileserver/tile.aspx?z={z}&x={x}&y={y}**', {
    minZoom: 7, maxZoom: 16,
    attribution: 'My Tile Server'
}).addTo(map);
</script>

Tiles.aspx

Option Strict On

Partial Class tile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim z, x, y As Integer

        z = CInt(Request.QueryString("z"))
        x = CInt(Request.QueryString("x"))
        y = CInt(Request.QueryString("y"))

        Dim b() As Byte = DB.GetTile(z, x, y)

        Response.Buffer = True
        Response.Charset = ""
        'Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = "image/png"
        Response.AddHeader("content-disposition", "attachment;filename=" & y & ".png")
        Response.BinaryWrite(b)
        Response.Flush()
        Response.End()
    End Sub

Please how can we improve this with an effective caching?

Community
  • 1
  • 1
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
  • There are too many solutions. Off-the-shelf proxy-like caching mechanisms like squid and mapproxy, ram-based caching, proximity-based metatile prefetching... It will all depend on where your bottleneck is. – IvanSanchez Jun 24 '16 at 12:20
  • @IvanSanchez yes there are several caching solutions, but which is most appropriate for a Map-Tile Server, which renders tiles from a DB? – Charles Okwuagwu Jun 24 '16 at 12:22
  • It depends. Where is your bottleneck? – IvanSanchez Jun 24 '16 at 12:25
  • @IvanSanchez at present, i have not added any caching. I see requests for the same tile being repeated over and over again...i feel this can be improved with some (caching?) mechanism – Charles Okwuagwu Jun 24 '16 at 12:43
  • Do not *"feel"*. Monitor your servers, measure where's your bottleneck, improve that. Otherwise you'll be a victim of premature optimization. – IvanSanchez Jun 24 '16 at 13:10
  • 1
    If the problem is "the same client is requesting the same tile over and over again", then cache in the client. – IvanSanchez Jun 24 '16 at 13:10
  • 1
    If the problem is "different clients are requesting the same tile over and over again", put a caching proxy. – IvanSanchez Jun 24 '16 at 13:11
  • 1
    If the problem is "the same tile is requested from the database over and over again", then cache database results. – IvanSanchez Jun 24 '16 at 13:13
  • 1
    Do not *"feel"*, do not solve problems you don't have, and do not optimize prematurely. Measure, find bottlenecks, fix those bottlenecks. – IvanSanchez Jun 24 '16 at 13:15
  • @do not "feel" -- true, but even looking at your existing design you can preempt possible bottlenecks by doing things intelligently – Charles Okwuagwu Jun 24 '16 at 13:17
  • the 3 caching options you mentioned are all viable and may be combined to give a "balanced solution" - this is not premature – Charles Okwuagwu Jun 24 '16 at 13:19

0 Answers0