Skip to content
Back to all concepts
CloudServerlessAWSArchitecture

Serverless Architecture (FaaS)

Functions as a Service (AWS Lambda). The Event-Driven paradigm shift. Benefits (Scaling to Zero) vs Drawbacks (Cold Starts, Vendor Lock-in).

By ScaleWikiUpdated 3 min readIntermediate

No Servers? No, Just Other People's Servers.

Serverless: You write code (Function). Cloud Provider manages infrastructure (OS, patching, scaling).

Key Traits:

  1. Event-Driven: Code sleeps until triggered (HTTP, S3 upload, Queue message).
  2. Ephemeral execution: Local state may survive a warm invocation but is not durable; put required state in an external store.
  3. Scale to zero: On-demand compute can fall to no active invocations. Storage, provisioned capacity and other services can still incur charges.

Architecture Patterns

1. Web API (API Gateway + Lambda)

Replaces EC2 + Nginx.

2. Async Processing (S3 + Lambda)

Image resizing pipeline.

  1. User uploads photo.jpg to S3 Bucket raw-images.
  2. S3 sends event ObjectCreated to Lambda.
  3. Lambda resizes image and saves to processed-images.

3. Fan-Out (SNS + multiple Lambdas)

  1. User registers.
  2. Publish "UserCreated" to SNS Topic.
  3. Lambda A (Email Service): Sends Welcome Email.
  4. Lambda B (Analytics): Updates Dashboard.

The Cold Start Problem ❄️

The biggest drawback.

  1. Request arrives: AWS finds no running container.
  2. Download Code: Pulls your zip file (50MB) from S3.
  3. Start Container: Spins up Firecracker MicroVM.
  4. Init Runtime: Starts Python/Node/Java process.
  5. Execute Handler: Runs your code.

Latency: Depends on package size, runtime, initialization and platform configuration. Measure cold and warm invocation distributions separately.

Mitigation:

  • Warming pings: May reuse an environment but do not guarantee capacity or eliminate cold starts.
  • Provisioned Concurrency: Pay to keep NN instances warm.
  • Micro-Frameworks: Don't use heavy frameworks (Spring/Django). Use lightweight ones (Flask/Express/Go).

Limitations

FeatureServerless (Lambda)Containers (Fargate/K8s)Virtual Machines (EC2)
Max Runtime15 minutes (Hard limit)UnlimitedUnlimited
Disk SpaceConfigurable ephemeral storage; check current quotasDepends on attached storageDepends on attached storage and quotas
Connection LimitsMassive concurrency kills DBs (Connection Pooling needed)Controlled scalingControlled scaling
CostDepends on duration, memory and invocationsDepends on provisioned capacity and utilizationDepends on instance/storage commitments and utilization

Code Example: AWS Lambda (Python)

python
import json
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Users')

def lambda_handler(event, context):
    """
    Triggered by API Gateway.
    event: { "body": "{\"name\": \"Alice\"}" }
    """
    # Avoid logging complete request bodies or credentials.
    
    try:
        # Parse Input
        body = json.loads(event.get('body') or '{}')
        if not isinstance(body, dict):
            return {"statusCode": 400, "body": "Expected JSON object"}
        name = body.get('name')
        
        if not isinstance(name, str) or not name.strip():
            return {"statusCode": 400, "body": "Missing name"}

        # Business Logic
        user_id = save_user(name)
        
        return {
            "statusCode": 200,
            "body": json.dumps({"message": "Success", "id": user_id})
        }
        
    except (json.JSONDecodeError, TypeError):
        return {"statusCode": 400, "body": "Invalid JSON"}
    except Exception as e:
        print(f"Storage error: {type(e).__name__}")
        return {"statusCode": 500, "body": "Internal Error"}

def save_user(name):
    item = {"pk": name, "status": "active"}
    table.put_item(Item=item)
    return name

Interview Tips 💡

  • "When NOT to use Serverless?" — Long-running tasks (>15m), WebSocket servers (need stateful connections), Heavy GPU tasks (Training), High-frequency trading (latency variance).
  • "Idempotency" — Retry behavior depends on the event source and invocation mode. Duplicate events are possible; use stable idempotency keys and atomic deduplication, not an unprotected check-then-write.
  • "Vendor Lock-in" — Moving Lambda logic to Google Cloud Functions requires rewriting infrastructure code (Terraform helps, but logic is tied to SDKs).

Worked decision: a burst that exhausts the database

Assume 500 function instances each open ten database connections: the application can attempt 5,000 connections even though individual functions look healthy. Set concurrency against the downstream budget, use an appropriate proxy or pool, and queue work that can wait. Compare the cost of provisioned concurrency with the latency target rather than sending warming requests and assuming the problem is solved.

The Python fragment above requires a Users DynamoDB table and IAM permissions. Its name-keyed overwrite is a storage demonstration, not user registration or a complete idempotent API. AWS documents duplicate-event handling in its Lambda application design guidance.