Apa itu Serverless?
Serverless bukan berarti "tanpa server" — server tetap ada, tapi kamu tidak perlu mengelolanya. Kamu hanya menulis function, cloud provider yang menjalankan dan men-scale-nya.
Cara Kerja
Request → API Gateway → Lambda Function → Response
│
Scale otomatis (0 → N instances)
Bayar per eksekusi
Contoh: Vercel Serverless Function
// api/hello.js (Vercel)
export default function handler(req, res) {
const { name } = req.query;
res.status(200).json({ message: `Hello, ${name || "World"}!` });
}
// Endpoint: https://myapp.vercel.app/api/hello?name=Dev
Contoh: AWS Lambda
// handler.js
exports.handler = async (event) => {
const body = JSON.parse(event.body);
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ result: body.a + body.b }),
};
};
Kapan Pakai Serverless?
- Cocok: API endpoint, webhook handler, cron job, image processing, email sender
- Kurang cocok: Long-running tasks (>15 min), WebSocket, application dengan banyak state
Cold Start
Saat function tidak dipakai beberapa menit, instance dimatikan. Request pertama setelah itu perlu waktu lebih lama (cold start). Solusi: provisioned concurrency atau keep-warm strategy.