Looking at the surface, Espera Médica might look like a simple aggregated data table. Under the hood, scraping all data presents several challenges: dozens of official portals with no stable API contracts, and the dashboard must not wipe clean whenever a data source changes or goes down. Each hospital publishes wait-time data differently. There's a bit of everything: APIs, JSON payloads, site counters/clocks, and color-coded status boards. The term “wait time” isn't even defined the same way everywhere—some group a single metric/indicator across multiple specialties, while others don't display a queue at all. Without normalizing the data, making meaningful comparisons is nearly impossible.
Infrastructure challenges were also a major hurdle. In some cases, CORS policies prevented me from fetching data directly from the browser portal. Several hospitals sit behind CDNs or WAFs (Akamai, Cloudflare, Imperva, etc.), while others restrict access exclusively to Brazilian IPs. In other instances, a basic fetch returns a 403, a security challenge, or an empty page. I had to handle these cases differently from straightforward ones, designing a fallback chain with varying approaches—scaling up to the most expensive options, such as residential proxies to bypass datacenter IP blocks. Within this chain, one key rule saved both costs and headaches: if a step fails, I don't retry it in the same run. I simply attempt the next fallback option once and move along until it succeeds or all strategies are exhausted. If a scraper fails to measure an actual value in time, I keep the last collected value—even if it's stale. Fortunately, this strategy paid off.
Just when I thought everything was sorted out, operating multiple scraping routines simultaneously became complex. I had to manage CPU and memory footprints alongside strict timeout limits. This isn't a single cron job that "scrapes everything." It's a set of scheduled micro-jobs, each handling a slice of the overall catalog. If I run everything in parallel, the platform consumes all available CPU and memory; if I chain too much into a single isolate, it times out and the batch fails halfway through. In practice, this turned into a resource budgeting puzzle: how much parallelism can run without crashing the worker, and how much serial execution can it tolerate without breaching wall-clock time limits?
All of this was driven by my goal to fit the entire stack into all free-tier solutions. Scraping is heavy on CPU and memory—downloading, parsing (heavy regex work), mapping, and upserting. When I lumped too much logic into the same isolate, the worker crashed. I had to break it down into multiple scheduled jobs and move heavy HTML processing away from Edge Functions over to Vercel Functions.
To keep costs under control, I also had to balance update frequency, infrastructure expenditure, and data retention. Storing a snapshot of every single scrap run for analytical metrics—such as identifying the best time slot for each specialty—wasn't feasible for the database. Instead, I opted for a strategy that stores rolling averages aggregated by unit, specialty, day of the week, and hour. This allows the system to tell users "2 PM is usually the best time" while keeping database costs under control.