Firebase Integration Patterns
Tech-stack skill for Firebase services in mobile and web applications. Covers Firestore, Cloud Functions, security rules, authentication, storage, and multi-environment management.
Supporting Files
| File | Purpose |
|---|
| firestore-patterns.md | Query optimization, pagination, batch writes, listeners, offline persistence |
| security-rules.md | Least privilege, document validation, RBAC with custom claims, testing |
| cloud-functions.md | Cold starts, memory sizing, connection pooling, triggers, timeouts |
| environment-management.md | Multi-environment setup, region consistency, Terraform IaC, index management |
Services Overview
| Service | Key Considerations |
|---|
| Firestore | Pagination, batch writes (500 max), compound indexes, listener cleanup |
| Authentication | Token refresh via auth state stream, App Check enforcement, multi-provider |
| Cloud Functions | Cold starts, memory sizing, region consistency, conditional triggers |
| Storage | Image compression before upload, CDN caching, thumbnails via resize function |
| Analytics | Event tracking, user properties, conversion funnels |
| Crashlytics | Error reporting, custom keys, non-fatal exceptions |
| Performance | Custom traces, screen load tracking, network monitoring |
| Messaging | FCM tokens, topic subscriptions, background handlers |
| App Check | Play Integrity (Android), App Attest (iOS), debug tokens for dev |
Key Principles
- Region consistency -- all services in the same region for lowest latency
- Offline-first -- leverage Firestore offline persistence and cache-first reads
- Pagination everywhere -- never load unbounded collections
- Listener hygiene -- always cancel StreamSubscriptions in dispose()
- Least privilege -- security rules deny by default, grant specific access
- Index management -- create composite indexes in all environments
- Cost awareness -- every listener, query, and function invocation has a cost
Cost Model (Key Metrics)
| Service | Free Tier | Cost After Free |
|---|
| Firestore Reads | 50K/day | $0.06/100K |
| Firestore Writes | 20K/day | $0.18/100K |
| Storage | 5 GB | $0.026/GB/month |
| Cloud Functions | 2M invocations/month | $0.40/M |
| Phone Auth | 10K/month | $0.06/verification |
Authentication Patterns
- Single auth state listener -- not per-screen, centralized in a service
- Token refresh handled reactively via
authStateChanges() stream
- Anonymous upgrade -- allow anonymous users to link permanent credentials
- Sign-out cleanup -- clear all local state, cancel listeners, reset providers
- No auth tokens in logs -- sanitize error messages
Storage Patterns
- Compress images before upload (quality reduction, resize)
- Generate thumbnails via Cloud Function on upload
- CDN caching -- Firebase Storage uses Google CDN by default
- Progressive loading -- show thumbnail first, load full resolution on demand
- User-scoped paths -- store files under
users/{uid}/ for security rules
Performance Monitoring
- Custom traces for business-critical operations
- Screen load tracking via mixin on screen widgets
- Route observer for navigation performance
- Network monitoring for API call latency