Skip to content

Optimize KV usage to stay within free tier limits #2

Closed

Description

@neuromechanist

Problem

Cloudflare KV usage reached 50% of daily write limit, triggering a warning email. Investigation shows nemar-api rate limiter is the primary consumer.

Findings

KV Usage (Jan 15, 2026)

Namespace Worker Today's Operations Daily Limit % Used
RATE_LIMIT nemar-api 619 reads + 592 writes 1,000 writes 59%
RATE_LIMITER hed-bot/hedit ~22 1,000 <1%
RATE_LIMITER osa-worker ~12 1,000 <1%
HED_CACHE hed-bot/hedit ~6 1,000 <1%

Root Cause

Each API request to nemar-api triggers:

  1. KV read (check rate limit)
  2. KV write (increment counter)

With 20+ CI test runs today, this quickly consumed the daily write quota.

Cloudflare Free Tier Limits

  • 100,000 reads/day
  • 1,000 writes/day
  • 1 GB storage

Mitigations Already Applied

  • CI path-based filtering - Tests only run when relevant files change
  • CI concurrency control - Cancels duplicate runs
  • TEST_BYPASS_TOKEN configured - Tests skip rate limiting

Recommendations

Short-term (Pick one)

  1. Disable rate limiting in development

    // In rateLimit.ts
    if (c.env.ENVIRONMENT !== 'production') {
      await next();
      return;
    }
  2. Use Cache API instead of KV for rate limiting (no daily limits)

    const cache = caches.default;
    // Cache API has no operation limits
  3. Increase rate limit window to reduce writes

    const WINDOW_SIZE = 300; // 5 minutes instead of 60 seconds

Long-term

  1. Upgrade to Workers Paid ($5/month) - 10M reads, 1M writes/month
  2. Use Durable Objects for rate limiting - Better suited for counters
  3. Implement token bucket with longer persistence

GraphQL Query for Monitoring

query KvOperations {
  viewer {
    accounts(filter: { accountTag: "ACCOUNT_ID" }) {
      kvOperationsAdaptiveGroups(
        filter: { 
          namespaceId: "9afb2679c6ea4ed4acd1a5916cf291d7",
          date_geq: "2026-01-01"
        }
        limit: 100
        orderBy: [date_DESC]
      ) {
        sum { requests }
        dimensions { date actionType }
      }
    }
  }
}

References

Activity

  1. neuromechanist commented on Jan 15, 2026

    @neuromechanist
    ContributorAuthor

    Resolution

    Implemented the following changes to resolve KV usage concerns:

    1. Switched to Cache API for Rate Limiting

    • Production now uses Cloudflare Cache API instead of KV
    • Cache API has no daily operation limits
    • Same functionality, zero KV operations

    2. Created Development Environment

    3. Updated CI Workflow

    • All tests now run against dev API (no rate limiting)
    • Added dev branch to workflow triggers
    • Path-based filtering still in place

    4. Branch Strategy

    • dev branch for development and testing
    • main branch for production
    • Both protected with same workflow

    Commits

    • be8eeed: feat: switch to Cache API rate limiting and add dev environment

    The KV namespace 9afb2679c6ea4ed4acd1a5916cf291d7 is no longer used and can be deleted to clean up.

  2. neuromechanist commented on Jan 16, 2026

    @neuromechanist
    ContributorAuthor

    Resolution Status

    The KV usage issue has been addressed:

    1. Rate limiter migrated to Cache API (commit be8eeed) - Cache API has no daily operation limits
    2. KV namespace deleted - The RATE_LIMIT namespace (9afb2679c6ea4ed4acd1a5916cf291d7) is no longer in use
    3. Development environment bypasses rate limiting - CI tests use dev environment with rate limiting disabled
    4. Test bypass token configured for any production API testing

    The 50% warning received today was from usage accumulated before the fix was fully deployed. Daily KV limits reset at midnight UTC, so tomorrow's metrics should confirm near-zero KV usage from nemar-api.

    Will monitor and close this issue once confirmed.

  3. added 2 commits that reference this issue on May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions