# Serverless architecture

### **Whats is Serverless?**

🏗️ Traditional servers (old way)

* You rent or buy servers (physical or virtual).
* You install operating systems, updates, and patches.
* You must decide how much CPU/RAM you need before you know the traffic.
* If too many users come at once, your server may crash.

⚡ Serverless (new way)

* You don’t manage servers — the cloud provider (AWS, Azure, GCP) runs them for you.
* You just write small pieces of code (called functions) or run apps.
* The cloud automatically gives you as much power as you need when people use your app.
* When no one is using it, you pay nothing.

📖 Simple analogy

Think of it like Uber vs. owning a car:

* Owning a car (traditional server): You pay for it even if it sits unused. You must maintain it.
* Uber (serverless): You only pay when you need a ride. You don’t worry about fuel, repairs, or parking.

### **Components of serverless**

1\. Compute (Brains)

* This is where your code runs without servers.
* Example:
  * AWS Lambda
  * Azure Functions
  * Google Cloud Functions

👉 Runs only when triggered, then stops.

2\. Event Sources (Triggers)

* Something has to wake up your function.
* Example triggers:
  * A user clicks a button on a website
  * A file is uploaded to storage (like S3 bucket)
  * A new row is added to a database
  * A message arrives in a queue

3\. Storage

* Since compute is stateless & ephemeral, you need permanent storage.
* Example:
  * Object storage: AWS S3, Azure Blob
  * Database: DynamoDB, Cosmos DB, Firebase

4\. API Gateway

* Lets users/apps talk to your serverless functions over the internet.
* Example: AWS API Gateway, Azure API Management, Google API Gateway.

👉 Think of it like a receptionist that takes requests and forwards them to your functions.

5\. Messaging & Queues

* Helps functions communicate and handle events in order.
* Example:
  * AWS SQS (queue)
  * AWS SNS (notifications)
  * Kafka, Pub/Sub

6\. Orchestration / Workflow

* Sometimes functions need to run in a sequence or flow.
* Example:
  * AWS Step Functions (workflow)
  * Azure Durable Functions

7\. Monitoring & Logging

* You still need to see what’s happening.
* Example:
  * AWS CloudWatch
  * Azure Monitor
  * Google Cloud Logging

Imagine a restaurant kitchen:

* Compute (Chef): Prepares the dish (runs code).
* Event source (Waiter): Brings the order (trigger).
* Storage (Fridge): Keeps ingredients safe (data).
* API Gateway (Receptionist): Takes customer orders.
* Queue (Order board): Manages multiple requests.
* Orchestration (Head Chef): Coordinates the cooking flow.
* Monitoring (CCTV/Manager): Ensures quality and reports issues.

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2F0M1dmYIJAB4aN5e2M0ev%2Fimage.png?alt=media&#x26;token=8e1b9f2b-10d9-4e6c-ac76-a552d580592f" alt=""><figcaption></figcaption></figure>

### &#x20;**Stateless**

* Meaning: No memory of the past.
* Each request is treated as brand new, with no knowledge of what happened before.

👉 Example:

* When you go to a fast-food counter, each order is fresh. The cashier doesn’t remember your last order unless you tell them again.
* In cloud/serverless, this means: if your app gets a request, it doesn’t “remember” the previous request.

⚡ Why?

* Makes scaling easy (any server/function can handle the next request).

### **Ephemeral**

* Meaning: Short-lived, temporary, not permanent.
* Resources or sessions exist only for a little while, then disappear.

👉 Example:

* Snapchat messages = ephemeral (they vanish after you read them).
* In cloud, a container, virtual machine, or function may run just for a few seconds/minutes, then shut down.

⚡ Why?

* Saves cost and resources.
* Perfect for tasks that don’t need to live forever.

**Architecture**

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2F4Em1U0tFr09uYsgTJUoi%2Fimage.png?alt=media&#x26;token=ebf601d9-184c-43c1-b002-3f49a4dea23b" alt=""><figcaption></figcaption></figure>

### 🔐 **Amazon Cognito**

* A user identity and access management service from AWS.
* It helps you add login, signup, and authentication to your apps without building it from scratch.

🧩 Main parts of Cognito

1\. User Pools 👥

* This is like a user directory.
* Stores usernames, passwords, emails, phone numbers.
* Handles:
  * Signup & Sign-in
  * Forgot password
  * Multi-Factor Authentication (MFA)
  * Social logins (Google, Facebook, Apple)

👉 Example: If you build an app, Cognito can be the “Login with Email or Google” system.

2\. Identity Pools (Federated Identities) 🎭

* Lets users get temporary AWS credentials to access AWS services (like S3 or DynamoDB).
* Works with User Pools OR external identity providers.
* Example:
  * Your app’s users log in → Cognito gives them a temporary token → They can upload files to S3 without needing permanent AWS keys.

3\. Cognito Sync (deprecated, replaced by AppSync / Amplify) 📦

* Was used for syncing app data across devices (like preferences).

⚡ Simple Analogy

Think of Cognito like a bouncer + guest list at a club:

* User Pool = The guest list (who is allowed in, with what password/ID).
* Identity Pool = The wristband that lets you access certain areas inside (like AWS services).

### 🎬 **Transcoding**

* Definition: Converting a media file (video or audio) from one format, size, or quality into another.
* It’s like changing the “clothes” of a video so it can fit different devices and networks.

🧩 Example in real life

1. You record a 4K video on your phone.
2. Your friend has slow internet and an old laptop.
3. You transcode the video into 720p smaller file size, so they can watch it smoothly.

🛠️ In the cloud (like AWS)

* AWS Elemental MediaConvert is a serverless service for transcoding.
* Example:
  * Upload video to S3 → MediaConvert transcodes it → outputs multiple versions (4K, 1080p, 720p).
  * Streaming services (like Netflix, YouTube) do this automatically, so every user gets the best quality for their device.

✅ Difference from related terms

* Encode: Convert raw video/audio into a compressed format (e.g., camera video → MP4).
* Transcode: Convert from one encoded format to another (e.g., MP4 → WebM, or 4K → 1080p).

⚡ Analogy

Transcoding is like translating a book into different languages and versions:

* Same story (video content).
* Different format/size so everyone can enjoy it on their own “device.”
