Back to All Concepts
CloudServerlessAWSArchitectureIntermediate

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).

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. Stateless: Functions are ephemeral. No local disk persistence.
  3. Scale to Zero: If no traffic, you pay $0.

Architecture Patterns

1. Web API (API Gateway + Lambda)

Replaces EC2 + Nginx.

mermaid
graph LR
    User -->|HTTP Request| APIGw[API Gateway]
    APIGw -->|Invoke| Lambda[Lambda Function]
    Lambda -->|Read/Write| Dynamo[(DynamoDB NoSQL)]
Click to expand code...

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.

Total Latency: 200ms (Node.js) to 10s (Java Spring Boot).

Mitigation:

  • Keep Warming: Ping function every 5 mins.
  • 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 Space512MB - 10GB (Ephemeral)Persistent VolumesInfinite (EBS)
Connection LimitsMassive concurrency kills DBs (Connection Pooling needed)Controlled scalingControlled scaling
CostExpensive at high sustained loadCheaper at scaleCheapest (Reserved Instances)

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\"}" }
    """
    print(f"Received event: {event}")
    
    try:
        # Parse Input
        body = json.loads(event.get('body', '{}'))
        name = body.get('name')
        
        if not name:
            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 Exception as e:
        print(f"Error: {e}")
        return {"statusCode": 500, "body": "Internal Error"}

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

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" — Lambda guarantees "At Least Once" delivery. Your function might run twice for the same event. Make it idempotent! (Check DB before writing).
  • "Vendor Lock-in" — Moving Lambda logic to Google Cloud Functions requires rewriting infrastructure code (Terraform helps, but logic is tied to SDKs).

Related Concepts

About ScaleWiki

ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.

Read more about our Editorial Guidelines & Authorship.

Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.

Related Articles