Skip to content

ADR-003: Cost Optimization Strategy

Status: Accepted
Date: 2026-02-01
Deciders: Abdisamed Mohamed
Related ADRs: ADR-002 (Edge-First Architecture)

Context

CepatEdge targets university maintenance teams with potentially thousands of users. Traditional SaaS pricing models charge per user or feature, but universities operate on limited budgets.

The system must remain virtually free to operate while serving significant scale.

Decision

Implement a cost-optimization-first architecture that maximizes free tiers and minimizes operational expenses.

Cost Optimization Strategies

1. Maximize Free Tiers

Cloudflare Workers

  • Free Tier: 100K requests/day
  • Optimization: 90%+ cache hit rate reduces actual requests
  • Cost: $0.0005 per additional 1K requests

Neon PostgreSQL

  • Free Tier: 1GB storage, 100 compute hours
  • Optimization: Aggressive caching reduces DB calls by 80-90%
  • Cost: $0.0001 per compute second after free tier

Cloudflare Pages

  • Free Tier: Unlimited static hosting
  • Optimization: Static generation for optimal caching
  • Cost: Always free

Durable Objects

  • Free Tier: Included with Workers
  • Optimization: Single DO class reduces complexity
  • Cost: Always free

2. Architecture-Level Optimizations

Intelligent Caching Strategy

typescript
// Cache hierarchy: DO → CDN → Browser
const CACHE_TTL = {
  user_session: 3600,     // 1 hour
  maintenance_list: 300,  // 5 minutes
  user_profile: 1800,     // 30 minutes
  system_settings: 86400, // 24 hours
};

Query Optimization

  • Drizzle ORM: Lightweight queries with automatic optimization
  • Indexing: Strategic database indexes for common queries
  • Connection Pooling: Efficient Neon serverless connections

Request Batching

  • API Design: Batch related operations
  • Caching: Cache aggregated data to reduce multiple calls
  • Lazy Loading: Load data only when needed

3. Usage-Based Scaling

Monitoring & Alerts

typescript
const COST_THRESHOLDS = {
  WORKERS_REQUESTS: 80000,  // Alert at 80% of free tier
  NEON_COMPUTE: 80,         // Alert at 80% of free hours
  DO_STORAGE: 100,          // Monitor storage usage
};

Automatic Optimization

  • Cache TTL Adjustment: Increase TTL based on usage patterns
  • Query Optimization: Log and optimize slow queries
  • Resource Cleanup: Automatic cleanup of expired cache entries

Cost Projections

Free Tier Capacity

  • Workers: 100K requests/day = ~3M requests/month
  • Neon: 100 compute hours = ~4,167 hours/month
  • Pages: Unlimited static content
  • DO: Included with Workers

Scaling Costs (After Free Tier)

Service          | Unit Cost     | Monthly Estimate
-----------------|---------------|------------------
Workers          | $0.0005/1K   | $0.50 (200K extra)
Neon Compute     | $0.0001/sec  | $2.50 (9,000 extra hours)
Neon Storage     | $0.0001/GB   | $0.10 (2GB extra)
Email (Resend)   | $0.0004/email| $2.00 (5K emails)
-----------------|---------------|------------------
Total            |               | <$5.10/month

Worst-Case Scenario

Even with 10x usage spikes:

  • Workers: 1M requests/day = $5/month
  • Neon: 1,000 compute hours = $3.60/month
  • Total: <$10/month

Implementation

Cost Monitoring Service

typescript
export class CostMonitor {
  async trackUsage(endpoint: string, duration: number, cached: boolean) {
    // Track API usage for cost analysis
  }

  async getCostEstimate() {
    // Calculate current month cost projection
  }
}

Automated Optimization

typescript
export class CostOptimizer {
  async analyzeUsage() {
    // Analyze usage patterns
    // Adjust cache TTLs
    // Optimize queries
  }

  async cleanupResources() {
    // Remove expired cache entries
    // Optimize database indexes
  }
}

Consequences

Positive

  • University-Friendly: Virtually free for educational institutions
  • Scalable Pricing: Costs grow linearly with usage
  • No Hidden Fees: Transparent pricing with free tiers maximized
  • Sustainable: Long-term cost predictability

Negative

  • Usage Limits: Free tier constraints require optimization
  • Complexity: Additional code for cost optimization
  • Monitoring Overhead: Need to track usage continuously

Mitigation

  • Built-in Optimization: Cost optimization is core architecture
  • Transparent Monitoring: Usage visible to administrators
  • Graceful Degradation: System works even if optimizations fail

Alternatives Considered

Per-User Pricing

  • Pros: Predictable revenue, standard SaaS model
  • Cons: Too expensive for universities, limits adoption

Freemium Model

  • Pros: Free basic features, paid for advanced
  • Cons: Complex feature tiers, administrative overhead

Donation/Sponsorship Model

  • Pros: Completely free for users
  • Cons: Unreliable funding, development uncertainty

References

  • Cloudflare Workers Pricing
  • Neon Serverless Pricing
  • Resend Email Pricing
  • University Budget Constraints Analysis