Quick answer
Use serverless functions for bounded, independently executable requests or events when the platform’s lifecycle and resource limits fit them. Use long-running servers or workers when the workload needs persistent connections, continuous processing, or predictable process ownership. Either choice still needs durable storage and recovery. Serverless transfers process provisioning to a provider; it does not make background work after a response reliable or remove execution limits.
Practical implementation steps
1. Classify work by lifetime
Separate a short API read from a report export, continuous stream consumer, or connection gateway. Check maximum duration, payload size, memory, concurrency, and event-delivery behavior for the actual platform and mode. AWS Lambda documents bounded execution and mode-specific timeout limits; those values are not universal serverless limits. Use representative large inputs, not a tiny local sample, to evaluate the risky task.
2. Separate process reuse from durable state
A provider may reuse a warm execution environment, but the application must not rely on that reuse for business records. Global memory and temporary files can disappear or differ across instances. A persistent server can also restart or scale into several processes. Store accepted jobs and customer records in the authoritative durable system, and treat process-local caches as expendable acceleration.
3. Match connection and concurrency behavior
Rapidly scaling functions can create many database connections unless a suitable pool or proxy controls them. Long-running processes can reuse pools but must drain work during deployment and tolerate connection loss. Do not assume autoscaling a frontend fixes a downstream bottleneck. Define admission limits, timeouts, and backpressure according to what the database or provider can safely accept.
4. Define acknowledgement and shutdown
A request accepting a long export should record durable work or submit it to a durable queue before reporting acceptance. The worker completes it separately and records status. For a continuous server, handle termination signals, stop taking new work, and finish or safely release leases. A task started without awaiting it inside a function can be frozen or lost when the invocation ends.
Implementation example
This interface makes durable acceptance part of the request contract. It does not implement a queue or claim exactly-once processing. The queue adapter must persist the job before resolving, and the worker must use requestId to prevent duplicate business effects. Return accepted status only after that contract succeeds; notification delivery can be a later step.
type ExportJob = {
requestId: string;
verifiedOwnerId: string;
reportId: string;
};
interface DurableQueue {
enqueue(job: ExportJob): Promise<{ jobId: string }>;
}
export async function acceptExport(queue: DurableQueue, job: ExportJob) {
const { jobId } = await queue.enqueue(job);
return { status: "accepted" as const, jobId };
}
Tradeoffs and alternatives
Functions reduce idle infrastructure ownership and can fit variable event workloads. A persistent process gives more explicit lifecycle control and may suit continuous load. Both need monitoring, credentials, dependency updates, and recovery procedures. A hybrid public frontend plus worker can be simpler than forcing long work into a request function or moving every route to a server solely for one export feature.
Common mistakes
Avoid durable writes to ephemeral files, work started after a success response without a durable owner, uncontrolled connection multiplication, and timeout settings derived from average rather than upper-bound inputs. Do not assume a container automatically provides persistent data or safe shutdown. Finally, cold-start anecdotes are not evidence for your workload; measure first and repeated requests in the intended runtime.
Production checklist
- Every workload fits the chosen execution limits.
- Accepted work and records survive process termination.
- Database connections and concurrency have limits.
- Workers can retry safely and expose status.
- Deployment shutdown and recovery are documented.
FAQ
Is serverless always cheaper?
No. Compare the real execution frequency, duration, memory, downstream costs, and operating work. Avoid making a price claim without a workload and current provider pricing.
Does a long-running server guarantee job completion?
No. Processes fail and deployments interrupt them. Durable job state, leases, retries, and idempotent effects provide recovery independently of the hosting label.
Next step
Measure the longest realistic task and map what happens when its process stops halfway through. Choose a runtime that fits its lifetime, then add a durable handoff when acceptance must outlive the request. Keep short public reads independent of worker topology.
Primary sources
Related guides and examples
Need this implemented in a real product?
One connected implementation across the interface, application logic, APIs, and data layer.