com um clique
caching-strategies
Caching patterns. HTTP caching, Redis, CDN, browser cache, cache invalidation strategies.
Menu
Caching patterns. HTTP caching, Redis, CDN, browser cache, cache invalidation strategies.
Web accessibility (a11y). WCAG 2.1, ARIA, keyboard navigation, screen readers, testing tools.
Analytics and event tracking. Product analytics, Mixpanel, PostHog, Segment, GDPR compliance, event taxonomy.
UI animation patterns. CSS transitions, Framer Motion, GSAP, performance, accessibility.
Authentication and authorization patterns. JWT, sessions, OAuth 2.0, RBAC, ABAC, refresh tokens, security.
Background job patterns. Cron jobs, queued jobs, scheduling, BullMQ, pg-boss, Sidekiq patterns.
CI/CD pipeline patterns. GitHub Actions, GitLab CI, Docker builds, testing, deployment stages, secrets management.
| name | caching-strategies |
| description | Caching patterns. HTTP caching, Redis, CDN, browser cache, cache invalidation strategies. |
A cache hit is the fastest request. The fastest database query is no query.
Browser Cache (ms)
↓
CDN / Edge Cache (ms)
↓
Load Balancer Cache (ms)
↓
Application Cache / Redis (ms)
↓
Database Query Cache (ms)
↓
Database (ms–s)
| Header | Purpose | Example |
|---|---|---|
Cache-Control | Directives for browser/CDN | max-age=3600, s-maxage=86400 |
ETag | Conditional request validation | "abc123" |
Last-Modified | Time-based validation | Wed, 21 Oct 2015 07:28:00 GMT |
Vary | Cache key variations | Vary: Accept-Encoding, Cookie |
Cache-Control directives:
├── public → CDN can cache
├── private → Browser only (logged-in content)
├── no-cache → Revalidate before use
├── no-store → Never cache (sensitive data)
├── max-age=N → Browser TTL in seconds
└── s-maxage=N → CDN TTL (overrides max-age for CDN)
| Strategy | Description | Use When |
|---|---|---|
| TTL (Time-to-Live) | Expire after N seconds | Data changes predictably |
| Write-through | Update cache on write | Low write volume, strong consistency |
| Write-behind | Async cache update | High write throughput |
| Cache-aside | App manages cache manually | General purpose |
| Event-driven | Invalidate on domain events | Microservices |
Cache-aside pattern:
1. Read: check cache → hit → return
2. Read: miss → query DB → store in cache → return
3. Write: update DB → DELETE cache key (not update)
# Cache-aside (Python)
import json
from redis import Redis
r = Redis()
TTL = 3600 # 1 hour
def get_user(user_id: str):
key = f"user:{user_id}"
cached = r.get(key)
if cached:
return json.loads(cached)
user = db.query_user(user_id) # DB call
r.setex(key, TTL, json.dumps(user))
return user
def update_user(user_id: str, data: dict):
db.update_user(user_id, data)
r.delete(f"user:{user_id}") # Invalidate
// TypeScript / Node.js
const CACHE_KEY = (id: string) => `user:${id}`;
async function getUser(id: string) {
const cached = await redis.get(CACHE_KEY(id));
if (cached) return JSON.parse(cached);
const user = await db.user.findUnique({ where: { id } });
await redis.setex(CACHE_KEY(id), 3600, JSON.stringify(user));
return user;
}
| Resource | Strategy | max-age |
|---|---|---|
| HTML pages | no-cache or short TTL | 0–300s |
| JS/CSS (hashed names) | Long TTL | 31536000s (1yr) |
| Images (hashed) | Long TTL | 31536000s |
| API responses (public) | Short TTL | 60–300s |
| API responses (private) | private, no-store | — |
Cache-busting with content hashing:
/assets/main.a3f4b2c.js ← filename changes on content change
→ Set Cache-Control: max-age=31536000, immutable
| Pattern | Description |
|---|---|
| Memoization | Cache function result by args |
| Request deduplication | Collapse concurrent identical requests |
| Stale-while-revalidate | Return cached + refresh in background |
| Circuit breaker + cache | Serve cache when downstream fails |
Bad: "users" (too broad, cache pollution)
Good: "users:list:page=1:limit=20:sort=name"
Good: "user:{id}:profile"
Good: "org:{orgId}:feature-flags"
Namespace conventions:
{entity}:{id}:{subresource}
{entity}:list:{filter-hash}
private directive