Skip to content

Best Practices

Use hierarchical keys with colons:

user:123:profile
user:123:settings
session:abc:data
config:features
stats:daily:2024-01-15

Separate environments:

myapp-dev
myapp-staging
myapp-prod

Always handle errors:

try {
const { value } = await kv.get('myapp', 'key');
} catch (error) {
if (error.message.includes('404')) {
// Key not found
} else {
// Other error
}
}

Monitor headers and implement backoff:

const response = await fetch(url, { headers });
const remaining = response.headers.get('X-RateLimit-Remaining');
if (remaining < 10) {
// Slow down requests
}
  • Use environment variables for API keys
  • Never commit keys to git
  • Rotate keys regularly
  • Use different keys per environment
  • Cache frequently accessed data
  • Use prefix queries for bulk operations
  • Batch operations when possible
  • Monitor usage in dashboard