KV
KV session or cache
Session or cache layer with get/set/delete and TTL.
The Pattern
KV
Why this pattern?
- No database: Simple key-value over HTTP, no schema or migrations
- TTL: Optional expiration so sessions or cache entries auto-expire
- JSON values: Store objects (session data, cached API responses)
- Cache-aside: Check KV first; on miss, compute and store for next time
Example implementation
// 1. Set a session or cache entry (key in path; optional ttlSeconds)
await fetch('https://api.rocketstack.dev/kv/items/session-usr_abc123', {
method: 'PUT',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
value: {
userId: 'usr_abc123',
cart: [{ sku: 'item_1', qty: 2 }],
lastActive: Date.now()
},
ttlSeconds: 86400 // 24 hours
})
});
// 2. Get the value
const res = await fetch('https://api.rocketstack.dev/kv/items/session-usr_abc123', {
headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
});
const data = await res.json();
const session = data.data?.value; // { userId, cart, lastActive }
// 3. Delete when done (e.g. logout)
await fetch('https://api.rocketstack.dev/kv/items/session-usr_abc123', {
method: 'DELETE',
headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
});
// Cache-aside: get, on miss compute and set
async function getCached(key) {
const r = await fetch(`https://api.rocketstack.dev/kv/items/cache-${key}`, {
headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
});
if (r.ok) {
const json = await r.json();
const value = json.data?.value;
if (value) return value;
}
const computed = await expensiveCompute(key);
await fetch(`https://api.rocketstack.dev/kv/items/cache-${key}`, {
method: 'PUT',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({ value: computed, ttlSeconds: 3600 })
});
return computed;
} Free tier available
Ready to start building?
Create your free account and integrate infrastructure primitives in minutes.