- Scalability (Horizontal vs. Vertical Scaling)
- API Gateway
- Load Balancing (Round Robin, Least Connections, etc.)
- Caching (CDN, Redis, Memcached, Cache Invalidation)
- CAP Theorem & PACELC Theorem
- Bloom Filters
- Read about API gateways (Kong, Nginx, AWS API Gateway).
- Implement a load balancer using Nginx and HAProxy.
- Use Redis for caching in a Node.js project.
- Study CAP theorem and apply its principles in designing databases.
- Implement Bloom Filters in JavaScript.
- SQL vs. NoSQL Databases (MongoDB, PostgreSQL, MySQL)
- Data Modeling and Normalization
- Indexing Strategies (B-Trees, Hash Indexes)
- Partitioning (Range, Hash, List, Composite)
- Sharding (Key-based, Range-based, Directory-based)
- Replication (Leader-Follower, Multi-Leader, Peer-to-Peer)
- Set up and configure a PostgreSQL database.
- Implement sharding and replication in MongoDB.
- Explore database indexing using PostgreSQL.
- Design a scalable database schema for a social media app.
- Basics of Computer Networking
- REST API Principles
- gRPC and Protocol Buffers
- GraphQL (Schema Design, Queries, Mutations)
- DNS & Proxies
- WebSockets & Long Polling
- Build RESTful and GraphQL APIs in Node.js.
- Implement gRPC communication in a microservice.
- Set up WebSockets for real-time chat applications.
- Configure a reverse proxy using Nginx.
- Distributed Systems Basics
- Consistency Models (Strong, Eventual, Causal)
- Quorum Mechanism
- Leader-Follower Architecture
- Merkle Trees & Data Integrity
- Consistent Hashing
- Build a leader-follower replication model using PostgreSQL.
- Implement consistent hashing in a caching layer.
- Study and design distributed log processing systems (Kafka).
- Simulate quorum-based decision-making in a database cluster.
- API Design Best Practices
- Architectural Patterns (Monolith vs. Microservices)
- Event-Driven Architecture (Pub/Sub Model, Message Queues)
- Sharding Pattern
- Circuit Breaker Pattern
- Static Content Hosting
- Refactor a monolithic Node.js app into microservices.
- Implement a message queue with RabbitMQ or Kafka.
- Use a circuit breaker library like opossum in Node.js.
- Deploy a microservices-based application on Kubernetes.
Build a scalable, real-time application (e.g., chat app, stock price tracker) incorporating:
- Load balancing and caching
- Scalable database design (sharding, replication)
- Efficient API communication (REST, GraphQL, WebSockets)
- Microservices and event-driven architecture
This structured plan will help you progress from system design fundamentals to advanced distributed systems, ensuring a strong foundation in designing scalable and efficient systems with Node.js. π
We'll break down the topics into 7 days to move from basic to advanced concepts.
- What is Scalability?
- Scalability is the ability of a system to handle increased load by adding resources. It ensures that performance remains stable as traffic grows.
- Types of Scalability:
- Vertical Scaling (Scaling Up)
- Adding more resources (CPU, RAM, Storage) to a single server.
- Limited by hardware capacity.
- Example: Upgrading a server from 8GB RAM to 32GB.
- Horizontal Scaling (Scaling Out)
- Adding more machines (servers) to handle traffic.
- Load is distributed across multiple instances.
- Example: Deploying multiple instances behind a load balancer.
- Vertical Scaling (Scaling Up)
- When to Use?
- Vertical Scaling is easier but expensive and has a limit.
- Horizontal Scaling is more flexible but requires extra infrastructure (load balancers, distributed databases).
π Activity: Research how companies like Netflix, Amazon, and Google scale their applications.
- What is an API Gateway?
- An API Gateway is a single entry point for client requests. It acts as a reverse proxy and handles routing, authentication, and rate limiting.
- Why Use an API Gateway?
- β Manages multiple microservices efficiently.
- β Improves security with authentication & rate limiting.
- β Reduces API call overhead by aggregating responses.
- Popular API Gateways:
- Kong API Gateway
- AWS API Gateway
- NGINX as API Gateway
π Activity: Install Kong API Gateway locally and set up a simple API.
- What is Load Balancing?
- Load balancing distributes incoming traffic across multiple servers to prevent overload.
- Types of Load Balancers:
- DNS Load Balancing (Traffic is distributed at the DNS level)
- Software Load Balancers (NGINX, HAProxy)
- Hardware Load Balancers (F5, Citrix NetScaler)
- Load Balancing Algorithms:
- Round Robin β Requests are evenly distributed.
- Least Connections β Sends requests to the server with the fewest connections.
- IP Hashing β Routes requests from the same client to the same server.
π Activity: Set up an NGINX Load Balancer and route traffic to multiple Node.js servers.
- What is Caching?
- Caching stores frequently accessed data to reduce database queries and improve performance.
- Types of Caching:
- CDN Caching β Stores static assets closer to users (Cloudflare, AWS CloudFront).
- Application Caching β Stores computed results (Redis, Memcached).
- Database Caching β Caches query results for faster retrieval.
- Cache Invalidation Strategies:
- Time-based Expiration (TTL - Time to Live).
- Write-through Cache (Data is updated in cache and DB).
- Lazy Loading (Cache updates only when data is requested).
π Activity: Install Redis and set up caching for a simple Node.js API.
- What is the CAP Theorem?
- It states that a distributed system cannot guarantee all three at the same time:
- Consistency (C) β All nodes see the same data at the same time.
- Availability (A) β Every request gets a response.
- Partition Tolerance (P) β System works despite network failures.
- β‘ Tradeoff: In reality, systems pick CP (Strong Consistency) or AP (High Availability).
- It states that a distributed system cannot guarantee all three at the same time:
- PACELC Theorem
- Extends CAP by adding a trade-off in the absence of network failure:
- If partitioned (P) β Choose A or C.
- Else (E) β Choose Latency (L) or Consistency (C).
- Extends CAP by adding a trade-off in the absence of network failure:
π Activity: Study how MongoDB (AP) and PostgreSQL (CP) follow CAP.
- What is a Bloom Filter?
- A probabilistic data structure used to check if an item may be present in a dataset (false positives possible).
- How it Works?
- Insert an item β Hash it to multiple positions in a bit array.
- Lookup β Check those positions; if all bits are 1, the item may exist.
- π Use Cases:
- Caching (Avoid unnecessary DB lookups).
- Spell checkers.
- Preventing duplicate registrations.
π Activity: Implement a Bloom Filter in JavaScript.
- Project: Build a Scalable API Service
- β Use NGINX for load balancing.
- β Implement caching using Redis.
- β Design an API Gateway using Kong.
- β Optimize API responses using Bloom Filters.
π Activity: Deploy your API and test its scalability with a load-testing tool like Apache JMeter.
π― What You've Learned in Week 1:
- βοΈ Scaling applications horizontally & vertically.
- βοΈ Setting up API Gateways & Load Balancers.
- βοΈ Implementing caching using Redis.
- βοΈ Understanding CAP & PACELC Theorems.
- βοΈ Using Bloom Filters for optimization.
π Next: Move to Week 2 - Databases & Data Storage! Would you like any hands-on coding examples for these topics? π
This week, we will explore SQL & NoSQL databases, data modeling, indexing, partitioning, sharding, and replication with hands-on implementation.
-
What is a Database?
- A database is a structured system for storing, retrieving, and managing data efficiently.
-
Types of Databases:
- SQL Databases (Relational Databases)
- Structured data stored in tables.
- Uses SQL for queries (SELECT, JOIN, etc.).
- ACID compliance (Atomicity, Consistency, Isolation, Durability).
- Examples: PostgreSQL, MySQL, MariaDB.
- NoSQL Databases (Non-Relational)
- Designed for flexible and high-performance applications.
- Supports unstructured or semi-structured data.
- BASE (Basically Available, Soft state, Eventually consistent).
- Examples: MongoDB (Document-based), Redis (Key-Value), Cassandra (Wide-Column), Neo4j (Graph-based).
- SQL Databases (Relational Databases)
-
When to Use What?
| Use Case | SQL (Relational) | NoSQL (Non-Relational) |
|---|---|---|
| Banking, Transactions | β Yes (ACID) | β No |
| Social Media, Real-time Apps | β No | β Yes (Scalability) |
| E-commerce | β Yes | β Yes (Hybrid Approach) |
| IoT, Big Data | β No | β Yes |
π Activity: Install PostgreSQL and MongoDB on your local machine. Create a sample table in PostgreSQL and a document in MongoDB.
-
What is Data Modeling?
- Data modeling defines the structure, relationships, and rules for storing and retrieving data efficiently.
-
Data Modeling in SQL:
- 1NF (First Normal Form): No duplicate columns.
- 2NF (Second Normal Form): No partial dependencies.
- 3NF (Third Normal Form): No transitive dependencies.
- BCNF (Boyce-Codd Normal Form): Stronger version of 3NF.
- Example: E-commerce Database
- Users (id, name, email, address)
- Orders (id, user_id, product_id, quantity, status)
- Products (id, name, price)
-
Data Modeling in NoSQL:
- Denormalization is preferred for fast reads.
- Schema is flexible (JSON, Key-Value, Graph).
- Example: A User Profile document in MongoDB:
{ "_id": "123", "name": "Alice", "email": "alice@example.com", "orders": [ {"product": "Laptop", "price": 1200}, {"product": "Mouse", "price": 50} ] }
π Activity: Design a normalized database for an online bookstore in PostgreSQL. Create a NoSQL schema for a messaging app in MongoDB.
-
What is Indexing?
- An index is a data structure that speeds up search queries.
-
Types of Indexes:
- B-Tree Index (Balanced Tree)
- Default in PostgreSQL, MySQL.
- Optimized for range queries.
- Hash Index
- Used for exact matches.
- Faster than B-Tree but not for range queries.
- GIN (Generalized Inverted Index)
- Used for full-text search.
- B-Tree Index (Balanced Tree)
-
Creating an Index in PostgreSQL:
CREATE INDEX idx_user_email ON users(email);
π Activity: Run an EXPLAIN ANALYZE query in PostgreSQL before and after indexing. Implement a MongoDB index on a user collection.
-
What is Partitioning?
- Partitioning divides a large table into smaller, more manageable pieces.
-
Types of Partitioning:
- Range Partitioning: Based on value ranges (e.g., Date).
- Hash Partitioning: Even distribution based on a hash function.
- List Partitioning: Based on predefined categories.
- Composite Partitioning: Combination of two or more partitioning types.
-
Partitioning in PostgreSQL:
CREATE TABLE orders ( id SERIAL PRIMARY KEY, user_id INT, total_price NUMERIC, created_at DATE ) PARTITION BY RANGE (created_at);
π Activity: Partition a sales table based on year in PostgreSQL. Implement range-based partitioning in MongoDB.
-
What is Sharding?
- Sharding splits large databases across multiple machines to improve scalability.
-
Types of Sharding:
- Key-based Sharding: Uses a hash function (e.g., User ID % number of shards).
- Range-based Sharding: Divides based on range (e.g., A-M goes to Shard 1, N-Z to Shard 2).
- Directory-based Sharding: A lookup service maps data to shards.
-
Sharding in MongoDB:
sh.enableSharding("myDatabase"); sh.shardCollection("myDatabase.users", { "_id": "hashed" });
π Activity: Implement range-based sharding in MongoDB for a large dataset. Study how Instagram uses sharding to scale.
-
What is Replication?
- Replication keeps multiple copies of a database across different servers.
-
Types of Replication:
- Leader-Follower (Master-Slave): Leader handles writes, followers handle reads.
- Multi-Leader (Master-Master): Multiple leaders for writes.
- Peer-to-Peer (No Master): All nodes are equal.
-
Replication in PostgreSQL:
SELECT pg_create_physical_replication_slot('replica_slot');
-
Replication in MongoDB:
rs.initiate(); rs.add("replica2:27017"); rs.add("replica3:27017");
π Activity: Set up PostgreSQL replication between two servers. Implement MongoDB replica set in Docker.
- Project:
- β Set up a PostgreSQL database with proper normalization.
- β Implement indexing and partitioning for optimization.
- β Shard a large dataset using MongoDB.
- β Set up replication to ensure high availability.
- β Design a scalable schema for a social media platform.
π Activity: Run load tests on your database using Apache JMeter or pgbench. Compare PostgreSQL vs. MongoDB performance for a use case.
π― What You've Learned in Week 2:
- βοΈ SQL vs. NoSQL databases and when to use them.
- βοΈ Data modeling techniques for relational & NoSQL databases.
- βοΈ Indexing to optimize queries.
- βοΈ Partitioning & sharding strategies for scalability.
- βοΈ Replication techniques for high availability.
π Next: Move to Week 3 - Networking & API Communication! Would you like coding examples for any of these topics? π
This week, you'll dive into networking fundamentals, REST API design, gRPC, GraphQL, WebSockets, and proxies, progressing from basic concepts to hands-on implementations.
- What is Networking?
- Networking is the process of connecting computers, servers, and devices to share data efficiently.
- Key Concepts:
- IP Addresses & Ports
- IPv4 vs. IPv6
- Ports (e.g., HTTP - 80, HTTPS - 443, MySQL - 3306)
- TCP vs. UDP
- TCP: Reliable, ordered, slower (e.g., HTTP, HTTPS).
- UDP: Fast, connectionless (e.g., video streaming, gaming).
- HTTP vs. HTTPS
- HTTP: Unencrypted, insecure.
- HTTPS: Secure (SSL/TLS).
- DNS (Domain Name System)
- Resolves domain names to IP addresses (e.g., google.com β 172.217.160.142).
- IP Addresses & Ports
π Activity:
Run ping google.com and traceroute google.com to see network paths. Test TCP vs. UDP by sending data with Netcat (nc).
-
What is REST?
- REST (Representational State Transfer) is an API design pattern that follows:
- β Statelessness (No session state stored on the server).
- β Client-Server Separation.
- β Resource-Based URLs (e.g., /users/{id}).
- REST (Representational State Transfer) is an API design pattern that follows:
-
RESTful API Methods:
| HTTP Method | Action | Example Endpoint |
|---|---|---|
| GET | Read Data | /users/1 |
| POST | Create Data | /users |
| PUT | Update Data | /users/1 |
| DELETE | Remove Data | /users/1 |
-
Example: Building a REST API in Node.js
const express = require('express'); const app = express(); app.use(express.json()); app.get('/users/:id', (req, res) => { res.json({ id: req.params.id, name: "John Doe" }); }); app.listen(3000, () => console.log('Server running on port 3000'));
π Activity: Test REST APIs using Postman or curl. Create CRUD APIs for a To-Do App using Express.js.
-
What is gRPC?
- gRPC is a high-performance, language-independent Remote Procedure Call (RPC) framework developed by Google.
- π Faster than REST due to Protocol Buffers (protobufs) instead of JSON.
-
Key Features:
- βοΈ Binary data format (protobuf) β Smaller, faster.
- βοΈ Strongly-typed contracts β API schema enforced.
- βοΈ Bidirectional streaming β Efficient real-time communication.
-
Example: Define a gRPC Service (user.proto)
syntax = "proto3"; service UserService { rpc GetUser (UserRequest) returns (UserResponse); } message UserRequest { string id = 1; } message UserResponse { string id = 1; string name = 2; }
-
Run gRPC Server in Node.js
const grpc = require('@grpc/grpc-js'); const protoLoader = require('@grpc/proto-loader'); const packageDefinition = protoLoader.loadSync('user.proto'); const userProto = grpc.loadPackageDefinition(packageDefinition).UserService; const server = new grpc.Server(); server.addService(userProto.service, { GetUser: (call, callback) => { callback(null, { id: call.request.id, name: "Alice" }); } }); server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => { server.start(); });
π Activity: Implement a gRPC microservice for user management. Compare REST vs. gRPC performance using Postman & BloomRPC.
-
What is GraphQL?
- GraphQL is a query language for APIs that allows clients to request only the data they need.
-
Key Benefits:
- βοΈ Single Endpoint (/graphql) β No multiple REST routes.
- βοΈ Strongly Typed Schema β Defines API contract.
- βοΈ No Over-fetching β Fetch only required fields.
-
GraphQL Schema Example (User Type)
type User { id: ID! name: String! email: String! } type Query { getUser(id: ID!): User } type Mutation { createUser(name: String!, email: String!): User }
-
Building a GraphQL API in Node.js
const { ApolloServer, gql } = require('apollo-server'); const typeDefs = gql` type User { id: ID! name: String! email: String! } type Query { getUser(id: ID!): User } type Mutation { createUser(name: String!, email: String!): User } `; const resolvers = { Query: { getUser: (_, { id }) => ({ id, name: "Alice", email: "alice@example.com" }), }, Mutation: { createUser: (_, { name, email }) => ({ id: "1", name, email }), }, }; const server = new ApolloServer({ typeDefs, resolvers }); server.listen(4000, () => console.log('GraphQL Server running on 4000'));
π Activity: Test GraphQL queries in Apollo Sandbox. Convert a REST API into GraphQL.
-
What is DNS?
- DNS (Domain Name System) translates domain names into IP addresses.
-
Types of Proxies:
- Forward Proxy: Acts on behalf of the client (e.g., VPNs).
- Reverse Proxy: Sits in front of the server (e.g., Nginx, Cloudflare).
-
Setting Up Nginx as a Reverse Proxy
server { listen 80; location /api/ { proxy_pass http://localhost:5000/; } }
π Activity:
Configure Nginx as a reverse proxy for a Node.js server.
Use dig or nslookup to analyze DNS resolution.
-
What is WebSockets?
- WebSockets allow bidirectional communication between client and server.
- π Used in chat apps, real-time notifications, stock trading apps.
-
WebSockets in Node.js
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', ws => { ws.send('Welcome to WebSocket Server!'); ws.on('message', message => console.log(`Received: ${message}`)); });
π Activity: Implement a real-time chat app using WebSockets. Compare WebSockets vs. Long Polling performance.
- β Build a REST API & GraphQL API for a user management system.
- β Implement a gRPC microservice for authentication.
- β Use WebSockets for real-time notifications.
- β Deploy with Nginx reverse proxy & load balancing.
π Congratulations! You've learned Networking & API Communication.
π Next: Move to Week 4 - Distributed Systems & Microservices!
This week, you'll explore distributed computing, consistency models, replication strategies, and fault tolerance techniques from basic concepts to hands-on implementations.
-
What is a Distributed System?
- A distributed system is a collection of computers working together as a single unit.
- π Examples: Google Search, Netflix, Kubernetes clusters.
-
Challenges in Distributed Systems:
- Latency & Network Failures β Communication delays and node failures.
- Consistency & Availability Trade-offs β CAP Theorem (Consistency, Availability, Partition Tolerance).
- Concurrency & Synchronization β Handling multiple requests efficiently.
-
Types of Distributed Architectures:
- Peer-to-Peer (P2P): Equal nodes (e.g., BitTorrent).
- Client-Server: Centralized servers handle clients (e.g., Web apps).
- Microservices: Independent services communicate via APIs (e.g., Netflix).
π Activity: Read about Google's Spanner & Amazon DynamoDB distributed systems. Set up a basic multi-node cluster using Docker Swarm.
-
Why is Consistency Important?
- Consistency ensures that all nodes in a distributed system have the same view of data.
-
Types of Consistency Models:
| Consistency Model | Description | Example |
|---|---|---|
| Strong Consistency | All nodes see the latest data immediately. | Traditional SQL databases (PostgreSQL, MySQL) |
| Eventual Consistency | Nodes may have stale data but will eventually be up-to-date. | NoSQL databases (DynamoDB, Cassandra) |
| Causal Consistency | Ensures cause-effect relationships in data updates. | Google Docs, collaborative editing apps |
π Activity: Simulate eventual consistency with MongoDB Replication. Experiment with strong consistency using PostgreSQL transactions.
-
What is Quorum?
- A quorum ensures a minimum number of nodes agree before an operation is confirmed.
- π Used in distributed databases (Raft, Paxos, Cassandra).
-
How Quorum Works in a Distributed Database:
- Read Quorum (R): Minimum nodes required to read data.
- Write Quorum (W): Minimum nodes required to confirm a write.
- Total Nodes (N): Typically, R + W > N ensures consistency.
-
Example: In Cassandra, setting R=2, W=2, N=3 ensures strong consistency.
π Activity: Simulate quorum-based reads & writes in Cassandra. Experiment with ZooKeeper quorum mechanisms.
-
What is Leader-Follower Replication?
- π Leader-Follower (Master-Slave) replication is a model where one node (Leader) writes data, while others (Followers) replicate it.
-
How It Works:
- Leader handles all writes β Ensures strong consistency.
- Followers replicate data asynchronously.
- Failover: If the leader fails, a new leader is elected.
-
Example: PostgreSQL Leader-Follower Setup
- Set up the Leader node
ALTER SYSTEM SET wal_level = 'replica'; SELECT pg_start_backup('backup');
- Configure the Follower node
cp -r data/* replica_data/ echo "standby_mode = 'on'" >> replica_data/postgresql.conf
- Start the Follower instance
pg_ctl start -D replica_data
π Activity: Implement leader-follower replication in PostgreSQL. Use Redis Sentinel for automatic failover in Redis clusters.
-
What is a Merkle Tree?
- A Merkle Tree is a tree structure where each node stores a hash of its child nodes.
- π Used in Git, Bitcoin, Cassandra to verify data integrity efficiently.
-
How It Works:
- Leaf Nodes: Store data hashes.
- Parent Nodes: Store combined hashes of child nodes.
- Root Hash: Unique fingerprint of all data.
-
Example: Verifying blockchain transactions with Merkle Trees
const crypto = require('crypto'); function hash(data) { return crypto.createHash('sha256').update(data).digest('hex'); } let transactions = ["tx1", "tx2", "tx3", "tx4"].map(hash); let parent = hash(transactions[0] + transactions[1]); console.log("Merkle Root Hash:", parent);
π Activity: Implement a Merkle Tree hash function in Python or JavaScript. Explore how Bitcoin uses Merkle Trees for transaction validation.
-
What is Consistent Hashing?
- Consistent hashing is a load-balancing technique that minimizes reallocation of data when servers are added or removed.
- π Used in DynamoDB, Memcached, Distributed Caching Systems.
-
How It Works:
- Each server is assigned a range on a circular hash ring.
- Keys are mapped to a server using a hash function.
- When a server fails or is added, only a fraction of keys are remapped instead of all.
-
Example: Hashing a Key to a Server
const crypto = require('crypto'); const servers = ["Server-1", "Server-2", "Server-3"]; function hashKey(key) { return crypto.createHash('md5').update(key).digest('hex'); } console.log("Assigned Server:", servers[parseInt(hashKey("user123"), 16) % servers.length]);
π Activity: Implement consistent hashing in a distributed caching system (e.g., Redis, Memcached). Analyze how Amazon DynamoDB uses consistent hashing.
- β Build a leader-follower PostgreSQL database cluster.
- β Implement a distributed cache using consistent hashing (Redis).
- β Use Merkle Trees for efficient data integrity verification.
- β Simulate quorum-based decision-making in Cassandra.
π Congratulations! You've completed Distributed Systems & Fault Tolerance!
π Next: Move to Week 5 - Scalability & Performance Optimization!
This week, you'll dive deep into API design, explore architectural patterns, and implement scalable system designs from basic concepts to hands-on applications.
-
What Makes a Good API?
- A well-designed API is:
- β Consistent β Follows predictable naming conventions.
- β Versioned β Allows backward compatibility.
- β Secure β Implements authentication & rate limiting.
- β Efficient β Uses proper status codes and pagination.
- A well-designed API is:
-
RESTful API Design Guidelines:
- Use nouns in URIs β /users, /orders/{id} β (Avoid verbs like /getUser).
- Use HTTP status codes:
- 200 OK β Success
- 201 Created β Resource created
- 400 Bad Request β Invalid input
- 404 Not Found β Resource missing
- Version your API: /api/v1/users
π Activity: Build a REST API in Node.js with Express. Apply API versioning & proper status codes.
-
Monolithic Architecture
- π’ Pros:
- β Simple to develop & deploy
- β Single codebase & shared memory
- π΄ Cons:
- β Hard to scale
- β A single failure can crash the entire system
- π’ Pros:
-
Microservices Architecture
- π’ Pros:
- β Independent deployment & scaling
- β Easier fault isolation
- π΄ Cons:
- β More complex communication
- β Requires service discovery & orchestration
- π’ Pros:
π Activity: Convert a monolithic Node.js app into microservices using Express & Docker. Implement inter-service communication with REST or gRPC.
-
What is Event-Driven Architecture?
- Event-driven systems react to changes asynchronously.
- π Used in real-time systems (Uber, Netflix, Slack).
-
Key Patterns:
- Pub/Sub Model: A publisher sends events to multiple subscribers.
- Message Queues (Kafka, RabbitMQ): Messages are sent to a queue and processed later.
-
Example: Kafka Pub/Sub in Node.js
const kafka = require('kafka-node'); const producer = new kafka.Producer(new kafka.KafkaClient()); producer.send([{ topic: 'order_created', messages: 'New order placed!' }], () => {});
π Activity: Set up a Pub/Sub model using Redis or Kafka. Implement event-driven order processing.
-
What is Sharding?
- Sharding splits data across multiple databases to improve performance & scalability.
- π Used by Facebook, Instagram, YouTube for large-scale systems.
-
Types of Sharding:
| Type | How It Works | Example |
|---|---|---|
| Key-Based | Uses a hash function to distribute data. | User IDs in a distributed database. |
| Range-Based | Divides data into ranges. | Orders from 2020β2022 in DB1, 2023 in DB2. |
| Directory-Based | Stores a lookup table for shard locations. | Managing large-scale file storage. |
π Activity: Implement MongoDB sharding in a Node.js app. Analyze PostgreSQL partitioning strategies.
-
What is Circuit Breaking?
- A circuit breaker prevents a system from making repeated failed requests to an unhealthy service.
- π Used in Netflix, Amazon, and distributed microservices.
-
States of a Circuit Breaker:
- Closed: Requests are sent normally.
- Open: Requests are blocked after repeated failures.
- Half-Open: A few test requests check if the system recovers.
-
Implementing a Circuit Breaker in Node.js with Opossum:
const CircuitBreaker = require('opossum'); const slowAPI = () => fetch('https://example.com/api'); const breaker = new CircuitBreaker(slowAPI, { timeout: 5000, errorThresholdPercentage: 50 }); breaker.fallback(() => "Service unavailable, try later."); breaker.fire().then(console.log).catch(console.error);
π Activity: Implement a circuit breaker for a microservice using Opossum. Simulate a failing API and analyze recovery behavior.
-
What is Static Content?
- Static files (HTML, CSS, JS, images) don't change dynamically and can be cached for performance.
-
Hosting Options:
- CDN (Cloudflare, AWS S3, Netlify, Vercel): Best for speed & scalability.
- Nginx/Apache: Serve static files from a traditional web server.
- Node.js & Express: Serve files with express.static().
-
Example: Serving Static Files in Express.js
const express = require('express'); const app = express(); app.use(express.static('public')); app.listen(3000, () => console.log('Server running on port 3000'));
π Activity: Deploy a React app on Vercel or Netlify. Optimize images & cache static assets with Nginx.
- β Refactor a monolithic app into microservices.
- β Use Kafka for asynchronous event-driven communication.
- β Implement a circuit breaker in a microservice.
- β Deploy the application on Kubernetes using Docker.
π Congratulations! You've mastered APIs & Architectural Design Patterns!
π Next: Move to Week 6 - Scalability & Performance Optimization!