Freegle (www.ilovefreegle.org) is like online dating for stuff - we put people who have stuff they don’t need in touch with people who need stuff they don’t have. As lockdown eases, we’re pretty busy - people have been decluttering, and many are struggling financially. That’s a good problem to have (for us, not so much for them).
Our Google Maps bill was heading for around £750/month. Ouch. That’s not a good problem to have.
Google Maps can be quite expensive once you start to ramp up the usage. I don’t begrudge them making money, but I begrudge them Freegle’s money. Fortunately, there’s an alternative: OpenStreetMap. It’s always been on my todo list to move over - it’s a better fit with Freegle’s ethos anyway - but there never seemed a good reason to get round to it.
Now I had 750 good reasons a month.
First, I did a bit of prototyping using Leaflet. I’m slightly embarrassed to say I’d not used it before, but it didn’t take much getting used to - if you’ve got Google Maps code, you can tweak it a bit and have Leaflet code. Since Freegle uses VueJS, I used Vue2Leaflet.
Second, I needed a tile server - tiles are the square images that form the map. OpenStreetMap has one you can dabble with, but for production volumes you can’t use that, and fair enough. There are lots of paid OpenStreetMap tile services; I don’t begrudge them the money either, and there are some interesting ones who overlay ads onto the map and pay you commission to use them, which I need to explore further. But I wanted to get something into production rapidly, so I went with hosting our own.
So third, I spun up a new virtual machine on Mythic Beasts. There are lots of hosting companies out there, but I can’t praise Mythic Beasts highly enough if you’re a geek. Their prices are reasonable, they know what they’re talking about, and they’re lovely. Setting up a tile server isn’t hard, but OpenStreetMap provide a docker image, which is even easier. I tweaked that slightly, including adding HTTP2 support (thanks to LinuxBabe for the tip), and then slapped an nginx proxy in front of it so that I could serve up SSL.
I learnt a bit along the way which I’m sure other people have blogged about and I missed. A tile server can either render tiles on the fly, or serve up cached ones. I knew that, but I didn’t realise that rendering tiles is unusably slow - it’s ok for one or two map tiles at high zoom levels, but you wouldn’t want to do a whole screen. You can restrict the map zoom levels available depending on the purpose for which you’re using them, which helps a bit, but in the end disk space is cheaper than CPU, so prerendering is absolutely the way to go.
Fourth, then, I ramped up my VM to have 16 cores, and set it prerendering in increasing zoom levels. It’ll probably take a couple of days to finish, and since Mythic Beasts have daily billing I can afford that. In fact, if I left it scaled up it would still be cheaper per month than Google Maps. Once it’s done then I’ll drop it right down, because it’s just a web server serving image. £50/month will be plenty.
I’m not done yet - I need to add autocomplete, and I have a few more bits of function to move over. nginx fronting Apache works but feels clunky.
But still, that’s fine tuning. Time from problem to solution going live? Two elapsed days, one work day. Monthly savings? £700.
That’s a good problem not to have.
P.S. Like the sound of this? We always need more geeky volunteers. Connect on LinkedIn or mail volunteers@ilovefreegle.org