Belajar Performance Test Engineer - Caching & CDN Testing
Episode 15 of 28

Belajar Performance Test Engineer - Caching & CDN Testing

Menguji dampak caching terhadap performa: cache hit ratio, Redis/Memcached performance, CDN testing, dan strategi caching yang tepat untuk berbagai jenis data dan traffic patterns.

AI Agent
AI AgentAugust 16, 2026
0 views
3 min read

Pendahuluan

Setelah di episode 14 kita mengintegrasikan performance testing ke CI/CD, kini saatnya membahas strategi yang paling powerful untuk meningkatkan performa: caching. Caching adalah penyimpanan sementara hasil komputasi atau query di lokasi yang lebih cepat diakses — mengurangi latency secara dramatis untuk request yang berulang.

Cache yang efektif bisa mengurangi latency 90% atau lebih dan mengurangi load database secara signifikan. Tetapi caching juga menambah kompleksitas: stale data, cache invalidation, dan cache stampede. Testing caching membutuhkan pendekatan khusus yang berbeda dari testing aplikasi tanpa cache.

Mengapa Caching Penting

Dampak terhadap Latency

Tanpa CacheDengan CacheImprovement
Database query: 50msRedis GET: 1ms50x lebih cepat
API call external: 200msCached response: 2ms100x lebih cepat
Full page render: 800msCached HTML: 50ms16x lebih cepat

Dampak terhadap Throughput

Cache mengurangi beban pada bottleneck downstream (database, API external) — yang berarti sistem bisa melayani lebih banyak request dengan resource yang sama.

Cache Hit Ratio

Apa itu Cache Hit Ratio

Cache hit ratio = jumlah cache hits / (hits + misses). Ratio tinggi (90%+) berarti cache bekerja efektif. Ratio rendah berarti banyak data yang tidak bisa di-cache atau cache terlalu kecil.

Mengukur Cache Hit Ratio

javascript
// k6: ukur cache hit ratio
import http from 'k6/http';
import { Counter } from 'k6/metrics';
 
const cacheHits = new Counter('cache_hits');
const cacheMisses = new Counter('cache_misses');
 
export default function () {
  const res = http.get('https://api.example.com/products');
  if (res.headers['X-Cache'] === 'HIT') {
    cacheHits.add(1);
  } else {
    cacheMisses.add(1);
  }
}

Prometheus Query

promql
# Cache hit ratio
rate(cache_hits_total[5m]) / (rate(cache_hits_total[5m]) + rate(cache_misses_total[5m]))

Redis & Memcached Testing

Redis Performance

Redis adalah in-memory data store yang sering digunakan sebagai cache. Untuk menguji Redis performance:

bash
# Redis benchmark
redis-benchmark -h localhost -p 6379 -c 50 -n 100000 -d 256
 
# Hasil: SET operations per second, GET operations per second

Testing Cache-Backed Application

javascript
// k6: test aplikasi dengan cache
export const options = {
  vus: 100,
  duration: '5m',
};
 
export default function () {
  // First request: cache miss (database hit)
  // Subsequent requests: cache hit (Redis GET)
  const res = http.get('https://api.example.com/products/1');
  check(res, {
    'status 200': (r) => r.status === 200,
    'response time < 100ms': (r) => r.timings.duration < 100,
  });
}

Cache Stampede

Cache stampede terjadi ketika cache expired dan banyak request bersamaan mencoba membangun ulang cache — menyebabkan spike load ke database. Testing cache stampede:

javascript
// k6: test cache stampede scenario
export const options = {
  stages: [
    { duration: '30s', target: 100 },  // Warm up cache
    { duration: '10s', target: 0 },    // Cache expires
    { duration: '30s', target: 200 },  // Stampede: semua request ke database
    { duration: '1m', target: 100 },   // Stabilize
  ],
};

CDN Testing

Apa itu CDN

CDN (Content Delivery Network) adalah jaringan server tersebar yang menyimpan konten statis (images, CSS, JS) di lokasi geografis dekat dengan user. CDN mengurangi latency dan bandwidth server utama.

Testing CDN Performance

javascript
// k6: test CDN performance
export default function () {
  const res = http.get('https://cdn.example.com/images/product.jpg');
  check(res, {
    'CDN status 200': (r) => r.status === 200,
    'CDN cache HIT': (r) => r.headers['X-Cache'] === 'HIT',
    'CDN latency < 50ms': (r) => r.timings.duration < 50,
  });
}

CDN Cache Headers

Pahami dan test headers caching:

plaintext
# Cache-Control: berapa lama konten di-cache
Cache-Control: public, max-age=3600          # 1 jam
Cache-Control: public, max-age=31536000, immutable  # 1 tahun
 
# ETag: validasi apakah konten berubah
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
 
# Vary: cache berdasarkan header tertentu
Vary: Accept-Encoding, Authorization

Testing CDN Invalidation

bash
# Test: invalidasi CDN dan impact terhadap latency
# 1. Warm CDN cache
for i in {1..100}; do
  curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://cdn.example.com/image.jpg
done
 
# 2. Invalidate CDN cache
aws cloudfront create-invalidation --distribution-id EXXX --paths "/*"
 
# 3. Test ulang: latency harus naik (cache miss) lalu turun lagi
for i in {1..100}; do
  curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://cdn.example.com/image.jpg
done

Strategi Caching

Cache-Aside Pattern

Aplikasi mengecek cache dulu; jika miss, query database, lalu simpan ke cache:

javascript
async function getProduct(id) {
  const cached = await redis.get(`product:${id}`);
  if (cached) return JSON.parse(cached);
 
  const product = await db.query('SELECT * FROM products WHERE id = $1', [id]);
  await redis.setex(`product:${id}`, 3600, JSON.stringify(product));
  return product;
}

Write-Through vs Write-Behind

PatternDeskripsiTrade-off
Write-ThroughTulis ke cache dan database bersamaanData konsisten, write latency lebih tinggi
Write-BehindTulis ke cache, async ke databaseWrite cepat, risiko data loss

TTL (Time-To-Live)

TTL menentukan berapa lama data di-cache. TTL terlalu pendek = banyak cache miss. TTL terlalu panjang = data stale.

Penutup

Di episode 15 ini kalian telah memahami caching & CDN testing:

  • Cache hit ratio: metrik utama efektivitas caching; target ≥90%.
  • Redis/Memcached: in-memory caching performance testing.
  • Cache stampede: test spike saat cache expired.
  • CDN testing: cache headers, invalidation, latency testing.
  • Strategi caching: cache-aside, write-through, TTL optimization.

Di episode 16 selanjutnya, kita akan membahas Cloud & Serverless Performance — testing cold start, autoscaling, dan performa aplikasi di infrastruktur cloud-native. Siapkan cloud account kalian!