# CloudSaver
<div align="center">





**Prevent cloud bill explosions before they happen**
[English](#the-problem) | [中文](README_CN.md)
</div>
---
## The Problem
> "We were on the free tier. Then one morning we woke up to a $12,000 bill."
This happens every day:
- A missing index turns a simple query into **1 billion Row Reads** on Cloudflare D1
- An N+1 query pattern burns through Supabase compute credits overnight
- A cron job with `SELECT *` on an unindexed table racks up **$8,000 in a weekend**
- A Redis instance without TTLs silently fills up memory and triggers overage charges
**Free tiers are generous. But one bad query can cost more than a year of paid hosting.**
CloudSaver catches these problems before they hit your bill.
## Why Web Development Needs This
In typical web development, developers focus on features and functionality — not on the cost implications of their code. A query that works perfectly in development with 100 rows can become a **$5,000 surprise** in production with 1 million rows.
Common scenarios that slip through code reviews:
- **ORM-generated queries** that look innocent but create N+1 patterns
- **"Just add an index later"** — but later never comes, and the bill arrives first
- **Prototype code** that goes to production with `SELECT *` everywhere
- **Cron jobs** that worked fine at small scale, then explode overnight
- **Third-party integrations** that silently increase API call costs
CloudSaver acts as a **cost risk control layer** for your development workflow:
| Stage | Risk | CloudSaver Action |
|-------|------|-------------------|
| Development | Missing index on new table | Detect before deploy |
| Code Review | N+1 query in API endpoint | Flag with fix suggestion |
| Staging | Slow query under load | Benchmark and optimize |
| Production | Sudden cost spike | Alert and auto-fix |
**It's not just about saving money — it's about preventing financial incidents before they happen.**
## What It Does
CloudSaver is a set of optimization skills that automatically detect and fix expensive cloud service patterns:
| What It Catches | Example | Potential Savings |
|----------------|---------|-------------------|
| Missing indexes | Full table scan on 1M rows | 99.9% Row Reads |
| N+1 queries | 100 queries in a loop | 90%+ reduction |
| Deep pagination | `OFFSET 10000` | 99.8% Row Reads |
| SELECT * | Fetching 20 columns, using 3 | 90% bandwidth |
| Uncached queries | Same query 10K times/day | 99.9% reduction |
| Memory leaks | Redis without TTLs | 70% memory |
## Quick Start
```bash
git clone https://github.com/huifer/CloudSaver.git
cp -r CloudSaver/skills/* your-project/.claude/skills/
```
## Supported Services
| Service | Focus Areas |
|---------|-------------|
| **Cloudflare D1** | Row Reads, indexes, query rewriting, denormalization |
| **Cloudflare Workers/KV/R2** | Invocations, cache strategy, storage optimization |
| **Supabase** | Database performance, storage, Auth MAU, Edge Functions |
| **Redis** | Memory usage, connections, hot keys, command optimization |
| **PostgreSQL/SQLite** | Index optimization, query rewriting |
| **Vercel** | Function invocations, bandwidth, build minutes |
| **Netlify** | Function invocations, bandwidth, form submissions |
## Real-World Impact
### Before CloudSaver
```sql
-- This query costs 1,000,000,000 Row Reads
SELECT c.*, COUNT(o.id) as offer_count
FROM campaigns c
LEFT JOIN offers o ON o.campaign_id = c.id
GROUP BY c.id
ORDER BY c.created_at DESC;
```
### After CloudSaver
```sql
-- This query costs 20 Row Reads
SELECT c.*,
(SELECT COUNT(*) FROM offers o WHERE o.campaign_id = c.id) as offer_count
FROM campaigns c
ORDER BY c.created_at DESC
LIMIT 20;
```
**That's a 99.999998% reduction.**
## Anti-Patterns Detected
| Anti-Pattern | Service | Impact |
|--------------|---------|--------|
| Missing indexes | D1, Supabase | 99%+ Row Reads reduction |
| Deep OFFSET | D1, Supabase | 99.8% improvement |
| SELECT * | All SQL | 90% data transfer reduction |
| N+1 queries | All SQL | 90%+ query reduction |
| LIKE '%xxx%' | All SQL | 99.5% improvement |
| Full table scan | All SQL | 99%+ improvement |
| OR across columns | All SQL | 99.998% improvement |
| Type mismatch | All SQL | 99.999% improvement |
| Uncached results | All | 99.9% improvement |
| Missing TTLs | Redis, KV | 70%+ memory savings |
## Skills
| Skill | Service | References |
|-------|---------|------------|
| `d1-cost-optimizer` | Cloudflare D1 | 16 |
| `supabase-audit` | Supabase | 8 |
| `cloudflare-audit` | Cloudflare | 3 |
| `redis-audit` | Redis | 3 |
| `index-optimizer` | PostgreSQL/SQLite | 6 |
| `query-rewriter` | SQL | 6 |
| `cost-monitor` | All | 2 |
| `spend-cap-manager` | All | 2 |
| `cost-audit-master` | All | 1 |
## CLI Tools
```bash
# Check Supabase health
./tools/clis/supabase.sh db-size
./tools/clis/supabase.sh slow-queries
# Check Cloudflare usage
./tools/clis/cloudflare.sh workers
./tools/clis/cloudflare.sh d1
# Check Redis status
./tools/clis/redis.sh memory
./tools/clis/redis.sh large-keys
```
## Coming Soon
- AWS (S3, Lambda, RDS, DynamoDB)
- Google Cloud (Functions, Storage, Firestore, BigQuery)
- Azure (Functions, Blob Storage, Cosmos DB)
- PlanetScale, Neon, Turso, MongoDB Atlas
- Multi-channel alerting (Slack, Discord, PagerDuty)
- Cost forecasting and anomaly detection
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
## License
Apache License 2.0 - See [LICENSE](LICENSE)
---
<div align="center">
**Stop worrying about surprise cloud bills**
[Report a Bug](https://github.com/huifer/CloudSaver/issues) · [Request a Feature](https://github.com/huifer/CloudSaver/issues)
</div>