Mengapa Caching Penting?
Caching menyimpan hasil komputasi atau data yang sering diakses agar tidak perlu diproses ulang. Ini mengurangi latency dan beban server.
Layer Caching
Browser Cache → CDN → Reverse Proxy Cache → App Cache → Database Cache
(terdekat ke user) (terdekat ke data)
1. Browser Cache (HTTP Headers)
Cache-Control Directives
# Siapa yang boleh cache?
Cache-Control: public # browser + CDN + proxy
Cache-Control: private # hanya browser (response per-user)
# Berapa lama?
Cache-Control: max-age=3600 # client cache 1 jam (detik)
Cache-Control: s-maxage=86400 # CDN cache 1 hari (override max-age di shared cache)
# Boleh cache tapi harus validasi dulu?
Cache-Control: no-cache # CACHE boleh, tapi wajib revalidate tiap request
Cache-Control: must-revalidate # setelah expired, wajib revalidate (jangan pakai stale)
# Sama sekali jangan cache
Cache-Control: no-store # jangan simpan apapun (data sensitif, banking)
# Untuk assets dengan hash di nama file (style.abc123.css)
Cache-Control: public, max-age=31536000, immutable
# immutable = jangan bother revalidate, isi tidak akan berubah
ETag & Last-Modified: Conditional Requests
# Server mengirim:
ETag: "v2-abc123"
Last-Modified: Wed, 10 Apr 2025 10:00:00 GMT
# Browser cache expired, kirim request validasi:
GET /api/profile
If-None-Match: "v2-abc123"
If-Modified-Since: Wed, 10 Apr 2025 10:00:00 GMT
# Server cek — tidak berubah? Balas:
HTTP/1.1 304 Not Modified
# Body kosong! Hemat bandwidth, browser pakai cache lokal.
# Jika berubah:
HTTP/1.1 200 OK
ETag: "v3-def456"
# kirim body baru
ETag adalah hash/version konten. Last-Modified adalah timestamp. ETag lebih presisi (konten sama meski timestamp beda tetap cache hit).
Vary: Cache Berbeda per Header
# Response berbeda tergantung Accept-Language atau Accept-Encoding
Vary: Accept-Encoding, Accept-Language
# CDN akan simpan entry terpisah untuk:
# - gzip vs brotli
# - en-US vs id-ID
# Tanpa Vary, user bisa dapat cache versi bahasa salah!
CDN Behavior
s-maxageoverridemax-ageuntuk shared cache (CDN) — bisa cache lebih lama di edge daripada di browser- CDN respect
no-storedanprivate— tidak akan cache - Gunakan
stale-while-revalidate=60untuk serve cache lama selama fetch fresh di background - Purge / invalidate manual tersedia di API CDN (Cloudflare, CloudFront) untuk emergency flush
Pattern Nginx
# Static assets dengan hash di filename — cache lama, immutable
location ~* \.(js|css|woff2|png|jpg|webp)$ {
expires 1y;
add_header Cache-Control "public, max-age=31536000, immutable";
}
# HTML — selalu fresh (karena reference assets versi terbaru)
location ~* \.html$ {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
# API — tergantung endpoint
location /api/public/ {
add_header Cache-Control "public, max-age=300"; # 5 menit
}
location /api/me {
add_header Cache-Control "private, no-cache"; # selalu revalidate
}
2. CDN (Content Delivery Network)
CDN menyimpan copy konten statis di edge server dekat user:
- Cloudflare — CDN + WAF + DDoS protection
- Vercel Edge — Optimized untuk Next.js
- AWS CloudFront — Integrasi AWS
3. Application-level Cache
# Redis sebagai cache
SET user:123 "{...}" EX 3600 # cache 1 jam
# Laravel cache
Cache::remember("user.123", 3600, function () {
return User::find(123);
});
# Invalidation strategy
Cache::forget("user.123"); # manual invalidation
Cache::tags(["users"])->flush(); # flush by tag
Cache-aside (Lazy Loading)
Pola cache-aside adalah strategi caching yang paling umum: aplikasi cek cache dulu, jika tidak ada (cache miss) baru ambil dari database lalu simpan ke cache. Contoh Cache::remember() di Laravel adalah implementasi cache-aside.
Cache Invalidation
"There are only two hard things in CS: cache invalidation and naming things."
- TTL (Time to Live) — Expire otomatis setelah waktu tertentu
- Event-based — Invalidate saat data berubah
- Versioning — URL berisi hash/version (untuk assets)