How do you handle state in serverless applications?

Answer

Since serverless functions are stateless (state is lost after execution), all state must live in external services: (1) User session state — store in DynamoDB (low-latency reads, millisecond response) or ElastiCache (Redis) (in-memory, fastest for session data); use a session token in cookies or JWTs; (2) Workflow state — use AWS Step Functions for multi-step process state, or store state in DynamoDB and pass workflow tokens; (3) CachingElastiCache (Redis/Memcached) for database query results; initialize the Redis connection in the module scope to reuse across warm invocations; (4) File/binary state — store in S3; (5) Ephemeral state within one invocation/tmp provides 512MB–10GB ephemeral disk (cleared between cold starts); (6) Lambda execution context reuse — initialize expensive connections (DB, HTTP clients) outside the handler; they persist on warm containers. Design principle: embrace statelessness — it's what enables infinite scaling.