Mapping Factorio with Leaflet

Overview

This article discusses the implementation of interactive Leaflet maps for the game Factorio, enabling players to share their factory designs. It covers the challenges of hosting large amounts of map images, optimizing performance, and utilizing Cloudflare Workers for efficient path management.

What You'll Learn

1

How to implement interactive maps using Leaflet for large-scale applications

2

Why using multi-tier object storage can optimize performance and reduce costs

3

How to leverage Cloudflare Workers for efficient path management in web applications

Key Questions Answered

What storage solution is best for hosting large amounts of map images?
Backblaze B2 is highlighted as a cost-effective solution for hosting map images due to its free upload API calls and low egress costs, making it suitable for the 17 million images used by FactorioMaps.com.
How does Leaflet manage image loading for different zoom levels?
Leaflet maps load images based on the user's X/Y/Z coordinates, requiring 4x as many images as the map is zoomed in. This means that 75% of a map's images are utilized at the maximum zoom level.
What strategies were used to reduce latency in image requests?
To reduce latency, a local SSD cache was implemented on the VPS for the first few zoom levels, which accounted for 66% of requests, significantly improving response times compared to accessing all images directly from storage.
How can Cloudflare Workers enhance the performance of web applications?
Cloudflare Workers allow for JavaScript execution at the edge, enabling efficient path rewrites and redirects, which can improve the performance of web applications by reducing latency and optimizing resource access.

Key Statistics & Figures

Total map images hosted
17 million
This number reflects the scale of the FactorioMaps.com project, which requires efficient storage and management solutions.
Average latency from VPS to B2
800-1200ms
This latency was noted as a challenge when accessing images through a VPS proxy.
Percentage of requests served from GCS
66%
This percentage indicates the effectiveness of the multi-tier storage strategy implemented.
Mean latency from GCS
<350ms
This latency reflects the performance improvement achieved by using Google Cloud Storage for frequently accessed images.

Technologies & Tools

Frontend
Leaflet
Used for creating interactive maps that allow users to explore Factorio factories.
Backend
Cloudflare Workers
Enables serverless execution of JavaScript for efficient resource management and path rewriting.
Storage
Backblaze B2
Used for cost-effective storage of map images.
Storage
Google Cloud Storage
Provides low-latency access to frequently requested images.

Key Actionable Insights

1
Implementing multi-tier object storage can drastically improve performance and reduce costs.
By storing frequently accessed data in a faster, more expensive storage solution like Google Cloud Storage, while keeping less frequently accessed data in a cheaper option like Backblaze B2, you can optimize both speed and cost.
2
Utilizing Cloudflare Workers can streamline your web application's resource management.
By offloading path management to Cloudflare Workers, you can reduce server load and improve response times, especially for geographically distributed users.
3
Consider using a local SSD cache for high-frequency data requests.
This can significantly reduce latency for the majority of requests, especially when dealing with large datasets like map images in a gaming application.

Common Pitfalls

1
Relying solely on a single storage solution can lead to performance bottlenecks.
Using only one storage provider may result in high latency for users, especially if the data is not cached or optimized for access.
2
Neglecting to optimize image loading can degrade user experience.
If images are not loaded efficiently based on user interactions, it can lead to slow map rendering and frustrated users.

Related Concepts

Caching Strategies
Image Optimization Techniques
Serverless Architecture