Best Practices
Key Naming
Section titled “Key Naming”Use hierarchical keys with colons:
user:123:profileuser:123:settingssession:abc:dataconfig:featuresstats:daily:2024-01-15Namespace Organization
Section titled “Namespace Organization”Separate environments:
myapp-devmyapp-stagingmyapp-prodError Handling
Section titled “Error Handling”Always handle errors:
try { const { value } = await kv.get('myapp', 'key');} catch (error) { if (error.message.includes('404')) { // Key not found } else { // Other error }}Rate Limiting
Section titled “Rate Limiting”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}Security
Section titled “Security”- Use environment variables for API keys
- Never commit keys to git
- Rotate keys regularly
- Use different keys per environment
Performance
Section titled “Performance”- Cache frequently accessed data
- Use prefix queries for bulk operations
- Batch operations when possible
- Monitor usage in dashboard