TahminApp is my own side project: a small sports prediction app, still in development. The figures below are not measured product results — they are the project's architecture decisions and design targets.
Problem: On a sports prediction platform, instantly push real-time match events (goals, cards, halftime) to 500K+ users while updating live scores in the UI.
Design targets: 500K MAU, p99 latency 180ms, uptime above 99.9%, no incident longer than 30 minutes. None of these is a measured result — the architecture is built to aim at them.
Pro Tip: The bottleneck is not where you expect it. In this design it isn't Firestore writes — it's push delivery routing. Every big scaling story starts the same way.
1. Architecture Overview
3 tiers:
- Edge layer (CF Workers): Webhook receiver, dedup, fan-out trigger
- Backend (Firestore + Cloud Functions): Persistent state, business logic
- Mobile (iOS native + Flutter Android): Realtime listener + push notification handler
1[Sports Data Feed]2 ↓ webhook3[CF Worker — dedup + transform]4 ↓ event5[Cloud Function — match state update]6 ↓ write7[Firestore — predictions, users, scores]8 ↓ realtime listener9[Mobile Apps — UI update]10 ↓11[FCM/APNs — push notification]2. The Bottleneck Story
First hypothesis: Firestore writes don't scale. At this volume they don't — sharding solves it.
Second hypothesis: Cloud Functions cold starts are the problem. A warm instance pool rules that out too.
The real bottleneck: APNs delivery latency. On a 500K-push fan-out, default routing delivers in the 4–6 minute band rather than 30 seconds. That is the typical source of "late notification" complaints during big-match moments.
Fix: Priority routing — match-event pushes use apns-priority: 10 plus parallel batch sends from Cloudflare Workers. Target: 500K pushes delivered in the 25–35 second band.
3. Firestore Design
500K users × 50 predictions/user × 100 matches/season = 2.5M documents. A naive /predictions/{id} design collapses at that volume.
The right pattern — sharded collection:
1/predictions/shard-{0-19}/items/{predictionID}Shard count is 20, assigned from a hash of the prediction ID. Each shard has its own index and its own write limit. Total write throughput: 20x.
Realtime listener: Mobile listens to /predictions/shard-{userShard} — only its own shard. Network savings: 95%.
4. Cloud Functions Patterns
The design calls for 40+ Cloud Functions in total. The 3 most critical:
onMatchEvent: webhook trigger, 30-second dedup window, state update + push triggeronPredictionWrite: leaderboard recalculation (sharded counter pattern)onUserWrite: profile cache invalidation (Cloudflare KV purge)
Every function:
- Memory: 256MB (most), 1GB (leaderboard)
- Timeout: 60s
- Max instances: 1000 (auto-scale)
- Retry: 3x exponential backoff
- Idempotency: Firestore set-based dedup on
eventID
5. iOS App Architecture
iOS native (SwiftUI, iOS 16+):
- TCA (The Composable Architecture) for state management
- Firestore Snapshot Publisher — Combine pattern
- APNS Notification Service Extension — payload decryption + smart actions
- Local cache (SwiftData) — offline read-only mode
Memory baseline target: 95MB. Peak (live match-time UI): 145MB.
Target band for crash-free session rate: 99.8%+, tracked in Crashlytics.
6. Cost Profile
The design's estimated monthly infrastructure budget (assuming the 500K MAU peak target):
Item | Cost |
|---|---|
Firestore (read/write/storage) | $420 |
Cloud Functions (invocations) | $180 |
Cloud Messaging (FCM) | $0 (free tier is enough) |
APNs | $0 (Apple side) |
Cloudflare Workers | $25 (Pro plan) |
Cloudflare KV | $5 |
Hetzner monitoring server | $7 |
Total | $637/month |
Per-user cost: $0.00127/user/month. More than workable unit economics for an indie project.
7. Lessons Learned
1. The predicted bottleneck is rarely the real bottleneck. Items like APNs delivery only surface under real traffic, so the measurement plan has to be ready before launch.
2. Pay for sharding up front. Adding sharding after you've already hit 500K users is technical debt measured in months.
3. Idempotency via event ID is critical — otherwise webhook retries silently create duplicates.
4. The Notification Service Extension is gold for content modification and alert prioritization.
5. Doing dedup + transform at the edge layer (CF Workers) cuts Cloud Functions cost several times over.
6. Run Crashlytics and custom telemetry together. Crashlytics gives you aggregation; custom telemetry gives you decision-making data.
7. In this product class, average notification latency during big match moments is what App Store reviews keep coming back to. Optimizing latency is the line item that moves the rating directly.
Bottom line: Scaling to 500K users is usually not a "Firestore problem" — it's a "delivery infrastructure" problem. Design the architecture as multi-tier from day one, then measure and refactor.
Tags
iOS Development News
Weekly Swift tips, SwiftUI tricks and iOS best practices. No spam, only valuable content.
Your subscription starts when you open the link in the confirmation email and press “Confirm my subscription”. The newsletter keeps open/click statistics; you can unsubscribe anytime with one click. Privacy
