Optimizing Cloud-optimized GeoTIFF tile requests to not explode your Cloudflare bill.
A Cloud Optimized GeoTIFF (COG) is a big image file built so you can read small pieces of it without downloading the rest. Inside, the picture is chopped into blocks, each one compressed on its own, and the file starts with an index saying where every block sits.
COGs are great right up until you look at your storage bill. I keep them in Cloudflare R2, and R2 charges per read request (it calls reads Class B operations), not just per gigabyte. Every time a browser asks for a single map tile, the tiler - the program that draws that tile - might need several blocks out of the COG to stitch it together. Fetch every block as its own HTTP request and those operations rack up fast.
Take a real example from a USGS aerial survey over Clearview Landfill: four compressed blocks add up to 668,373 bytes (~668 KB) of imagery. The naive approach makes four separate requests to storage, each one a range request: “send me bytes X to Y of this file”. By letting the tiler read straight across tiny gaps between blocks in the same row, it grabs all four in two requests - at the cost of downloading just 94 extra bytes. Meanwhile, it still skips the 1 MB gap between rows.

Four neighbouring image blocks in a 512 × 512 window of the USGS Clearview survey, fetched with range merging applied to their block offsets. Read the byte offsets and ranges · USGS survey.
Merging byte ranges without getting greedy
When I moved raster rendering into a Cloudflare Worker, the flow was straightforward: read the GeoTIFF header and index (cached after the first hit), pick the right overview for the zoom level (an overview is a smaller, pre-shrunk copy of the image stored in the same file for zoomed-out views), and fetch the compressed blocks covering that window.
The trick is deciding when two byte ranges are close enough to merge into one request. I landed on a simple rule: combine adjacent ranges if the gap between them is 64 KiB or less (65,536 bytes) and the merged chunk stays under 8 MiB. Think of picking up groceries: if two things are on the same shelf, you grab both in one reach instead of walking back to the cart in between. Reading 47 throwaway bytes between two neighbouring blocks is basically free compared to paying for another round trip and another R2 operation. Reading across a 1 MB jump to the next row would just waste bandwidth.
Drawn from the recorded block offsets. Two range requests cover all four blocks at the cost of 94 extra bytes.