System Design
Imagine you build a shopping app. It works well when 10 people use it. But what happens when 10,000 people open it together? What if two people try to buy the last item? What if a server stops working after a customer pays?
System design means planning how your application's parts work together to handle situations like these.
These notes cover all 33 topics from the video in the same eight sections. Each topic starts with a simple explanation, then shows how it works and where you would use it.
How to study: Read the beginner explanation first. When you feel comfortable, open the smaller section marked Advanced. You do not need to understand every advanced detail on your first reading.
The company examples help explain possible designs. They do not claim to show the exact private systems used by Instagram, IRCTC, or a payment company.
Contents
| Section | What you will learn |
|---|---|
| 1. Foundations | What happens when an app opens, HLD, LLD, and scaling |
| 2. Web infrastructure | Load balancers, CDN, proxies, HTTPS, caching, and request flow |
| 3. APIs and communication | API, gateway, REST, GraphQL, gRPC, WebSocket, webhook, SOAP, and SDK |
| 4. Databases and data scaling | Partitioning, sharding, replication, SQL, and NoSQL databases |
| 5. Distributed systems | CAP, ACID, BASE, queues, cache writes, and rate limiting |
| 6. Architecture and code design | Microservices, containers, Kubernetes, and SOLID |
| 7. Security and login | Authentication, authorization, JWT, sessions, OAuth, and SSO |
| 8. Real-world designs | A photo-sharing app, train booking, and UPI |
| Quick revision | Short reminders and a simple interview approach |
| References | Further reading for technical details |
1. Foundations
Before starting, understand these basic words:
| Word | Simple meaning | Example |
|---|---|---|
| Client | The app or browser asking for something | Your browser asks to see your orders |
| Server | A computer or program that handles requests | Your backend finds the orders |
| Database | An organized place to save and find data | Stores users, products, and orders |
| Request | A message asking for an action or information | “Show product 42” |
| Response | The answer to a request | Product name, price, and image link |
| Latency | How long one operation takes | Your feed takes 200 milliseconds to load |
| Throughput | How much work finishes in a period of time | A server handles 1,000 requests per second |
| Scalability | Ability to handle more work by changing capacity | Add servers when traffic grows |
| Availability | Whether people can use the service when needed | The app stays usable after one server fails |
| Reliability | Whether the system keeps doing its job correctly | A confirmed order stays saved |
| Bottleneck | The part slowing down the whole system | The database cannot keep up with requests |
| Distributed system | Several computers working together over a network | Several app servers share the work |
1.1 What happens when you open a website or mobile app?
What does it mean?
You tap an app and see a screen. But behind that screen, your phone and the company's computers are talking to each other.
Let's take a streaming app. You open it because you want to watch a movie.
How does it work?
- Your phone starts the app. The code and data it is using need memory, called RAM.
- The app may immediately show saved content, such as its logo or an old list of movies.
- To get new information, it needs to contact a server.
- It finds the server's address using DNS.
- It makes a secure connection and asks, “Give me the home page information.”
- The server finds the information and sends it back.
- The app displays the movie list. Images and videos may come from separate delivery servers.
An app does not have to become completely useless without internet. Downloads, saved pages, or other offline features may still work.
What is DNS?
DNS means Domain Name System. Think of it like your phone's contact list. You remember a friend's name; your phone finds the number. Similarly, you remember example.com; DNS helps find its IP address.
An IP address is a network address used to reach a destination. IPv4 and IPv6 are two address formats.
Real-world use: Every time your browser needs to connect to a website by name, it needs the right address, often from a saved DNS answer.
Advanced — How DNS finds the address
A recursive resolver finds the answer for your device. It may be operated by your internet provider or a public DNS provider.
| Part | Job |
|---|---|
| Browser or operating-system cache | Checks whether an answer is already saved |
| Resolver | Finds an answer and may save it for later |
| Root name server | Points toward the relevant top-level domain servers |
| TLD server | Handles a domain ending such as .com and points toward the domain's authoritative servers |
| Authoritative server | Provides the domain's records |
When an answer is missing, the resolver follows these directions until it finds it. A TTL, or time to live, says how long an answer can be cached.
There are 13 named root-server identities, but many actual server instances around the world. They are not just 13 physical computers.
IPv4 addresses use 32 bits; IPv6 addresses use 128 bits. You do not need to memorize those sizes to understand the request flow.
Remember: DNS finds an address. It does not fetch your movie or carry your API request.
1.2 HLD vs LLD
HLD means High-Level Design. LLD means Low-Level Design.
Imagine you are building a house.
First, you decide where the kitchen, bedrooms, and bathrooms will go. That is the big plan. Later, you decide how the kitchen cupboards, pipes, and wiring will work. That is the detailed plan.
Software works in a similar way.
| HLD: the big plan | LLD: the detailed plan |
|---|---|
| Which major parts do we need? | How will one part work internally? |
| How will those parts communicate? | Which classes, functions, and rules will we write? |
| Where will data be stored? | What fields and checks will each record need? |
Example: a train-booking app
HLD includes login, train search, seat booking, payment, and notification services.
LLD for booking includes a Booking class, a Seat class, and methods such as reserveSeat() and cancelBooking(). It also defines rules such as “a confirmed seat cannot be sold to another person for the same journey segment.”
Where is it used?
Use HLD to explain an application's overall structure. Use LLD when turning one part of that structure into code.
Advanced — Design patterns mentioned in the video
A design pattern is a reusable way to organize code for a common problem.
| Pattern | Simple meaning | Example |
|---|---|---|
| Factory | A place that creates the right kind of object | Create the selected payment provider's adapter |
| Strategy | Give different ways to perform one job a common interface | Different rules for cancellation fees |
| Singleton | Keep one instance of an object within a particular program context | One shared configuration object in a process |
A Singleton in one server does not control what happens on every other server. It cannot, by itself, prevent double booking across a distributed system.
Remember: HLD describes the whole system. LLD explains its parts in detail.
1.3 Horizontal vs vertical scaling
What is scaling?
Scaling means changing your system's capacity so it can handle more work.
Imagine one cook can prepare 20 meals per hour. Now 100 customers arrive. You have two choices: give that cook better equipment, or bring in more cooks.
| Type | Simple meaning | Server example |
|---|---|---|
| Vertical scaling | Make one machine more powerful | Increase RAM from 8 GB to 32 GB |
| Horizontal scaling | Add more machines or instances | Run the app on five servers instead of one |
When would you use them?
A small application may first need a stronger server. This is often easier to manage.
A busy application may need several servers sharing the requests. A load balancer can send requests to those servers.
What are the problems?
Vertical scaling has a limit. You cannot keep adding power to one machine forever. If it is your only server and it fails, the application may stop.
Horizontal scaling gives you more machines to manage. They must agree about shared information, such as who is logged in and which seats are booked.
Advanced — Stateless servers and shared state
A stateless application instance does not depend on its own private memory to remember everything about a user between requests.
For example, shared sessions can be stored in Redis. Then server A can handle your first request and server B can handle the next one.
Adding application servers does not automatically fix an overloaded database. Also, several servers improve availability only when failed servers can be avoided and the remaining ones have enough capacity.
Horizontal scaling does not require putting machines in different countries. Both scaling methods can be used together.
Remember: Vertical = more power in one machine. Horizontal = more machines.
2. Web infrastructure
2.1 Load balancer
What is it?
A load balancer shares incoming traffic across several servers.
Imagine a restaurant with five waiters. If every customer calls the first waiter, that person becomes overloaded while the others stand around. A manager can send customers to different waiters. A load balancer does a similar job for servers.
How does it work?
- A request reaches the load balancer.
- It chooses a suitable working server.
- It sends the request there.
- The server handles the request and returns a response.
It can also check which servers are healthy and avoid a server that has stopped working.
Real-world use: During a big sale, an online store runs many app instances. The load balancer spreads requests across them.
Simple ways to choose a server
| Method | Meaning |
|---|---|
| Round robin | Take turns: A, B, C, then A again |
| Weighted routing | Give a stronger server a larger share |
| Least connections | Prefer a server with fewer active connections |
Remember: A load balancer distributes capacity. If every server is overloaded, it cannot magically create more capacity.
Advanced — Types, layers, and global routing
| Type | Meaning | Examples |
|---|---|---|
| Hardware | A dedicated appliance | F5 BIG-IP |
| Software | A program running on a machine | NGINX, HAProxy, Envoy |
| Managed cloud | The cloud provider operates the service | AWS, Azure, Google Cloud services |
Layer 4 balancing uses information such as IP addresses, ports, and TCP/UDP connections.
Layer 7 balancing understands application information such as an HTTP path. It could send /payments to one service and /videos to another.
Global routing chooses a region or entry point. Regional balancing chooses a backend inside that region.
Anycast lets multiple locations advertise the same IP address. Network routing chooses a path to one of them. “Nearest” usually means a suitable network route, not necessarily the shortest physical distance.
The load balancer also needs a failure-recovery plan. Otherwise, it can become the one broken component that stops the whole application.
2.2 CDN — Content Delivery Network
What is it?
A CDN keeps copies of content in different locations so users can download it from a suitable nearby location.
Imagine a company has one warehouse far away. Every delivery takes a long time. It opens smaller warehouses near customers and keeps popular items there. Delivery becomes faster.
A CDN does something similar with images, files, and video parts.
How does it work?
- You request an image.
- A CDN edge server checks whether it has a usable copy.
- If it has one, it sends it immediately. This is a cache hit.
- If not, it gets the image from the original source. This is a cache miss.
- It may keep a copy for the next person.
The original source is called the origin. An edge server is a delivery server closer to users in the network.
Real-world use: A course website can use a CDN for thumbnails and lesson videos. Its main server does not have to send every video byte to every student.
What is the catch?
A saved copy can become old. If you change an image, you need rules for replacing or expiring old copies. Private content also needs access control.
Advanced — A simple speed calculation
Suppose a cache hit takes 20 milliseconds and a miss takes 2,500 milliseconds. If 80% of requests are hits:
Average = 0.8 × 20 + 0.2 × 2,500 = 516 milliseconds per request
These are example numbers. Actual speed depends on the network and system.
TTL controls how long a copy stays valid. Invalidation removes or refreshes an old copy. A new asset filename can also avoid using an older version.
CDNs can cache some dynamic responses too, when their rules allow it. But you must not accidentally share one person's private response with another person.
Keep the durable original media in suitable storage. Do not assume the CDN is your only permanent copy.
Remember: A CDN reduces the distance and repeated work needed to deliver content.
2.3 Forward proxy vs reverse proxy
What is a proxy?
A proxy is a middleman that receives a request and passes it along.
The important question is: whose side is it working for?
| Type | Works on behalf of | Simple example |
|---|---|---|
| Forward proxy | The client | A college checks outgoing website requests |
| Reverse proxy | The backend servers | A website receives requests through NGINX before they reach Express |
Forward proxy example
Your college wants to block access to certain websites. Your request goes through its proxy. The proxy checks the rules and either forwards the request or blocks it.
Reverse proxy example
Your app has three backend servers. Users contact one public address. The reverse proxy receives requests and forwards them to the right backend.
A reverse proxy can handle routing, load balancing, caching, HTTPS connections, and compression.
Where are they used?
Forward proxies are useful for controlling outbound traffic. Reverse proxies are useful for managing incoming traffic to applications.
Advanced — Security and TLS termination
TLS termination means the proxy handles the client's encrypted connection. It can then make another encrypted connection to the backend.
A private network is not automatically safe enough to send everything without encryption.
Proxies do not guarantee anonymity. Accounts, cookies, and other information may still identify a user. A VPN tunnels network traffic; it is not simply another name for an HTTP proxy.
A reverse proxy may apply security rules, but proxying alone does not stop SQL injection or all denial-of-service attacks.
Compression reduces the size of transferred data. It does not choose which response fields a user needs.
Remember: Forward proxy represents clients. Reverse proxy represents servers.
2.4 SSL certificates, HTTPS, and TLS
What problem do they solve?
Suppose you send a password across a network. You do not want someone watching that network to read it or secretly change the message.
HTTPS protects HTTP communication using TLS. People still commonly say “SSL certificate,” but modern connections use TLS.
What is a certificate?
Think of it as a checked identity document for a domain and its public key. A trusted organization called a Certificate Authority, or CA, signs it. Examples include Let's Encrypt and DigiCert.
How does it work, simply?
- Your browser connects to the website.
- The website presents its certificate and proves it controls the required private key.
- The browser checks whether the certificate is valid for that domain and trusted.
- The two sides establish keys used to protect their messages.
- Your request and response travel with encryption and protection against changes.
Real-world use: Login pages, online shopping, and private APIs.
Remember: HTTPS protects the connection. It does not prove that the business is honest or that its application has no bugs.
Advanced — The basic cryptography words
| Term | Simple meaning |
|---|---|
| Symmetric encryption | Both sides use shared secret key material to protect data |
| Public/private key pair | Related keys used in asymmetric cryptography; the private key must stay secret |
| Hash | A digest calculated from data |
| Digital signature | A check that can prove who signed data and detect unauthorized changes |
| Certificate chain | A path of certificate signatures leading to a trusted authority |
The browser checks things such as the domain name, validity period, and certificate chain. Modern TLS uses efficient symmetric encryption for application traffic after its setup process.
Do not memorize “signing means encrypting with a private key” as a universal rule. Signing and encryption are different cryptographic operations.
2.5 Caching
What is it?
Caching means keeping a useful copy so you do not have to repeat expensive work every time.
Imagine you cook often. You keep salt near the stove instead of walking to a distant storeroom every time. The storeroom still holds your main supplies; the nearby salt saves time.
Example: product details
- A customer asks for product 42.
- The app checks the cache.
- If the product is there, the app returns it quickly.
- If it is missing, the app reads the database.
- The app saves a copy in the cache for later requests.
A cache hit means the data was found. A cache miss means it was not found.
Where can caching happen?
| Place | Example |
|---|---|
| Browser or phone | Downloaded images and saved content |
| DNS resolver | Previously found domain addresses |
| CDN | Images and video parts |
| Proxy or gateway | Suitable pages or API responses |
| Application/Redis | Product details, feed data, or session records |
| Database | Frequently accessed data and index pages |
Real-world use: Thousands of people open the same product page. A cache helps avoid asking the database the same question thousands of times.
What can go wrong?
The cached price might be old. Before accepting an order, check important facts such as price and stock against the correct authoritative data.
Advanced — Expiry, invalidation, and cache overload
TTL: How long a cached item can remain valid.
Invalidation: Removing or updating a copy when the original changes.
Eviction: Removing entries to make room when the cache is full.
Cache-aside: The application checks the cache, reads the database on a miss, and fills the cache.
Cache stampede: Many requests miss together and all hit the database. You can combine identical in-progress reads or spread expiry times to reduce this burst.
If Redis goes down, allowing every request to hit the database immediately may overload it. Plan a limit on fallback traffic.
Redis can use persistence. Also, databases already cache data in memory. The speed of reading RAM is not the same as the complete time for a network request to Redis.
Remember: A cache makes repeated work cheaper, but you must decide how fresh the answer needs to be.
2.6 Complete request flow: putting the pieces together
Let's use a restaurant-booking app.
You click Book a table. What happens behind the scenes?
| Step | Component | Its job |
|---|---|---|
| 1 | Client | Sends your request |
| 2 | DNS | Helps find the destination address if needed |
| 3 | CDN | Can serve saved images and other suitable content |
| 4 | Load balancer | Chooses an application server |
| 5 | API gateway | Checks entry rules and routes the request |
| 6 | Booking service | Checks the booking rules |
| 7 | Cache/database | Supplies data; the database records the reservation |
| 8 | Response | Tells your app whether the booking succeeded |
| 9 | Background worker | Sends a confirmation email |
These jobs may be combined in some systems. Every application does not need a separate product for every row.
The important detail: A menu image may come directly from the CDN. A new booking needs the booking service to safely change stored data. Different requests can follow different paths.
Where is this useful?
When you design an app, follow one request from start to finish. It helps you see which part is doing each job.
Advanced — Monitoring, logs, traces, and auditing
| Term | What it tells you |
|---|---|
| Metrics | How much traffic, how many errors, and how long requests take |
| Logs | What events happened |
| Traces | Which services one request passed through and where time was spent |
| Audit records | Who changed important information and when |
Customer feedback helps improve the product, but it cannot replace technical monitoring. You want to know that booking is failing before every customer has to complain.
DNS is an address-lookup step. The HTTP request does not travel through the DNS server to reach the application.
Remember: Each component should have a clear job. Do not add tools just to make the architecture look impressive.
3. APIs and communication
3.1 What is an API? What is an API gateway?
API means Application Programming Interface.
An API defines how one piece of software can ask another piece to do something.
Think about a restaurant menu. It tells you what you can order. You do not need to know exactly how the kitchen prepares every dish. Similarly, an API tells your frontend what operations the backend offers and what information to send.
Example
GET /products/42
This asks the backend for product 42. The response might be:
{
"id": 42,
"name": "Keyboard",
"price": 2500
}
The backend still does the actual work: checking rules, reading data, and returning the result.
What is an API gateway?
Imagine a receptionist at the entrance. You explain what you need, and the receptionist directs you to the right department.
A gateway receives API requests and sends them to the right service. It can also check login tokens, limit requests, and record traffic.
Real-world use: In a shopping app, /products goes to the catalog service and /orders goes to the order service.
Advanced — API vs gateway vs load balancer
| Term | Main purpose |
|---|---|
| API | Defines how software interacts |
| Gateway | Manages entry rules and routes API requests |
| Load balancer | Shares traffic among backend instances |
One product can perform more than one of these jobs.
APIs are not limited to the internet. A local library can expose an API too. A gateway does not remove the need for backend authorization: a service still needs to check whether a user may access a specific resource.
Remember: An API is the agreed way to interact. A gateway is a component that handles incoming API traffic.
3.2 REST, GraphQL, gRPC, WebSockets, webhooks, and SOAP
These are different ways to organize communication. You do not have to choose one for your entire application.
REST: ask for resources through endpoints
A resource can be a product, user, or order. You use an address and an HTTP method to say what you want.
| Method | Simple meaning | Example |
|---|---|---|
| GET | Read something | Show product 42 |
| POST | Create something or request an action | Place an order |
| PUT | Replace the target's representation | Replace profile details |
| PATCH | Change part of something | Change only the profile name |
| DELETE | Remove something | Remove a cart item |
Use it for: Ordinary web/mobile APIs such as products, orders, and profiles.
GraphQL: ask for the fields you want
Suppose a screen only needs a user's name and recent orders. You can describe those fields in one query:
query {
user(id: "7") {
name
recentOrders {
id
status
}
}
}
The backend returns those requested fields. This can reduce extra data and client round trips.
Use it for: Screens that need different combinations of related data.
gRPC: call a method on another service
Imagine your order service asks the inventory service:
CheckStock(productId = 42)
It feels like calling a function, but the function runs in another service. gRPC uses a defined contract so both sides agree on the request and response.
Use it for: Communication between backend services.
WebSocket: keep a two-way conversation open
With normal repeated requests, a delivery app might keep asking, “Where is my driver now?” With a WebSocket connection, the server can send a location update when it has one. The client can also send messages back.
Use it for: Chat, auction bids, driver-location updates, and live scores.
Webhook: tell me when an event happens
Your payment provider should not need you to ask every second, “Has this payment finished?” You give it a backend URL. When the payment event happens, it sends a request to that URL.
Use it for: Payment updates, shipping updates, or notifications from an external service.
SOAP: exchange messages in a strict XML format
SOAP messages use a defined structure with an envelope and body. It is like filling out a formal form that both sides agree to understand.
Use it for: Integrating with systems that already require SOAP, including some enterprise and banking systems.
| Need | A possible choice |
|---|---|
| Fetch products and create orders | REST |
| Request selected fields across related data | GraphQL |
| Call internal service methods | gRPC |
| Continuous two-way updates | WebSocket |
| Receive an external event | Webhook |
| Work with an existing strict XML integration | SOAP |
Advanced — Important differences and common mistakes
- REST is an architectural style. It is not restricted to JSON. HTTP connections can be reused; each REST request does not require a new connection.
- GraphQL uses a schema describing its data. Functions called resolvers fetch the requested fields. One client query may still cause many backend queries, so control expensive requests.
- REST can also offer selected fields or a combined endpoint. GraphQL is not automatically faster.
- gRPC commonly uses Protocol Buffers, a compact binary format, over HTTP/2. Actual performance depends on the workload.
- WebSockets need reconnect handling and a way to recover missed updates. The classic HTTP/1.1 upgrade response is
101 Switching Protocols. - A webhook receiver must verify the provider's signature and handle retries. A duplicate event must not create a duplicate payment effect.
- SOAP can use features such as WS-Security. XML alone does not make it secure.
A signature helps verify an event's origin and detect changes. An ordinary hash that anyone can calculate is not enough to prove a payment event is genuine.
Remember: Pick communication based on the job. One app can use several approaches together.
3.3 API vs SDK
SDK means Software Development Kit.
An API tells you how to interact with a system. An SDK gives you ready-made tools that make that interaction easier.
Imagine you want to assemble furniture. The instructions tell you what to do. A toolkit gives you the tools to do it more easily.
Example: adding a payment provider
Without its SDK, you might build the HTTP request, send it, read the response, and handle errors yourself.
With its SDK, you may call a helper like:
paymentClient.createPayment(details)
The SDK handles some of the request-building work for you.
| API | SDK |
|---|---|
| Defines available operations | Provides code and tools for using a platform |
| Can be called directly | May wrap API calls in convenient functions |
| Example: payment endpoint | Example: payment client library and checkout helpers |
Real-world use: Maps, payments, social login, and AI integrations.
Advanced — What to check before using an SDK
Check its error handling, retry behavior, supported features, and version changes. A retry can be important when an operation might charge money twice.
Keep server-side API secrets on the backend. An SDK does not make a secret safe to place in browser code.
Remember: API = the interface. SDK = tools that help you use the platform.
4. Databases and data scaling
4.1 Database partitioning
What is it?
Partitioning means splitting a large dataset into smaller parts.
Imagine a library puts every book in one giant pile. Finding a book is difficult. It divides books into sections: science, history, and literature. Now you can go to the relevant section.
Database example
Your orders table has years of orders. You divide it by year:
| Partition | Data inside |
|---|---|
| Orders 2024 | Orders placed during 2024 |
| Orders 2025 | Orders placed during 2025 |
| Orders 2026 | Orders placed during 2026 |
A query for 2025 may only need the 2025 partition.
Horizontal vs vertical partitioning
| Type | Meaning | Example |
|---|---|---|
| Horizontal | Divide rows into groups | Old orders in one part, new orders in another |
| Vertical | Divide columns into groups | Basic user details separate from a large biography |
With vertical partitioning, keep a shared ID so the pieces can be linked again.
Real-world use: A large orders or event table becomes easier to search, archive, and maintain.
Advanced — Range, list, hash, and hybrid partitioning
| Method | How it groups rows |
|---|---|
| Range | By intervals, such as one month per partition |
| List | By selected values, such as country names |
| Hash | Apply a function to a key to choose a partition |
| Hybrid | Combine approaches |
A simple teaching example is user_id % 4. The remainder chooses one of four buckets:
| User ID | Remainder | Partition |
|---|---|---|
| 12 | 0 | 0 |
| 13 | 1 | 1 |
| 26 | 2 | 2 |
Partition pruning means skipping partitions that cannot contain the requested data. This only helps when the query and database can use the partition rule.
Partitioning does not replace indexes. An index is an extra lookup structure that helps find rows without scanning everything. SQL queries do not always read every row.
Remember: Partitioning organizes data into smaller parts. It does not automatically make every query faster.
4.2 Partitioning vs sharding
What is sharding?
Sharding means putting different parts of a dataset on different database nodes.
Use the restaurant example:
- Local partitioning is like making separate counters inside the same restaurant.
- Sharding is like opening several branches and giving each branch part of the work.
| Local partitioning | Sharding |
|---|---|
| Divide data within one database instance | Divide data across different nodes |
| Helps organization and some queries | Helps spread storage and processing load |
| Still uses that instance's capacity | Uses capacity from multiple machines |
How does the app know which shard to use?
It follows a rule or checks a mapping.
For example, with shard = user_id % 3, user 10 belongs to shard 1. When reading that user's data, the app applies the same rule and goes to shard 1.
Real-world use: A messaging application has too much data for one database node. It distributes conversations or users across nodes.
Advanced — Shard keys and difficult queries
The shard key is the value used to decide where data belongs.
A good key spreads load and keeps commonly accessed data together. A poor key can put too much work on one shard. That overloaded shard is often called a hot shard.
If data is divided by user ID, a query for “all users living in Kathmandu” may need to ask many shards and combine the results.
Adding a shard changes simple modulo routing. Many existing records may need to move. That is why real systems need a migration and rebalancing plan.
Sharding is a form of horizontal partitioning across nodes. Local partitioning is not another name for vertical scaling.
Remember: Sharding divides data across machines; you still need a reliable way to locate each piece.
4.3 Replication vs sharding
Replication means making copies. Sharding means dividing data.
Imagine your notes are stored in one notebook.
Replication is making copies of the notebook. Sharding is putting chapters 1–3 in one notebook and chapters 4–6 in another.
| Replication | Sharding |
|---|---|
| Several copies of the same data | Different pieces of the data |
| Helps recovery and may spread reads | Helps spread storage and processing |
| Changes must reach the copies | Requests must find the right piece |
How does replication work?
In a common setup, one database is the primary. Writes go there. Other databases, called replicas, copy its changes. Suitable read requests can go to replicas.
Real-world use: A shop's product catalog gets many reads. Read replicas can help serve them. If one database node fails, another copy may also help recovery when failover is properly configured.
Can we use both? Yes. Each shard can have its own replicas.
Advanced — Synchronous and asynchronous replication
Synchronous: The write waits for the required copies to acknowledge it. This can improve durability guarantees but takes extra coordination.
Asynchronous: The primary can finish before replicas catch up. This can be faster, but a replica may temporarily show old data. That delay is called replication lag.
Some failure scenarios can lose recent writes that had not reached another copy. Choose the guarantees carefully.
Replication is not a complete backup plan. If someone accidentally deletes data, the deletion may be copied too.
Remember: Copies help keep data available. Shards help divide a large workload. Neither solves every problem alone.
4.4 SQL vs NoSQL
What are they?
A relational database organizes data into tables with relationships. SQL is the language commonly used to query such databases.
NoSQL is a broad name for several other data models, such as documents, key-value pairs, wide-column records, and graphs.
Do not start by asking, “Which one is always faster?” Ask, “What data do I have, and what do I need to do with it?”
Example: an online shop
| Data/job | A possible choice | Why |
|---|---|---|
| Orders and payment records | PostgreSQL or MySQL | Related records and transaction rules |
| Products with different attributes | Document database or relational JSON fields | Flexible product details |
| Frequently read product copies | Redis | Quick access by a known key |
| Product images | Object storage plus CDN | Store and deliver large files |
When might relational storage fit well?
When relationships, constraints, transactions, and flexible queries matter. For example, connect customers to orders and order items.
When might a NoSQL model fit well?
When its particular model matches your needs. For example, key-value access for sessions or graph traversal for complex relationships.
Advanced — Avoid these shortcuts
- SQL does not mean “cannot scale.”
- NoSQL does not mean “no transactions.”
- Flexible product data does not force you to choose MongoDB.
- Social media data does not force you to avoid relational databases.
Check the actual database's guarantees and your read/write patterns. Using three databases means maintaining three systems and keeping related information synchronized. Start with the simplest combination that meets the requirements.
Remember: Choose based on the job, not the SQL/NoSQL label alone.
4.5 Document database
What is it?
A document database stores a record as a document containing named fields, lists, and nested information.
Think of one product's information as a form. A TV form needs screen size. A shoe form needs shoe size. They do not have to contain exactly the same fields.
Example documents
{
"id": "tv-1",
"name": "Smart TV",
"screenSize": 55,
"resolution": "4K"
}
{
"id": "shoe-1",
"name": "Running Shoe",
"size": 7,
"color": "black"
}
How does it work?
You store and query these documents. Indexes help find documents by useful fields. MongoDB and CouchDB are examples of document databases.
Real-world use: Product catalogs, profiles, and content-management systems where records can have different details.
Where do images go?
Usually, keep the image file in object storage and save its key or reference in the document. A CDN can deliver it to users.
Advanced — Embed or reference?
Embed means store related information inside the same document. This is convenient when the information is small and usually read together.
Reference means store another record's ID and fetch it separately. This helps when related data is large, grows without a clear limit, or changes independently.
Product variants might be embedded when there are only a few. A very large, independently changing set may be better kept separately.
MongoDB uses BSON, a binary document format, and has a 16 MiB BSON document limit. Do not place the whole product catalog in one giant document.
Flexible documents still need validation. JSON is usable across programming languages, not only JavaScript.
Remember: A document keeps related fields together while allowing different records to have different shapes.
4.6 Key-value database
What is it?
A key-value database saves a value under a key. You use the key to get the value back.
Think of a locker room. Your locker number is the key. The things inside are the value.
| Key | Value |
|---|---|
cart:user-7 | Items in user 7's cart |
session:abc123 | Information about that login session |
product:42 | Saved details for product 42 |
How does it work?
Save: SET cart:user-7 = [keyboard, mouse]
Read: GET cart:user-7
This is a simple illustration, not complete command syntax for every product.
Real-world use: Carts, sessions, cached data, and request counters. Redis is a common example; DynamoDB also supports key-value access patterns.
What is the limitation?
It is convenient when you know the key. A question like “find every customer who bought a laptop last month and calculate average spending” may need a more suitable query model or extra indexes.
Advanced — How lookup works
A hash table is one way to locate values quickly, but not every key-value database uses the same internal structure.
Updating an existing key often replaces or changes its value. It does not necessarily produce a duplicate-key error.
Relational databases can also perform fast indexed lookups. Do not compare a key lookup against an unnecessarily unindexed full-table scan and conclude SQL is always slow.
Remember: Know the key, find the value.
4.7 Column-family / wide-column database
What is it?
A wide-column database organizes records around keys and columns and is designed to handle large datasets across machines. Cassandra, HBase, and Bigtable are examples, though they differ in their details.
This is one of the harder topics, so start with the use case.
Example: temperature sensors
Suppose thousands of devices send temperature readings all day. You usually ask, “Show this device's readings for this day.”
You can group records by device and day:
| Group | Readings inside |
|---|---|
| Device A, Monday | Monday's readings for A |
| Device A, Tuesday | Tuesday's readings for A |
| Device B, Monday | Monday's readings for B |
The design helps the database go to the relevant group instead of treating every reading as part of one undivided pile.
Real-world use: Large event, sensor, and time-series workloads with known access patterns.
Important clarification: The script mixes wide-column databases with analytical columnar storage. Those are different ideas.
Advanced — Wide-column vs analytical columnar storage
| Model | Main idea |
|---|---|
| Wide-column | Organize distributed records around useful keys and groups |
| Analytical columnar | Store a column's values together so analytical queries can scan selected columns efficiently |
For example, “average salary across all employees” may benefit from analytical columnar storage because it mainly needs the salary column. That does not mean Cassandra simply stores each table column on a separate server.
OLTP means routine operational work, such as inserting an order. OLAP means analytical work, such as calculating monthly sales trends.
In Cassandra, a partition key groups and locates data. Clustering columns order records inside that group. Tables are usually designed for specific queries, and ordinary CQL queries do not support joins.
Avoid groups that grow without a limit or receive nearly all the traffic. These databases can handle heavy writes; they are not inherently bad at inserting records.
Remember: Start with the queries you need, then choose keys and groups that support them.
4.8 Graph database
What is it?
A graph database focuses on things and the connections between them.
A thing is called a node. A connection is called an edge or relationship.
| Node | Relationship | Node |
|---|---|---|
| Sandeep | is friends with | Priya |
| Priya | works at | Company A |
| Sandeep | lives in | Kathmandu |
How does it work?
The database follows relationships to answer a question. For example: “Which of my friends work at Company A?”
Following connections is called a traversal.
Real-world uses
- Suggest mutual friends or professional connections.
- Find films related to films a user liked.
- Find suspicious groups of accounts sharing devices or payment details.
- Connect people, places, and facts in a knowledge graph.
Neo4j, Amazon Neptune, and TigerGraph are examples.
Advanced — Why not just use tables?
You can represent relationships in relational tables too. A graph database can be more natural for deep or changing relationship paths, but it is not automatically faster for every query.
A node with millions of connections can still require expensive work. Indexes and query design matter.
A product displaying mutual friends does not prove that it uses a particular graph database internally.
Remember: Graph databases are especially useful when relationships are the main part of the question.
5. Distributed systems
5.1 CAP theorem
What problem is CAP talking about?
Imagine you have two database servers that normally share updates. Suddenly, the network between them breaks. Both servers are running, but they cannot talk to each other.
Now one server changes a value. What should the other server do when someone asks for that value?
That is the kind of problem CAP helps explain.
| Letter | Simple meaning |
|---|---|
| C — Consistency | Give answers that behave like one up-to-date copy of the data |
| A — Availability | Keep completing requests to working nodes |
| P — Partition tolerance | Handle the possibility that parts of the network cannot communicate |
A network partition is this communication failure. It is different from splitting a database table into partitions.
The difficult choice
During the network break, the second server cannot know the latest change.
- It can continue answering, but its answer may be old.
- Or it can wait/refuse an operation that it cannot safely complete with the required consistency.
It cannot guarantee both properties for every operation while the communication problem lasts.
Real-world examples
For a public like count, showing a slightly old value may be acceptable.
For selling the last seat, accepting two conflicting bookings is not acceptable. It can be better to reject an attempt than confirm a seat you cannot safely allocate.
Advanced — What CAP does and does not say
“Choose any two forever” is an oversimplification. The unavoidable choice between C and A appears during a network partition. When communication is working, a system may provide both.
CAP consistency is commonly described as linearizability: operations appear to happen against one copy in an order that respects real time.
Returning an error is a response in everyday language, but it does not automatically count as completing the requested operation for CAP availability.
One application can make different choices for different operations. Do not label an entire bank or social network with one simple CAP category without describing the operation and failure conditions.
Remember: If servers cannot communicate, you may have to choose between waiting for a safe answer and continuing with weaker consistency.
5.2 ACID vs BASE
First, what is a transaction?
A transaction is a group of database operations treated as one unit of work.
Suppose you move 50 from account A to account B inside one database. You want both changes to happen correctly: subtract 50 from A and add 50 to B.
ACID describes useful transaction guarantees.
| Letter | Simple explanation | Example |
|---|---|---|
| A — Atomicity | Complete the whole transaction or none of it | Do not subtract money without completing the matching credit |
| C — Consistency | Follow the defined data rules | Do not create an invalid account state |
| I — Isolation | Control how simultaneous transactions affect each other | Two booking attempts cannot both claim one seat |
| D — Durability | Keep a committed result through the failures the storage is designed to survive | A saved booking remains after restart |
Simple number example: A has 100 and B has 200. Transfer 50. A becomes 50; B becomes 250. The total stays 300 when there are no fees.
What is BASE?
BASE describes an approach where some data can catch up later while the service remains useful.
| Part | Simple explanation |
|---|---|
| Basically Available | Try to keep useful service available |
| Soft State | Data can change as background updates arrive |
| Eventually Consistent | Copies should agree after updates stop and successful synchronization completes |
Example: You like a post. Your friend's screen may take a moment to show the new total. A short delay can be acceptable for that count.
Advanced — Important distinctions
ACID and BASE are not strict labels that separate every SQL database from every NoSQL database. A distributed or NoSQL database can offer transactions too.
ACID consistency is about preserving data rules. CAP consistency is about the ordering and freshness users observe across a distributed system.
Isolation levels offer different guarantees about concurrent transactions. Saying “the database has ACID” does not remove the need to choose correct transaction logic.
Eventual consistency does not mean “the data can be wrong forever.” The system must have a way to apply updates and resolve conflicts.
A transfer across independent banks is more complicated than one local database transaction.
Remember: ACID protects a transaction's correctness. BASE allows suitable parts of a system to catch up over time.
5.3 Message queues
What is a message queue?
A message queue holds work so another program can process it later.
Imagine a restaurant order slip. The waiter gives your order to the kitchen, and the kitchen prepares it when a cook is ready. The waiter does not need to stand still until the entire meal is finished.
Example: sending an order email
- Your backend safely saves an order.
- It creates a task: “Send confirmation for order 42.”
- The queue holds that task.
- A worker picks it up and sends the email.
- If sending fails, the system can try again according to its rules.
| Word | Simple meaning |
|---|---|
| Producer | The service that creates the message/task |
| Queue/broker | The system holding or routing it |
| Consumer/worker | The program doing the work |
| Acknowledgment | A signal that processing has reached the required point |
| Retry | Another attempt after a failure |
Synchronous: “I ask and wait for your answer before continuing this operation.”
Asynchronous: “I arrange the work, and completion happens separately.”
Real-world use: Emails, notifications, image processing, and background order tasks. The user does not have to wait for every supporting job.
Important: “Your request is queued” does not always mean “your purchase is confirmed.” Only claim the result that has actually been safely recorded.
Advanced — Durability, duplicates, and ordering
A queue is not automatically permanent or strictly first-in-first-out under all conditions. Check persistence, replication, acknowledgments, retention, and ordering guarantees.
A worker may receive a message again after a failure. Make the operation idempotent: repeating the same request should not repeat its business effect. For example, the same payment event must not create a second charge.
A dead-letter queue holds repeatedly failing messages so they can be investigated.
A queue absorbs temporary bursts. If work keeps arriving faster than workers can process it, the backlog keeps growing.
| Product | Basic idea |
|---|---|
| RabbitMQ | A message broker that routes messages to consumers |
| Amazon SQS | A managed queue service |
| Kafka | An event log that consumers read using positions called offsets |
Kafka does not normally delete a record as soon as one consumer reads it.
A transactional outbox saves a business change and a pending event together in one database transaction. Another process publishes the event later. This helps avoid saving an order but losing its event during a crash.
Remember: Queues let work happen separately in time. They still need correct failure handling.
5.4 Write-through vs write-back caching
What problem do these solve?
You have a value in a cache and a saved copy in a backing store, such as a database. If you change the cached value, when should the backing store change?
Suppose both places contain x = 100. Now you want x = 200.
| Method | What happens |
|---|---|
| Write-through | Complete the required backing-store update as part of the write before saying it is done |
| Write-back / write-behind | Update the cache first, then send the update to the backing store later |
Simple analogy
Write-through is like updating both your working note and official register before saying, “Done.”
Write-back is like updating your working note immediately and planning to update the official register later.
What is the trade-off?
Write-through includes the backing-store work, so the write may take longer.
Write-back can respond sooner, but you must protect updates waiting to be saved. If the only copy disappears before it is written back, you can lose the change.
Real-world use: Some noncritical counters can be collected and written later. Important acknowledged data needs a stronger durability plan.
Advanced — Dirty bits and different cache layers
A dirty bit in hardware caching marks data that changed in cache and needs to be written to its backing memory.
A software system might track pending updates using a queue or log instead of a literal bit.
The video uses both CPU-cache/RAM and application-cache/database examples. These are different layers. Updating RAM does not automatically save data to durable disk.
Write-through does not, by itself, solve every consistency problem. Another writer, a partial failure, or another cache may still create differences.
Cache-aside explains how reads fill the cache. Write-through and write-back explain how writes reach the backing store.
Remember: Write-through waits for the backing write. Write-back delays it and must safely track unfinished work.
5.5 Rate limiting and its algorithms
What is rate limiting?
Rate limiting controls how much work a client may request over time.
Imagine a ticket counter says, “One person can make only five booking attempts per minute.” This prevents one person from taking all the counter's attention.
Why use it?
To protect server capacity, reduce repeated login guesses, control API costs, and give users a fair share.
An HTTP API often returns 429 Too Many Requests when a limit is reached.
1. Fixed window counter
Divide time into fixed periods. Allow ten requests in each minute.
It is simple, but ten requests at the end of one minute and ten at the start of the next can create a burst of twenty close together.
2. Sliding window log
For every new request, look backward over the last 60 seconds and count requests in that period.
The window moves with time. It does not simply reset at the start of each clock minute.
3. Sliding window counter
Estimate recent usage using the current window's count and part of the previous window's count. It uses less memory than storing every request time, but the result is an estimate.
4. Token bucket
Imagine a bucket holding ten permission tokens. Each request uses one token. New tokens are added at a fixed rate, up to the bucket's capacity.
If the bucket is full, a short burst is allowed. When empty, requests must wait or be rejected according to the policy.
5. Leaky bucket
Imagine requests waiting in a bucket with a small outlet. They leave at a steady rate. If the bucket is full, extra arrivals are rejected.
This smooths bursts but can add waiting time.
| Algorithm | Best thing about it | Main limitation |
|---|---|---|
| Fixed window | Simple | Bursts near window boundaries |
| Sliding log | Accurate recent-request count | Stores many request times |
| Sliding counter | Smaller memory use | Approximate count |
| Token bucket | Allows controlled bursts | Bursts still need capacity |
| Leaky bucket | Steady release of work | Queued requests wait |
Real-world uses: Login attempts, paid API quotas, booking attempts, and limiting calls to another company's API.
Advanced — Shared limits and sliding-counter calculation
Suppose the previous minute had 80 requests. The current minute has 30, and 15 of its 60 seconds have passed.
Estimated usage = 30 + 80 × (45 / 60) = 90
The previous window contributes only its overlapping portion. This is not carrying unused request credits forward.
If three app servers each allow 100 requests independently, a user could get 300 requests overall. A shared atomic counter or a planned quota split can enforce a total limit.
Atomic means the check and update cannot be interrupted in a way that lets competing requests bypass the rule.
Enforce protection at appropriate points: CDN/WAF, gateway, and application. Client-side limits can be bypassed. IP-only rules can also affect many legitimate users who share an address.
Rate limiting is one protection. It does not stop every network-level denial-of-service attack. Also, allowing requests at a steady rate does not necessarily mean only one request executes at a time.
Remember: Choose whether you want to count recent usage, allow bursts, or smooth incoming work.
6. Architecture and code design
6.1 Microservices architecture
What is it?
Microservices means dividing an application into services that can be developed and deployed independently around clear business jobs.
Imagine a large restaurant. One team handles orders, one handles cooking, and one handles delivery. Each team has a clear responsibility, but they still need to communicate.
Example: grocery app
| Service | Job |
|---|---|
| User | Accounts and user information |
| Catalog/search | Show and find products |
| Inventory | Track stock |
| Cart | Remember intended purchases |
| Pricing | Calculate prices and discounts |
| Orders | Manage order progress |
| Payments | Handle payment integration and status |
| Stores | Manage fulfillment locations |
| Delivery/location | Assign and track deliveries |
| Notifications | Send messages |
| Reviews/support | Handle feedback and help requests |
| Analytics | Study usage and business activity |
A dark store is a location used to fulfill online orders rather than an ordinary walk-in shop.
What is a monolith?
A monolith is one deployable application. It can still have well-organized modules and can run on several servers.
| Monolith | Microservices |
|---|---|
| Often simpler to build and operate initially | Services can change and scale independently |
| Modules call each other inside one application | Services communicate over a network |
| Fewer moving parts | More deployment and failure-handling work |
When should you use microservices?
When independent teams, deployment needs, or different scaling requirements justify the added complexity. A small project does not become better just because it has many services.
Containers and Kubernetes, simply
A container packages your application and its dependencies so it runs in a predictable environment.
Kubernetes helps manage containerized applications: where they run, how they restart, and how the number of running copies changes.
Advanced — Communication and data ownership
Services can use REST/gRPC when an answer is needed now and events/queues when work can happen separately.
A service should control its own data boundary. That does not always require a separate physical database machine for every service from day one.
Network calls can time out or fail. Add timeouts, safe retries, monitoring, and clear recovery rules. Changing several services' data together is harder than changing several tables inside one database transaction.
Containers share the host operating-system kernel; they are not full virtual machines. Kubernetes needs configuration, resources, and operational knowledge. It cannot repair incorrect application logic automatically.
A gateway such as Kong can route requests and enforce entry policies, but it should not become the place where all business logic is mixed together.
Remember: Split services for a clear reason. A well-organized monolith is often a strong starting point.
6.2 SOLID principles, coupling, and cohesion
What is SOLID?
SOLID is a set of five principles for organizing object-oriented code so changes are easier to manage.
Imagine you replace a kitchen tap. You should not have to rebuild every wall in the house. Good code design tries to make changes similarly contained.
S — Single Responsibility
Give a class one clear, related job.
Bad example: one class registers users, formats emails, writes audit files, and generates reports.
Better: registration handles registration, an email component handles emails, and an audit component handles audit records.
O — Open/Closed
Make expected new behavior easy to add without repeatedly changing stable code.
Example: different payment providers implement a common payment interface. Adding another provider does not require editing payment conditions all over the app.
L — Liskov Substitution
If a class claims to be a kind of another class, it should fulfill the behavior callers expect.
Example: if Bird promises fly(), a penguin cannot safely fit that promise. Put flying behavior in a suitable separate capability instead.
I — Interface Segregation
Do not force a component to depend on methods it does not need.
Example: a print-only device should not be forced to implement scanning and faxing. Give those capabilities separate interfaces.
D — Dependency Inversion
Let important business code depend on an agreed interface rather than one provider's exact implementation.
Example: order code depends on PaymentGateway. An adapter connects that interface to your chosen provider.
| Principle | Short reminder |
|---|---|
| S | One clear responsibility |
| O | Make expected extensions easy |
| L | Keep the promised behavior |
| I | Keep interfaces focused |
| D | Depend on abstractions |
What are coupling and cohesion?
Coupling is how much one part depends on another part's details. Less unnecessary coupling makes changes easier.
Cohesion is how closely the jobs inside one part belong together. A class that does one related job has stronger cohesion than a class containing random unrelated tasks.
Advanced — A small TypeScript example
interface PaymentGateway {
pay(amount: number): Promise<void>;
}
class OrderService {
constructor(private gateway: PaymentGateway) {}
async checkout(amount: number) {
await this.gateway.pay(amount);
}
}
OrderService knows it can call pay(). It does not need to know the provider's internal code. Passing the gateway into the constructor is a form of dependency injection.
This example only demonstrates code organization. Real payments also need currency/amount rules, persisted status, duplicate protection, and failure handling.
SOLID is guidance, not a guarantee that the application is correct. Open/Closed does not mean you can never fix existing code. Too many unnecessary interfaces can make simple code harder to understand.
Remember: The goal is understandable, changeable code—not the maximum possible number of classes.
7. Security and login
7.1 Security layers in system design
Why do we need several layers?
Think of a house. A gate helps, but you may also need a door lock and a safe for important things. One protection should not be your entire security plan.
An application also protects different things at different places.
| Layer | Simple question | Example |
|---|---|---|
| Authentication | Who are you? | Check your login |
| Authorization | What are you allowed to do? | Let you see your orders, not everyone else's |
| API security | Is this request acceptable? | Check input and request limits |
| Data security | Is the information protected? | Protect stored passwords and network traffic |
| Infrastructure security | Who can reach our systems? | Restrict database access |
| Monitoring/fraud detection | Is something unusual happening? | Notice repeated failed login attempts |
Authentication vs authorization
You log in successfully: the system knows which account you are using.
You try to delete another person's order: the system must still check whether you are allowed to do that.
Being logged in does not mean you can do everything.
Password protection
Do not save a plain password. Store a suitable password hash with a salt. A hash is a one-way result used for verification. It is not a password encrypted for later decryption.
Real-world use: In a delivery app, a driver can manage an assigned delivery but should not be able to give themselves admin permissions.
Advanced — Security terms from the video
| Term | Simple meaning |
|---|---|
| OTP | A code intended for one-time use |
| MFA | Verify using different kinds of evidence, such as a password and a possessed device |
| Passkey/WebAuthn | A login credential based on public-key cryptography |
| RBAC | Give permissions based on roles |
| ABAC | Decide access using attributes such as owner, organization, and context |
| TLS | Protect communication while data travels |
| Encryption at rest | Protect data in stored form |
| WAF | Web Application Firewall; applies rules to web traffic |
| VPC/private network | A network boundary used to control infrastructure access |
A passkey may be unlocked on the device with a fingerprint or PIN. That does not mean the website receives your fingerprint.
Argon2 is used for password hashing/key derivation. AES is an encryption algorithm. DES is obsolete for new designs.
Store secrets safely, limit each service's permissions, and use parameterized database queries. Avoid recording passwords and tokens in logs.
Monitoring uses logs, metrics, traces, alerts, and audit records. It helps detect trouble, but does not replace preventive controls.
Remember: Check both identity and permission. Protect the data and the infrastructure too.
7.2 JWT — JSON Web Token
What is a JWT?
A JWT is a format for carrying claims. A claim is a statement such as “this token is for user 7” or “this token expires at this time.”
Imagine an event pass. You first prove you are allowed into the event. Then you show the pass at different checkpoints instead of repeating the entire registration process.
A signed JWT works somewhat like that pass.
How does it work?
- You log in.
- The server checks your credentials.
- It gives you a token.
- Your app sends that token with later requests.
- The receiving service checks that the token is valid before using its claims.
What is inside a typical signed JWT?
header.payload.signature
| Part | Simple meaning |
|---|---|
| Header | Information about the token and signing algorithm |
| Payload | Claims such as user ID and expiry |
| Signature | Helps verify the issuer and detect unauthorized changes |
Real-world use: Several services can check a trusted token when handling API requests.
Very important: A usual signed JWT is not secret inside. Its payload can be decoded and read. Do not put passwords or secret keys in it.
Advanced — Validation, expiry, and logout
The API must check the signature and expected algorithm, issuer, audience, expiry, and required claims. It must then check permission for the requested action.
| Claim | Meaning |
|---|---|
sub | Who or what the token is about |
iss | Who issued it |
aud | Who it is intended for |
exp | When it expires |
iat | When it was issued |
Removing a token from your browser does not destroy a stolen copy. It may remain valid until expiry unless the server checks revocation or other state.
Short-lived access tokens, controlled refresh tokens, revocation records, and current-permission checks solve different parts of this problem.
With asymmetric signing, a private key signs and public keys verify. With shared-secret signing, a service holding the secret can also sign tokens. Signing secrets must not be sent to the frontend.
Encoding, signing, and encryption are different operations. Encrypted JWT formats exist, but do not assume an ordinary signed token is encrypted.
Remember: A JWT carries claims. Correct validation makes those claims useful; the token's presence alone proves nothing.
7.3 Sessions vs JWT, OAuth, OIDC, and SSO
What is a session?
A session lets the server remember a logged-in user.
Imagine leaving a bag at a cloakroom. You receive a random ticket number. The staff keep the information about which bag belongs to that number.
A server-side login session is similar:
- You log in.
- The server saves session information.
- It gives your browser an unpredictable session ID, usually in a cookie.
- The browser sends the ID with later requests.
- The server looks up the matching session.
How is that different from a JWT?
| Session ID | Signed JWT |
|---|---|
| Usually points to information stored on the server | Carries claims inside the token |
| Server checks the session record | API can verify the signature and claims |
| Deleting the server session can revoke it | Immediate revocation may require extra state/checks |
Can sessions work with multiple servers? Yes. The servers can share a session store, such as Redis. The session does not have to exist only in one server's memory.
What is OAuth?
OAuth is a way to let another application access specific things without giving it your account password.
Think of giving a valet limited permission to park your car. You do not also give access to your house.
Example: Let a scheduling application read your calendar without allowing it to change everything in your account.
What is OIDC?
OpenID Connect adds identity/login information on top of OAuth 2.0. It helps an application learn who signed in through an identity provider.
What is SSO?
Single Sign-On means signing in once and then accessing participating applications without separately logging in to each one.
Example: Your company login gives you access to its internal tools.
Advanced — These are different categories
| Concept | Category |
|---|---|
| Session | A way to manage ongoing login state |
| JWT | A token format |
| OAuth | A delegated authorization framework |
| OIDC | An identity/authentication layer |
| SSO | A shared sign-in experience across applications |
These can be used together. They are not five interchangeable login options.
A scope describes a named permission, such as reading calendar events.
A cookie is a browser storage/transport mechanism. It can contain a session ID or a token. A JWT is not the opposite of a cookie.
Cookie settings such as HttpOnly, Secure, and SameSite help control exposure and sending behavior. Cookie-based authentication also needs suitable protection against forged cross-site requests. Browser-readable tokens have their own risks if malicious JavaScript runs on the page.
Phone photo-library permissions are operating-system permissions, not automatically OAuth.
Remember: Sessions remember login state, JWTs carry claims, OAuth delegates access, OIDC supports identity login, and SSO reuses sign-in across apps.
8. Real-world designs
These examples put the earlier ideas together. Start with the basic flow. The Advanced sections explain what changes when failures and large traffic arrive.
8.1 Design an Instagram-style photo and video app
What are we building?
An app where people create profiles, follow others, upload photos/videos, see a feed, like/comment, send messages, and receive notifications.
Start with the main parts
| Part | Job |
|---|---|
| User service | Manage accounts and profiles |
| Follow service | Remember who follows whom |
| Post service | Save post information |
| Media storage | Keep photo and video files |
| Feed service | Decide which posts appear on the home screen |
| Like/comment service | Record interactions |
| Messaging service | Handle messages |
| Notification service | Send alerts |
| Search/recommendation | Help users find relevant content |
| Safety/ads/analytics | Check content, manage ads, and understand usage |
Stories and reels have their own product rules. Live features may need separate media delivery and real-time control systems.
What happens when you upload a video?
- The app checks that you are allowed to upload.
- The video file is uploaded to storage.
- A background task checks and processes it.
- Workers create suitable video versions and thumbnails.
- The app marks the post ready.
- Your followers can discover it in their feeds.
- A CDN helps deliver the video to viewers.
Why create several video versions?
One person has fast internet; another has slow internet. Different video sizes/qualities help the player choose something suitable. Converting media into these versions is called transcoding.
What happens when someone opens the feed?
The feed service finds possible posts, removes unsuitable ones, chooses an order, and returns post information. The client then loads media from delivery servers.
Recommendation words, simply
| Word | Meaning |
|---|---|
| Retrieval | Find possible posts to show |
| Filtering | Remove posts that should not be shown |
| Ranking | Decide which remaining posts are more relevant |
| Reranking | Adjust the order for things such as variety |
For example, even if one creator has many highly ranked posts, the app may avoid showing ten of their posts in a row.
Advanced — Celebrity feeds and fan-out
Fan-out means spreading a post reference to the feeds where it may belong.
| Approach | How it works | Cost |
|---|---|---|
| Fan-out on write | Add a post reference to followers' feed stores when it is published | Expensive for accounts with huge follower counts |
| Fan-out on read | Assemble relevant posts when someone opens a feed | More work during feed reads |
| Hybrid | Use both approaches for different accounts | More logic but a useful balance |
A celebrity post should not automatically trigger an enormous immediate update to every follower's stored feed. A hybrid design can merge those posts when users read their feeds.
Usually, fan-out distributes references such as post IDs—not a separate copy of the entire video for every follower.
Advanced — Answers to the video's difficult questions
| Question | Simple design answer |
|---|---|
| What if the internet disconnects during a large upload? | Upload in resumable parts and continue from saved progress |
| Why does the heart turn red before the count changes? | The UI can show the action immediately while a background count catches up; undo the UI change if the request fails |
| What if a popular post gets huge numbers of likes? | Store interactions safely and combine count updates to reduce pressure |
| How do we send huge numbers of notifications? | Queue, group, and limit notifications instead of sending one alert for every event |
| What happens when a private/deleted post remains in an old feed copy? | Recheck access/visibility so the stale reference does not expose the content |
A unique rule on (user_id, post_id) can prevent duplicate likes from the same user. The total count is a summary; it should not be the only record of who liked the post.
An analytics event records an action such as a view or skip. A data lake holds large collections of data for analysis. Recommendation models may learn from suitable events, subject to privacy and retention rules.
WebSockets can carry chat or live control updates. They are not automatically the best way to deliver every live video stream.
Remember: Media delivery, post metadata, feeds, and notifications are different jobs. Give each job a suitable design.
8.2 Design an IRCTC-style train-booking system
What are we building?
An app where people search trains, check seats, book, pay, cancel, and receive tickets.
The hardest situation is a booking rush: many people want a small number of seats at the same time.
Two separate problems
- Scaling: How do we handle all the requests?
- Concurrency control: How do we stop two requests from confirming the same seat?
Adding servers helps with the first problem. It does not automatically solve the second.
Main parts
| Part | Job |
|---|---|
| CDN/cache | Serve assets and suitable stable information |
| Load balancer | Share application requests |
| Gateway/rate limits | Check entry rules and reduce repeated requests |
| Search service | Find trains, routes, dates, and classes |
| Inventory service | Track actual available/held/booked seats |
| Booking service | Manage the booking's progress |
| Payment integration | Start payments and check their outcomes |
| Database | Safely store inventory and bookings |
| Queue/workers | Send tickets and notifications |
Simple safe booking flow
- You choose a train and request a seat.
- The backend safely creates a temporary hold if a seat is available.
- The hold has a time limit.
- You complete payment.
- The backend verifies the payment result.
- If the valid hold still belongs to you, it confirms the booking.
- A background worker sends the ticket.
- A failed or expired attempt releases the hold according to the rules.
What is a hold?
It is a saved reservation saying, “This seat is temporarily reserved for this booking until a certain time.” It gives the user time to pay.
Why not just check and then book?
Suppose both Sandeep and Priya read “one seat left.” Both then try to buy it. Reading availability alone does not decide the winner. The database must safely combine the decision and update so both cannot succeed.
Advanced — Locks, atomic updates, and short transactions
A lock coordinates conflicting operations. Think of one person holding the right to modify a record while another conflicting operation waits.
A transaction groups the necessary database steps. Keep it short. Do not leave it open while a user spends several minutes on a payment page.
An atomic update safely checks and changes a value as one operation. A simplified example is:
UPDATE seat_inventory
SET status = 'HELD', hold_id = :hold_id
WHERE journey_id = :journey_id
AND seat_id = :seat_id
AND status = 'AVAILABLE';
Continue only if exactly one row was changed. The placeholders represent parameters. This example shows the central idea; a full implementation also needs expiry, booking records, transaction handling, and release rules.
Optimistic concurrency checks that a version has not changed before updating. If someone changed it first, the operation fails or retries.
Constraints enforce rules in the database. They help ensure the data remains valid even under competing requests.
A cached seat count may be old. Final allocation must use authoritative inventory. A Redis lock alone should not be the only protection for a confirmed seat.
Real train inventory also considers route segments: the same seat can be used by different people on nonoverlapping portions of a journey.
Advanced — Rush traffic, payment failures, and the remaining design questions
| Problem | Approach |
|---|---|
| Known booking rush | Add capacity in advance, test it, and admit requests at a sustainable rate |
| Too many repeated attempts | Rate limits and abuse controls |
| Too many database reads | Cache schedules and suitable browsing information |
| Database becoming overloaded | Limit connections and the amount of concurrent work |
| Payment result times out | Check status; timeout does not prove failure |
| Payment succeeds after the hold expires | Reconcile and compensate/refund as required |
| Request is retried | Use an idempotency key to return the existing operation instead of creating another |
| Email/SMS fails after booking | Retry notification; keep the valid booking |
| A cancellation occurs | Apply the fee rule, update inventory, and track refund progress |
| A seat becomes available for a waitlist | Promote according to a defined policy and update safely |
| Need a booking reference/PNR | Use a controlled ID scheme and enforce uniqueness |
Idempotency means repeating the same operation does not repeat its business effect.
Compensation is a new action that undoes the business effect of earlier work, such as a refund. You cannot assume a local database rollback automatically undoes an external payment.
SQL-style transactional storage is often a natural fit for inventory rules. Redis can help with shared caches, sessions, and limits. Kafka/RabbitMQ can support background events and notifications. None of those tools replaces clear ownership rules.
LLD might include Train, Journey, Seat, Booking, Payment, and Ticket. Use clear states such as pending, held, confirmed, cancelled, and expired. Strategy-style code can organize cancellation rules; adapters can hide payment-provider differences.
Decide fairness explicitly. A database lock does not automatically guarantee first-come-first-served booking. Do not assume a learning example describes IRCTC's actual priority rules.
Remember: Handle traffic with capacity controls. Protect seats with correct data operations. Treat payment and notification failures separately.
8.3 UPI system design
What is UPI?
UPI means Unified Payments Interface. It is an Indian payment system operated by NPCI. It lets people initiate payments through participating apps and institutions.
For learning, focus on the roles and flow. This is not a complete technical integration guide.
Who does what?
| Participant | Simple role |
|---|---|
| Payer | The person sending money |
| Payee/beneficiary | The person receiving money |
| UPI app | The screen where the user starts the payment |
| PSP bank | A Payment Service Provider bank connecting the app/customer to UPI |
| NPCI | Operates the UPI platform and routes messages among participants |
| Remitter bank | Holds the payer's account for the transfer |
| Beneficiary bank | Holds the recipient's account |
A VPA, or Virtual Payment Address, is commonly called a UPI ID. A QR code can contain the address and other payment information.
A simplified payment flow
- You open an app and choose the recipient and amount.
- You authorize the payment through the required flow.
- The request reaches the UPI system through the app's PSP arrangement.
- NPCI routes messages to the relevant participants.
- The payer's bank checks and processes the debit according to its rules.
- The recipient's bank processes the credit.
- Status information comes back to the app.
The PSP bank and the bank holding your account can be different. Money does not have to enter a separate app wallet just because you used that app to initiate the payment.
What can we learn for system design?
Payment processing needs safe records, duplicate protection, controlled access, monitoring, and recovery when a participant or network has a problem.
Advanced — Pending outcomes, reversals, and reconciliation
Suppose the app sends a payment request and loses the connection. Did the transfer fail? Not necessarily. It might have completed while the response was lost.
That is why a payment can be pending or have an unknown outcome.
| Situation | What the system should do |
|---|---|
| Verified rejection before debit | Report the known result |
| Timeout | Check authoritative status rather than assume failure |
| Debit occurs but credit outcome is unclear | Investigate/reconcile the participant records |
| Same operation arrives again | Recognize it and avoid another business effect |
| Recovery requires money to be returned | Track the required reversal/compensation |
Reconciliation means comparing records and resolving differences about what happened.
A cross-bank transfer is not one ordinary SQL transaction your app can instantly roll back. Ledger updates, status messages, reversals, and settlement have their own responsibilities.
A TPAP is a third-party application provider participating through PSP arrangements.
Stateless application instances can still use a database to save every important payment state. Stateless means recovery should not depend only on one server's private memory.
Asynchronous processing lets other work progress while a response is pending. It does not by itself guarantee that money moved correctly.
This explanation does not assume current fees, guaranteed completion times, or a specific public integration protocol. Those details must come from the participating system's documentation.
Remember: The app starts the user's request. The payment network and banks coordinate the actual operation, and uncertain outcomes require careful recovery.
Quick revision
One simple reminder for each main idea
| Concept | Remember it like this |
|---|---|
| System design | Plan how the parts work together |
| DNS | Find the address for a name |
| HLD | Big-picture plan |
| LLD | Detailed plan inside a component |
| Vertical scaling | Make one machine stronger |
| Horizontal scaling | Add more machines |
| Load balancer | Share incoming work |
| CDN | Deliver content from suitable nearby locations |
| Forward proxy | A middleman for clients |
| Reverse proxy | A middleman for servers |
| HTTPS/TLS | Protect the connection |
| Cache | Reuse a useful copy |
| API | The agreed way software interacts |
| API gateway | Manage and route API entry traffic |
| REST | Work with resources through an interface |
| GraphQL | Ask for selected fields |
| gRPC | Call a remote service method |
| WebSocket | Keep a two-way message connection open |
| Webhook | Send an HTTP notification when an event happens |
| SOAP | Exchange formally structured XML messages |
| SDK | Ready-made development tools |
| Partitioning | Split data into manageable parts |
| Sharding | Put different parts on different nodes |
| Replication | Keep copies |
| Document database | Store records with flexible fields |
| Key-value database | Use a key to get a value |
| Wide-column database | Design distributed data groups around access patterns |
| Graph database | Follow relationships |
| CAP | Understand the C/A trade-off during a network break |
| ACID | Transaction guarantees |
| BASE | Allow suitable data to catch up over time |
| Queue | Hold work for separate processing |
| Write-through | Complete the backing write as part of the operation |
| Write-back | Propagate a cached write later |
| Rate limiting | Control request usage over time |
| Microservices | Independently deployable business services |
| SOLID | Principles for manageable code changes |
| Authentication | Who are you? |
| Authorization | What may you do? |
| Session | Server-managed login state |
| JWT | A format carrying claims |
| OAuth | Delegate limited access |
| OIDC | Add identity/login to OAuth |
| SSO | Reuse sign-in across participating apps |
Advanced — A simple way to answer system design interview questions
- Ask what the system must do. For example: search trains, book seats, cancel tickets.
- Ask what must never go wrong. For example: never confirm one seat for conflicting bookings.
- Estimate the workload. How many users, reads, writes, and files? What is the busiest period?
- Draw the simplest useful design. Client, API/backend, and database are a good starting point.
- Follow one read and one write. Explain each component's job.
- Find the likely bottleneck. Add caching, indexes, replicas, or more servers for a reason.
- Walk through a failure. What happens after a timeout, duplicate request, or server crash?
- Explain security and monitoring. Who can access what, and how will you notice problems?
- Explain the trade-off. What does your choice improve, and what becomes harder?
Functional requirements describe what the system does. Nonfunctional requirements describe qualities such as speed, availability, security, and scale.
For a rough average: requests per second = daily requests ÷ 86,400. The busiest moment can be much higher than the average.
Do not just list Redis, Kafka, Kubernetes, and five databases. Explain the actual problem each one solves. If you cannot explain why a component is needed, the design may not need it yet.
References
Advanced — Optional technical reading
The uploaded script provides the topic outline. These references support the technical clarifications. You do not need to read them before understanding the beginner explanations.
- IANA: root DNS servers
- RFC Editor: TLS 1.3
- PostgreSQL: table partitioning
- MongoDB: document limits
- Apache Cassandra: architecture
- Apache Cassandra: data definition
- Martin Kleppmann: A Critique of the CAP Theorem
- Redis: rate-limiting algorithms
- RFC Editor: JSON Web Token
- Auth0: OAuth 2.0
- Auth0: OpenID Connect
- NPCI: about UPI
- Google Pay: UPI participant responsibilities