Review request

Hiking trail guide on Firebase, 900k reads overnight and nobody was awake

Open · 7 views · asked by yuki_builds · edited

Repo or live app

trails-nagano.web.app

Unsure about: Costs

About 400 trails with photos and conditions. v0 built the list and detail pages against Firestore. I checked the console this morning and yesterday shows 900k document reads with maybe 200 visitors. The bill is still small but the shape of that number worries me more than the amount.

Comment

2 answers

sven_fire · edited

900k reads from 200 visitors is roughly 4,500 reads each, which means you are reading the whole collection repeatedly rather than once.

Two things cause this together in v0 output. First, onSnapshot on the entire trails collection instead of getDocs, so every write by anyone re-delivers every document to every open tab. Second, the subscription sits in a component body or an effect without a dependency array, so each render tears down and re-establishes it, and a fresh listener bills a full read of the result set.

For a list that changes a few times a day, use getDocs with a limit(20) and paginate with startAfter. Keep onSnapshot for the one document a user is actually editing.

Comment
lena_ops · edited

Also check your rules while you are in there. If trails is readable with allow read: if true, anyone can script the collection and your read count becomes someone else's decision. Rate limiting is not a thing in Firestore rules, so the practical defence is serving the list from a single cached document or a CDN-cached endpoint rather than letting clients query the collection directly.

Comment