advanced
Cloud Functions
Handle event-driven functions with managed triggers, short execution windows, IAM, cold starts, and deployment versions.
Cloud Functions runs short, event-driven Node.js handlers triggered by HTTP, Pub/Sub, Cloud Storage, or Firestore events. Gen 2 builds on Cloud Run, so you get concurrency settings and longer timeouts while keeping a function-centric deploy model.
import functions from '@google-cloud/functions-framework';
functions.http('webhook', async (req, res) => {
const eventId = req.get('x-event-id');
if (!eventId) {
res.status(400).send('missing event id');
return;
}
await processWebhook(req.body, { eventId });
res.status(204).send();
});
| Trigger | Typical FullStack use | |---------|----------------------| | HTTP | Webhooks and lightweight APIs | | Pub/Sub | Async workers reacting to domain events | | Storage | Image processing after user upload |
Keep handlers idempotent, finish within timeout, and push heavy work to queues or Cloud Run services.
On interviews: Functions versus Cloud Run; cold starts; idempotency with at-least-once delivery; IAM on invokers; environment secrets via Secret Manager.
Common pitfalls: long CPU work inside one invocation; relying on in-memory state between calls; logging secrets; unbounded retries on poison messages.
The trade-off is fast event glue code versus tight runtime limits and harder local parity for complex services.
Checklist:
- Design handlers to be idempotent and stateless.
- Offload long jobs to Pub/Sub consumers or Cloud Run jobs.
- Scope IAM to specific triggers and service accounts.
- Load secrets from Secret Manager, not env files in repo.