Skip to content

Commit 1a41047

Browse files
added advanced backend topics
1 parent 95fbe9a commit 1a41047

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# 1. Securing APIs
2+
3+
## a) Authentication
4+
5+
- **Basic Authentication** (username:password) — important for internal APIs.
6+
- **OAuth 2.0** — learn **Authorization Code flow**, **Client Credentials flow**, and **PKCE**.
7+
- **JWT (JSON Web Token)** — token-based authentication.
8+
- **OpenID Connect** — an identity layer on top of OAuth 2.0 (important for login systems).
9+
- **Multi-Factor Authentication (MFA)** — adding OTPs, apps like Google Authenticator.
10+
- **Biometric Authentication** — integrating with FaceID/Fingerprint APIs (optional but good for mobile-first apps).
11+
- **SSO (Single Sign-On)** — via OAuth/SAML (very important for enterprise backend apps).
12+
- **Session Management** — cookie-based authentication vs. token-based sessions.
13+
14+
## b) Authorization
15+
16+
- **RBAC (Role-Based Access Control)** — define roles like Admin, User, Guest.
17+
- **ABAC (Attribute-Based Access Control)** — based on attributes (e.g., department, age).
18+
- **PBAC (Policy-Based Access Control)** — newer — think fine-grained control using policies.
19+
- **Scope Management** — in OAuth 2.0 (limiting what a token can do).
20+
- **API Gateway Authorization** — secure APIs with API Gateways like **AWS API Gateway**, **Kong**, or **Apigee**.
21+
22+
---
23+
24+
# 2. Performance Optimization
25+
26+
## a) Caching strategies
27+
28+
- **Redis caching** — store sessions, API responses, database query results.
29+
- **Memory caching (Node.js)** — simple in-memory cache for ultra-fast reads.
30+
- **CDN caching** — for static assets (Cloudflare, AWS CloudFront).
31+
- **Cache Invalidation** strategies (very important):
32+
- TTL (Time to Live)
33+
- Manual Purging
34+
- Stale-While-Revalidate (used in Next.js)
35+
36+
## b) API Rate Limiting
37+
38+
- **express-rate-limit** library (Node.js).
39+
- **Redis-backed rate limiters** (for distributed systems).
40+
- **Token Bucket** and **Leaky Bucket** algorithms (understand how they work).
41+
- **Global API Gateways** (e.g., AWS API Gateway's throttling).
42+
43+
## c) Query Optimization and Database Indexing
44+
45+
- **Proper database indexing** (single field, composite indexes, partial indexes).
46+
- **Query Optimization Techniques**:
47+
- Avoid SELECT \* (only fetch needed fields).
48+
- Query planning and `EXPLAIN` command for databases.
49+
- Using **pagination** instead of fetching all records.
50+
- **Caching database queries** where necessary.
51+
- **Connection Pooling** — reusing DB connections instead of opening new ones.
52+
- **Database sharding and replication** — advanced for high scale.
53+
54+
---
55+
56+
# 3. Testing APIs and Microservices
57+
58+
## a) Unit Testing
59+
60+
- **Mocha** + **Chai** (most common in Node.js world).
61+
- **Jest** (modern and more powerful for new projects).
62+
- **Sinon** for mocking external services.
63+
64+
## b) Integration and End-to-End Testing
65+
66+
- **Supertest** — for API endpoint testing directly in Node.
67+
- **Postman/Newman** — create full testing collections.
68+
- **Pact** — for **Contract Testing** between microservices.
69+
- **Cypress** — for full-stack E2E tests if your backend interacts heavily with UI.
70+
71+
## c) Test Automation & CI/CD
72+
73+
- **GitHub Actions**, **GitLab CI/CD**, **CircleCI** — run API tests automatically on push.
74+
- **Mock Servers** — simulate 3rd party APIs when testing your API (Postman Mock Server, WireMock).
75+
76+
---
77+
78+
# 4. Deployment and Scaling
79+
80+
## a) Deployment
81+
82+
- **Deploy on AWS EC2**, **AWS Elastic Beanstalk**, **Google App Engine**, **Heroku**, **Render**, or **Vercel**.
83+
- **PM2** — process manager for Node.js to handle auto-restarts and scaling.
84+
- **Environment Variables** handling (dotenv, Vault, AWS Secrets Manager).
85+
86+
## b) Scaling
87+
88+
- **Docker** — containerize your Node.js apps.
89+
- **Kubernetes (K8s)** — orchestrate and scale containers automatically.
90+
- **Service Mesh** (Istio/Linkerd) — advanced networking for microservices.
91+
- **Horizontal Pod Autoscaling** (HPA) — Kubernetes automatically adding/removing pods.
92+
- **Vertical Scaling** (adding more power to a server) vs. **Horizontal Scaling** (adding more servers).
93+
- **Load Balancers** (AWS ALB, Nginx, HAProxy) to distribute traffic.
94+
- **Stateless APIs** — APIs should not store any state locally if you want to scale horizontally.
95+
96+
## c) Serverless
97+
98+
- **AWS Lambda**, **Google Cloud Functions** — event-driven, pay-per-use backend functions.
99+
- **Serverless Framework** — tool for managing serverless apps.
100+
101+
## d) Microservices-specific scaling
102+
103+
- **Distributed Tracing** (Jaeger, Zipkin) — track requests across multiple services.
104+
- **Event-Driven Architecture** — using Kafka, RabbitMQ, or SQS for communication.
105+
106+
---
107+
108+
# Additional must-know related topics
109+
110+
- **API Documentation Standards**
111+
- OpenAPI Specification (Swagger UI)
112+
- Redoc
113+
- **GraphQL APIs** — alternative to REST, learn **Apollo Server** and **Federation**.
114+
- **WebSockets** for real-time APIs (Socket.IO, native WS module).
115+
- **Event Sourcing and CQRS** — design patterns for complex backend systems.
116+
- **Monitoring and Observability**
117+
- Prometheus + Grafana (metrics monitoring)
118+
- ELK Stack (Elasticsearch, Logstash, Kibana) for logging
119+
- **Security Best Practices**
120+
- OWASP Top 10 for APIs
121+
- Helmet.js (HTTP security headers)
122+
- Proper input sanitization (SQL injection, XSS prevention)
123+
- **API Gateway Concepts**
124+
- Kong
125+
- AWS API Gateway
126+
- NGINX as an API Gateway
127+
- **Versioning your APIs**
128+
- URI Versioning (`/api/v1/resource`)
129+
- Header Versioning
130+
- **Message Queues and Pub/Sub systems**
131+
- RabbitMQ
132+
- Apache Kafka
133+
- AWS SNS/SQS
134+
- **Domain-Driven Design (DDD)** — high-level API and microservices design thinking.
135+
- **Resiliency Patterns**
136+
- Circuit Breaker (with libraries like `opossum` in Node.js)
137+
- Retry Logic
138+
- Bulkhead Isolation
139+
140+
---
141+
142+
# Summary Table
143+
144+
| Area | Topics to Learn |
145+
| :--------------------- | :---------------------------------------------------------------- |
146+
| **Security** | OAuth 2.0, JWT, MFA, OpenID Connect, RBAC, ABAC |
147+
| **Performance** | Redis caching, API Rate Limiting, Query Optimization, DB Indexing |
148+
| **Testing** | Unit + Integration + Contract Testing, Automation |
149+
| **Deployment/Scaling** | Docker, Kubernetes, Serverless, Monitoring, Microservices scaling |
150+
| **Modern Skills** | GraphQL, WebSockets, Event-Driven Systems, Resiliency Patterns |
151+
152+
---

redis/node_server/app.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const express = require("express");
2+
const axios = require("axios");
3+
const Redis = require("ioredis");
4+
5+
const app = express();
6+
const redis = new Redis({ host: "localhost", port: 6379 }); // Redis Docker container
7+
const PORT = 3000;
8+
9+
// Example API to cache (we'll use JSONPlaceholder)
10+
const API_URL = "https://jsonplaceholder.typicode.com/posts";
11+
12+
// Cache middleware
13+
async function cacheMiddleware(req, res, next) {
14+
const { id } = req.params;
15+
const cacheKey = `post:${id}`;
16+
17+
try {
18+
// Check Redis for cached data
19+
const cachedData = await redis.get(cacheKey);
20+
if (cachedData) {
21+
console.log("Serving from cache 🚀");
22+
return res.send(JSON.parse(cachedData));
23+
}
24+
next(); // No cache → proceed to fetch
25+
} catch (err) {
26+
console.error("Cache error:", err);
27+
next();
28+
}
29+
}
30+
31+
// Route: Get a post by ID (with caching)
32+
app.get("/posts/:id", cacheMiddleware, async (req, res) => {
33+
const { id } = req.params;
34+
const cacheKey = `post:${id}`;
35+
36+
try {
37+
// Fetch from API
38+
const response = await axios.get(`${API_URL}/${id}`);
39+
const data = response.data;
40+
41+
// Save to Redis (expire after 1 hour = 3600 seconds)
42+
await redis.setex(cacheKey, 600, JSON.stringify(data));
43+
console.log("Serving from API ⚡");
44+
45+
res.send(data);
46+
} catch (err) {
47+
res.status(500).send("Error fetching post");
48+
}
49+
});
50+
51+
app.listen(PORT, () => {
52+
console.log(`Server running on http://localhost:${PORT}`);
53+
});

0 commit comments

Comments
 (0)