<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Blackwatch</title>
    <description>The latest articles on DEV Community by Blackwatch (@blackwatch021).</description>
    <link>https://dev.to/blackwatch021</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F753337%2F1a960c31-5c88-416d-81b4-aa1c262d5029.jpeg</url>
      <title>DEV Community: Blackwatch</title>
      <link>https://dev.to/blackwatch021</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blackwatch021"/>
    <language>en</language>
    <item>
      <title>System Design 101 - The Building Blocks Every Developer Should Know</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Sat, 31 Oct 2026 13:51:13 +0000</pubDate>
      <link>https://dev.to/blackwatch021/system-design-101-the-building-blocks-every-developer-should-know-126f</link>
      <guid>https://dev.to/blackwatch021/system-design-101-the-building-blocks-every-developer-should-know-126f</guid>
      <description>&lt;p&gt;As a software developer why do we need a system design, isn't this one extra, "UNNECESSARY" step?? If we can directly jump into coding, make applications and make changes on the go. Well... right and wrong too.&lt;/p&gt;

&lt;p&gt;For very simple applications like a todo list or a color changer, no system design is required. But when we talk about masses, lot of data moving from one point to another, lot of users accessing the services at the same time, sudden surge in the usage etc. there we need system design.&lt;/p&gt;

&lt;p&gt;Think of it like this, when you are making noodles for yourself, you don't think a lot. But when you have to cook for 20 people, you prepare a whole plan first and only then execute it. That is system design. Designing the whole plan of how things will work and how things will take place, before actually picking up the tools (here, coding). It gives a clear cut idea of what we are building, and in case we are stuck, we can always refer back to it.&lt;/p&gt;

&lt;p&gt;In one line, &lt;strong&gt;System Design = Components + a Common Goal&lt;/strong&gt;. Lots of small pieces (database, server, cache, load balancer...) all working together to achieve one goal.&lt;/p&gt;

&lt;p&gt;In this article I will walk you through the 7 core components of a system and the main ideas around them, so treat it as your System Design 101.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Requirement&lt;/strong&gt; - Little bit of technical knowledge before hand (what a server, database and API roughly are).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What Are We Even Dealing With? Data!
&lt;/h2&gt;

&lt;p&gt;At the end of the day, almost every application is moving data around. Mainly it is one of these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Video&lt;/li&gt;
&lt;li&gt;Audio&lt;/li&gt;
&lt;li&gt;Text&lt;/li&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keep this in mind, cause every component we discuss below exists to store, move, or serve this data better.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Components
&lt;/h2&gt;

&lt;p&gt;Here is the list of components we will look at:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Database&lt;/li&gt;
&lt;li&gt;Server (Application Layer)&lt;/li&gt;
&lt;li&gt;Frontend (Client Application Layer)&lt;/li&gt;
&lt;li&gt;Cache Layer&lt;/li&gt;
&lt;li&gt;Load Balancer&lt;/li&gt;
&lt;li&gt;Message Queue&lt;/li&gt;
&lt;li&gt;Monitoring and Logs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F243kjaghfd25vjsd021z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F243kjaghfd25vjsd021z.png" alt="How the frontend, server and database talk to each other" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start with the first one.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Database
&lt;/h2&gt;

&lt;p&gt;The whole point of an application is to manage, deliver, present and analyse data, and give it to the users in a usable manner. A highly sophisticated application without data is of no use. So to hold this data for future operations, we need a place to store it, and that place is called a &lt;strong&gt;database (DB)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now we have two types of databases.&lt;/p&gt;

&lt;h3&gt;
  
  
  SQL (Relational Databases)
&lt;/h3&gt;

&lt;p&gt;SQL stands for &lt;strong&gt;Structured Query Language&lt;/strong&gt;. Technically SQL is the &lt;em&gt;language&lt;/em&gt; we use to talk to the database, and the databases that use it are called &lt;strong&gt;Relational Databases&lt;/strong&gt; (MySQL, PostgreSQL, Oracle etc.), but in day to day talk everyone just calls them "SQL databases".&lt;/p&gt;

&lt;p&gt;It's all about storing data in a structured manner. It uses &lt;strong&gt;tables&lt;/strong&gt; (rows and columns) and close &lt;strong&gt;relations&lt;/strong&gt; between these tables to present data. Every entity gets its own table, like Users, Posts, Pictures, Comments, Blogs.&lt;/p&gt;

&lt;p&gt;Here is a small &lt;code&gt;users&lt;/code&gt; table, every row is one user:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;fname&lt;/th&gt;
&lt;th&gt;lname&lt;/th&gt;
&lt;th&gt;phone_no&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Example&lt;/td&gt;
&lt;td&gt;Test&lt;/td&gt;
&lt;td&gt;9874597890&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These are good for complex queries and &lt;strong&gt;transactions&lt;/strong&gt;. A transaction is a group of operations which either all happen or none of them happen. Imagine sending money to a friend, money should go out of your account AND come into theirs. If only one of these happens, that's a disaster. SQL DBs guarantee this using &lt;strong&gt;ACID&lt;/strong&gt; properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A - Atomicity&lt;/strong&gt; - all or nothing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C - Consistency&lt;/strong&gt; - data always follows the rules we set&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I - Isolation&lt;/strong&gt; - two transactions running together don't mess up each other&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D - Durability&lt;/strong&gt; - once saved, it stays saved, even if the power goes off&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  No-SQL (Non-Relational Databases)
&lt;/h3&gt;

&lt;p&gt;Good for unstructured data and ingesting data in bulk. Stores data as key-value pairs, documents, columns or graphs (we'll see each type below). No fixed structure, in the first object we can save only one value while in the second we can store 50 values, no issue. Meanwhile SQL is structured, every row follows the same structure.&lt;/p&gt;

&lt;p&gt;Here we will focus more on SQL first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constraints
&lt;/h3&gt;

&lt;p&gt;While creating a table in SQL, we can set some rules which need to be followed to ensure consistent and good data. These rules are called constraints. The common ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NOT NULL&lt;/strong&gt; - value can't be empty/null&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UNIQUE&lt;/strong&gt; - every value in that column has to be unique (like email)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DEFAULT&lt;/strong&gt; - sets a default value if nothing is passed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CHECK&lt;/strong&gt; - value must pass a condition, e.g. &lt;code&gt;age &amp;gt;= 18&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PRIMARY KEY&lt;/strong&gt; - the unique identity of every row, like the &lt;code&gt;id&lt;/code&gt; column. It is UNIQUE + NOT NULL together, and a table can have only one primary key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FOREIGN KEY&lt;/strong&gt; - a column which points to the primary key of another table. This is how tables get connected. E.g. a &lt;code&gt;posts&lt;/code&gt; table has a &lt;code&gt;user_id&lt;/code&gt; column which points to &lt;code&gt;users.id&lt;/code&gt;, so every post knows which user it belongs to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is important cause users will feed data, and it's our responsibility to ensure that it's correct and following a particular format.&lt;/p&gt;

&lt;h3&gt;
  
  
  Joins
&lt;/h3&gt;

&lt;p&gt;Used to combine two or more tables (using the related columns, usually the foreign key) and then run a query on it to extract the required data. Most common ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;INNER JOIN&lt;/strong&gt; - only the rows which have a match in both tables&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LEFT JOIN&lt;/strong&gt; - all rows from the left table + matching ones from the right&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RIGHT JOIN&lt;/strong&gt; - all rows from the right table + matching ones from the left&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FULL JOIN&lt;/strong&gt; - everything from both, matched where possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example - "Give me all posts along with the name of the user who wrote them" = join &lt;code&gt;posts&lt;/code&gt; and &lt;code&gt;users&lt;/code&gt; on &lt;code&gt;posts.user_id = users.id&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Relations
&lt;/h3&gt;

&lt;p&gt;This describes how two tables are connected with each other.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One to One&lt;/strong&gt; - one user has one profile, one profile belongs to one user&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One to Many&lt;/strong&gt; - one user can write many posts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many to One&lt;/strong&gt; - many posts belong to one user (same thing as above, just seen from the other side)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many to Many&lt;/strong&gt; - a student can enroll in many courses, and a course has many students. For this we use a third table in between (&lt;code&gt;enrollments&lt;/code&gt;) which holds &lt;code&gt;student_id&lt;/code&gt; and &lt;code&gt;course_id&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  DB Scaling (the basics)
&lt;/h3&gt;

&lt;p&gt;We know that each DB has a limit, it can't store unlimited data magically. To ensure it's healthy and running, we need to scale it as the users/data increase. Ways to scale it up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vertical Scaling&lt;/strong&gt; - increase the resources of the same machine - RAM, storage, CPU etc. But it can be done to a particular extent only, you can't keep adding RAM forever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Horizontal Scaling&lt;/strong&gt; - add more DBs (more machines).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo3coin8dhwukgj7xfblx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo3coin8dhwukgj7xfblx.png" alt="Vertical scaling vs horizontal scaling" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Issue with horizontal scaling in SQL is that it's tedious/hard. SQL data is connected through relations, so once it's spread over many machines, a single JOIN or transaction may need data from 3 different machines, and that gets slow and complicated. While in No-SQL every document stands on its own, so it can sit on any machine without worrying about the others. (We will dig deeper into DB scaling later in this article.)&lt;/p&gt;

&lt;h3&gt;
  
  
  No-SQL in short
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Easy to scale&lt;/li&gt;
&lt;li&gt;Schema less&lt;/li&gt;
&lt;li&gt;No heavy linking between different entities, unlike SQL's relations&lt;/li&gt;
&lt;li&gt;Every entity stands on its own&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When SQL, When No-SQL??
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL&lt;/strong&gt; - payments, transactions, places where data is structured and consistency is a must&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No-SQL&lt;/strong&gt; - scale, speed, unstructured data
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Consistency  &amp;gt;&amp;gt;  Availability                      =&amp;gt;  SQL
Availability + Scalability  &amp;gt;&amp;gt;  Strict consistency  =&amp;gt;  No-SQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Types of No-SQL
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7m93irosb9zg0syxoiok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7m93irosb9zg0syxoiok.png" alt="Four types of No-SQL databases - key-value, columnar, graph and document" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Key-Value DB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The simplest one. Every piece of data is stored as a &lt;strong&gt;unique key&lt;/strong&gt; and its &lt;strong&gt;value&lt;/strong&gt;. The value can be anything - JSON, string, number, blob, array, or a combination of them. Think of it like a giant dictionary, give the key, get the value, super fast.&lt;/p&gt;

&lt;p&gt;Used for - cache, cookies/sessions, simple data storage.&lt;br&gt;
Ex - Redis, Amazon DynamoDB.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Columnar DB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Similar to a table in SQL, but it stores and reads data &lt;strong&gt;column wise&lt;/strong&gt;. Normal SQL DBs read the table row by row, so in case we want only the &lt;code&gt;marks&lt;/code&gt; column, a relational DB will still read row wise. Columnar DBs allow us to read only the columns we need.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;marks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;name1&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;name2&lt;/td&gt;
&lt;td&gt;95&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Ask "what's the average of marks?" and a columnar DB reads only the marks column (90, 95), it doesn't even touch the names.&lt;/p&gt;

&lt;p&gt;Ex - Google BigQuery, Amazon Redshift, Snowflake, ClickHouse (these actually speak SQL, but they store data column wise, which is the idea here).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Good for analysis, like reading millions of rows to build a report&lt;/li&gt;
&lt;li&gt;Writing in this is slower than other No-SQL DBs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Graph DB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Data is stored as &lt;strong&gt;Nodes&lt;/strong&gt; (the entities) and &lt;strong&gt;Edges&lt;/strong&gt; (the relationship between them). Both nodes and edges can have properties. Like a Student node connected to a Course node with an "enrolled" edge, and that edge holds the year, marks and year of passing. Good to find hidden patterns in analysis/research, like "friends of friends" on social media or fraud detection.&lt;/p&gt;

&lt;p&gt;Ex - Neo4j.&lt;/p&gt;

&lt;p&gt;Cons - complex to design, and slow for normal bulk queries which don't need relationships.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Document DB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This one is used widely. Data is stored as &lt;strong&gt;documents&lt;/strong&gt;, usually JSON-like objects (each document is like a key-value pair, where the value is a whole object).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flexible&lt;/li&gt;
&lt;li&gt;Schemaless&lt;/li&gt;
&lt;li&gt;No relations&lt;/li&gt;
&lt;li&gt;Ex - MongoDB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good for - logging, user profiles. Like an Instagram post, one document can hold the image, video and text together.&lt;br&gt;
Reads are fast cause one document has everything, no joins needed. But if the same data is copied into many documents, updating all of them gets messy.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Server (Application Layer)
&lt;/h2&gt;

&lt;p&gt;The backbone of any application. It is responsible for doing the heavy lifting, does all the operations, fulfils users' requests and acts as a connecting bridge between the frontend and the database. It hides the complexities of the DB and gives easy access to it through &lt;strong&gt;APIs -&amp;gt; Endpoints (CRUD - Create, Read, Update, Delete)&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Frontend (Client Application Layer)
&lt;/h2&gt;

&lt;p&gt;The device you are using currently to read this article, the website or the mobile app. The GUI, or the front face of any application through which the user interacts, is called the frontend.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Cache Layer
&lt;/h2&gt;

&lt;p&gt;A cache is a small but very fast storage (mostly kept in memory/RAM) which keeps the most asked data, and it mostly sits in between the server and the actual DB.&lt;/p&gt;

&lt;p&gt;Imagine a store, you walk into it and see that most of the items are kept on their shelves while some of the most used items are kept right on the counter. The shelves are our DB, while the items on the counter are our cache, for quick and easy access.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F404j6yec9344v9iqvimy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F404j6yec9344v9iqvimy.png" alt="Cache sits between the server and the database" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now imagine a situation, your website has a hero section where you are showing 6 courses, and each course needs an image, course details, price and a preview.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Single user  = 6 courses x 4 APIs        =        24 requests
10,000 users = 24 x 10,000               =   2,40,000 requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So 24 requests just to populate the hero section for a single user. Now imagine 10,000 users land on the site, it becomes 2,40,000 read requests to the DB, all asking for the exact same data. So we cache it and serve it from there only, cause it's not going to change, it's the same for every user. It saves bandwidth, decreases latency (the delay between asking for data and getting it) and improves user experience. And remember, &lt;strong&gt;high latency = poor performance = bad UX = business loss&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Important terminology
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cache Hit&lt;/strong&gt; - data is present in the cache = fast response&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache Miss&lt;/strong&gt; - data is not in the cache, so we go to the DB = slower response&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TTL (Time To Live)&lt;/strong&gt; - expiry time of the data in the cache. Once the TTL is over, the data is removed and fetched fresh next time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Storage in cache
&lt;/h3&gt;

&lt;p&gt;Data in a cache is stored like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;TTL&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;course_6_price&lt;/td&gt;
&lt;td&gt;499&lt;/td&gt;
&lt;td&gt;1 hr&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And &lt;strong&gt;cache size &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; actual DB&lt;/strong&gt;. The cache is kept small cause it lives in RAM, which is expensive. So we only keep the hot data, the stuff that's asked for again and again, otherwise it will become just another simple DB.&lt;/p&gt;

&lt;h3&gt;
  
  
  Caching Strategies
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbif3qeis2zwyvanytmdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbif3qeis2zwyvanytmdo.png" alt="The four caching strategies - read through, write through, write around and write back" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Read Through Cache (RTC)&lt;/strong&gt; - for reading&lt;/p&gt;

&lt;p&gt;First we will read the cache, if it's a cache miss then only we will read the DB and update the cache on the way back. (In pure read-through, the cache itself goes to the DB on a miss. When our app code does that job instead, it's called &lt;em&gt;cache-aside&lt;/em&gt;, but the idea is the same.)&lt;br&gt;
For write requests, it always goes to the DB first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Write Through Cache (WTC)&lt;/strong&gt; - for writing&lt;/p&gt;

&lt;p&gt;Every write goes to the cache and the DB together, the write is done only when both are updated. So the cache always has the latest data.&lt;br&gt;
Good for fast paced data where the latest data matters - like stock values.&lt;br&gt;
Downside - writes are a bit slower, cause we write at two places.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Write Around Cache (WAC)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Writes go directly to the DB, no cache update. Reads always go through the cache, on a hit we return, on a miss we go to the DB, update the cache and then return.&lt;br&gt;
Good when data is written once and rarely read immediately after, so we don't fill the cache with stuff nobody asks for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Write Back Cache (WBC)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this, the data is first written in the cache and the response is sent back to the BE (backend) right away, the write on the DB happens asynchronously. The issue is that there may be inconsistency, what if an error occurs while writing to the DB, or the cache crashes before saving? That data is lost.&lt;br&gt;
Read is similar to above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed &amp;gt;&amp;gt;&amp;gt; Consistency&lt;/strong&gt; is the trade-off here.&lt;/p&gt;

&lt;p&gt;Ex - high write traffic, like Swiggy/Zomato/Uber Eats where order status and delivery location update very quickly.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cache Eviction Strategies
&lt;/h3&gt;

&lt;p&gt;Cache is small, remember? So when it's full and new data comes, something has to go out. Who goes out is decided by eviction strategies.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;LRU - Least Recently Used&lt;/strong&gt; - remove the data which hasn't been used for the longest time. The most common one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MRU - Most Recently Used&lt;/strong&gt; - remove the data which was used most recently. Sounds weird, but think of streaming services, once you've watched an episode, you are not going to watch it again, the next episode is what you need.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LFU - Least Frequently Used&lt;/strong&gt; - remove the data which is used the least number of times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FIFO - First In First Out&lt;/strong&gt; - remove the oldest added data, like a queue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LIFO - Last In First Out&lt;/strong&gt; - remove the newest added data, like a stack of plates.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  5. Load Balancer
&lt;/h2&gt;

&lt;p&gt;As the application grows, we can't just rely on a single server, can we? What will happen if that server faults and goes down? The whole application will be unusable. To mitigate this and to give quick responses to the incoming requests, we have a cluster of servers working together. Now how will a request know which server to hit? For that we have the &lt;strong&gt;Load Balancer (LB)&lt;/strong&gt;. It decides which request will go to which server and maintains the load on the servers, so no one server dies under heavy load.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F609q59djt73fhm95vm9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F609q59djt73fhm95vm9y.png" alt="A load balancer spreading user requests across servers" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Responsibilities of LB
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Choosing the server&lt;/li&gt;
&lt;li&gt;Checking servers' health&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  LB Algorithms
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F402bnp822odnms40ugdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F402bnp822odnms40ugdl.png" alt="Load balancing algorithms at a glance" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Round Robin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Selects servers in numerical order, checks nothing. With 4 servers it goes 1, 2, 3, 4, 1, 2, 3, 4...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to implement&lt;/li&gt;
&lt;li&gt;Equal distribution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Issue - what if the servers aren't of the same config?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;S1&lt;/th&gt;
&lt;th&gt;S2&lt;/th&gt;
&lt;th&gt;S3&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RAM&lt;/td&gt;
&lt;td&gt;4 GB&lt;/td&gt;
&lt;td&gt;8 GB&lt;/td&gt;
&lt;td&gt;16 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage&lt;/td&gt;
&lt;td&gt;100 GB&lt;/td&gt;
&lt;td&gt;200 GB&lt;/td&gt;
&lt;td&gt;300 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here S1 will face difficulties to keep up with the others, cause it isn't capable enough to match them, but it still gets the same number of requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Geo Based Algo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Based on the server's and user's location. The servers are spread across the globe and the nearest one to the user is selected to entertain its requests. A user sitting in India gets the India server, not the US or EU one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Less latency&lt;/li&gt;
&lt;li&gt;Increases performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LB needs to manage extra details - location of servers, user location, and constantly check servers' health&lt;/li&gt;
&lt;li&gt;DBs across regions need to be maintained, ensuring the data is uniform everywhere&lt;/li&gt;
&lt;li&gt;VPN/Proxies can mislead the traffic. If your VPN says you are in the US while you are sitting in India, you get routed to a US server, that's one of the reasons your internet feels slower on a VPN.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Least Connection Algo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The server with the least number of active connections gets the next request.&lt;/p&gt;

&lt;p&gt;Disadvantages&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What if the server with the least connections is handling a heavy task, the next request is still sent to it. It will choke.&lt;/li&gt;
&lt;li&gt;LB needs to keep records of the connections of each server to implement it.&lt;/li&gt;
&lt;li&gt;What if some servers have fewer resources, still they will get the request -&amp;gt; slower response.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;4. Least Time Algo (Least Response Time)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Selects the server with the least response time, eventually giving less average time. If S1 is answering in 50ms and S2 in 10ms, S2 gets the next request.&lt;/p&gt;

&lt;p&gt;Less response time = usually high config server.&lt;br&gt;
Ex - real time apps like trading.&lt;/p&gt;

&lt;p&gt;In this the LB needs to calculate the average response time of each server, which depends on the type and config of the server, number of requests etc. It's complex, plus it keeps changing with the traffic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. IP Hash Algo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The user's IP is passed through a hash function (a function that turns any input into a number, and the same input always gives the same number), and the result decides the server. So the same IP always lands on the same server, good for keeping the user's session on one server. Good thing, the IP rarely changes in a single session, but the moment the connection breaks or the service provider changes, the IP changes. If the server changes, then the user's data needs to be there on the new server too. E.g. hash values 1-10 go to S1, 11-20 to S2, 21-30 to S3.&lt;/p&gt;

&lt;p&gt;Major disadvantage - Scaling. New server -&amp;gt; hash ranges change in the LB -&amp;gt; new mapping for every server, so lots of users suddenly get shifted to a different server. (&lt;em&gt;Consistent hashing&lt;/em&gt; is the technique used to reduce this reshuffling.) Not a great fit for microservices where servers come and go often.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Weighted Round Robin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this, each server is assigned a weight/value, and the load balancer uses these weights to distribute the requests in a sequential manner.&lt;/p&gt;

&lt;p&gt;Imagine server A with weight 1 and server B with weight 3. The LB will send 1 request to server A and 3 requests to server B, then again 1 to A, and so on. This fixes the issue of normal round robin with different config servers, give the powerful one a bigger weight.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In reality we use hybrid algos, we don't rely solely on a single algo.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Health Check
&lt;/h3&gt;

&lt;p&gt;How does the LB know a server is down? By checking its health.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Passive way&lt;/strong&gt; - checks the responses of the already sent (actual) requests. If they keep failing, something is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Active way&lt;/strong&gt; - by sending extra requests (pings, like hitting a &lt;code&gt;/health&lt;/code&gt; endpoint) after a particular interval to check if the server is up and running.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Parameters for Health Check
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Interval&lt;/strong&gt; - how often we check our server's health. Every 5 sec, every 2 hrs etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timeout&lt;/strong&gt; - how long do we wait to get a response before calling it a failure. E.g. if a server normally responds in 50ms (min) to 5 sec (max), then a response taking 10 sec is alarming.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Threshold&lt;/strong&gt; - setting the limit for when to change the server's status.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unhealthy threshold&lt;/strong&gt; - after how many failed checks in a row we mark the server unhealthy and stop sending traffic to it. E.g. 3 failed checks in a row and the server is out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Healthy threshold&lt;/strong&gt; - after how many successful checks in a row an unhealthy server is considered healthy again and gets traffic back. E.g. n successful calls -&amp;gt; everything is going good, bring it back.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  DB Scaling - Deep Dive
&lt;/h2&gt;

&lt;p&gt;We know vertical scaling is good but it has its limit, after that we need to add more DBs. And we can do that in a better way too, using &lt;strong&gt;Replication&lt;/strong&gt; and &lt;strong&gt;Partitioning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgd4jghb9c5j4fvnj76id.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgd4jghb9c5j4fvnj76id.png" alt="Replication copies the data, partitioning splits it" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Replication
&lt;/h3&gt;

&lt;p&gt;Keeping copies of the same data on multiple DBs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoids Single Point Of Failure (SPOF) - one DB dies, others are still there&lt;/li&gt;
&lt;li&gt;Keeps data closer for faster retrieval&lt;/li&gt;
&lt;li&gt;Read throughput increases
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Single DB -&amp;gt; 10,000 reads/s
2 DBs     -&amp;gt; 10,000/s x 2
3 DBs     -&amp;gt; 10,000/s x 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Writes don't scale like this though, every write still has to reach every copy.&lt;/p&gt;
&lt;h3&gt;
  
  
  Replication Algos
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Single Leader Replication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One leader, n followers. The leader (L) takes all the writes and passes them on to the followers (F). Reads can be served by any of them.&lt;/p&gt;

&lt;p&gt;In this, the write operation can be done in two ways, sync and async.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvgnie3g2gdyxi32dthqm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvgnie3g2gdyxi32dthqm.png" alt="Single leader replication - sync vs async writes" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sync&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The leader waits till every follower confirms the write, then only says "done" to the user.&lt;/p&gt;

&lt;p&gt;Advantages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Followers are always updated&lt;/li&gt;
&lt;li&gt;If the leader fails, the followers still have up to date data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Longer response time - need to wait for the response from each follower DB&lt;/li&gt;
&lt;li&gt;Blocks resources&lt;/li&gt;
&lt;li&gt;Impractical - if one follower is slow or down, every write gets stuck&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Async&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The leader saves the write, says "done" to the user straight away, and updates the followers in the background.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No blocked resources&lt;/li&gt;
&lt;li&gt;Fast response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantage - mismatched data. A user may get stale data from a follower which isn't updated yet, while another user gets the updated data. (This delay is called &lt;em&gt;replication lag&lt;/em&gt;.)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In real life, many systems go in between - one follower is sync and the rest are async (&lt;em&gt;semi-sync&lt;/em&gt;), so at least one copy is always up to date.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;How to add a new follower&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6q32tlscrlnwanq7qcsi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6q32tlscrlnwanq7qcsi.png" alt="Adding a new follower using a snapshot and then catching up" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First create a snapshot of the current system, at that moment, with a timestamp.&lt;/li&gt;
&lt;li&gt;Use this snapshot to feed data into the new follower, assume it took 3 hours.&lt;/li&gt;
&lt;li&gt;In those 3 hours new data came in. So now get only the changes made after the first snapshot (the DB keeps a log of all changes, so we just replay it).&lt;/li&gt;
&lt;li&gt;Populate this data.&lt;/li&gt;
&lt;li&gt;Connect it with the actual system.&lt;/li&gt;
&lt;li&gt;Congrats, new follower added!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How to elect a leader if L fails&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check which follower has the latest data.&lt;/li&gt;
&lt;li&gt;Promote F1 as the leader and update F2 to see F1 as its leader.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(Also make sure the old leader, if it comes back, knows it's not the leader anymore, otherwise we get two leaders, called &lt;em&gt;split brain&lt;/em&gt;.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Multi Leader Replication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;More than one leader, usually one in each data center. Each leader first populates its own followers and the other leaders, which then populate their followers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff75c7kx055d5swad63gy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff75c7kx055d5swad63gy.png" alt="Multi leader replication across two data centers" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users write to the nearest data center, so writes are faster&lt;/li&gt;
&lt;li&gt;Multiple leaders = more fault tolerance&lt;/li&gt;
&lt;li&gt;Collaborative editing (like Google Docs)&lt;/li&gt;
&lt;li&gt;2 leaders can write on the same data in parallel, but this increases the chances of conflict.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To resolve conflicts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Last write wins&lt;/strong&gt; - the latest timestamp wins&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replica/Leader with the higher value assigned wins&lt;/strong&gt; - each leader gets an ID/priority, higher wins&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do nothing&lt;/strong&gt; - pass the conflict to the user and the user will pick the value (like git merge conflicts)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;3. Leaderless Replication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No leader. Every query (read/write) goes to multiple nodes to get the response. (Ex - Apache Cassandra, ScyllaDB, Riak.)&lt;/p&gt;

&lt;p&gt;Say User1 updates some data, DB1 and DB2 get it but DB3 misses it. Now when User2 reads, DB1 and DB2 give the updated data while DB3 gives stale data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4cyidvoss84msyqz0xeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4cyidvoss84msyqz0xeb.png" alt="Leaderless replication with a quorum" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So do users need to wait for the response from every DB??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nope. There are different algos and we can follow one of them. One of them is &lt;strong&gt;Quorum&lt;/strong&gt;. We wait only for enough DBs to make w + r &amp;gt; n (explained below), not all of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Servers  n = 3
Write    w = 2   (wait for 2 out of 3 to say ok)
Read     r = 2   (read from 2 out of 3)

Rule:  w + r &amp;gt; n   -&amp;gt;  2 + 2 &amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why does this work? Cause if 2 DBs have the new write and we read from 2 DBs, at least one of them will surely have the latest data.&lt;/p&gt;

&lt;p&gt;For a read, say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Response 1 = updated data&lt;/li&gt;
&lt;li&gt;Response 2 = stale data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every value carries a version number, so we pick the one with the latest version, and also fix the stale DB on the way (&lt;em&gt;read repair&lt;/em&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Partition (Sharding)
&lt;/h3&gt;

&lt;p&gt;Issue with replication:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What if the data itself is huge? Every copy still holds ALL of it.&lt;/li&gt;
&lt;li&gt;Time for searching/any other action keeps increasing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So here we divide the DB into smaller pieces, called partitions (also known as &lt;strong&gt;shards&lt;/strong&gt;), and each partition can then be replicated too.&lt;/p&gt;

&lt;p&gt;Only rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the partitioned DBs are connected together, they should give the entire data back.&lt;/li&gt;
&lt;li&gt;Data should be equally distributed. (One partition with 90% and the other with 10% is no good.)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Partition Algos
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr0mr3su3wra901qa1d8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr0mr3su3wra901qa1d8c.png" alt="Partitioning by key range vs by hash of the key" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Partition by 'key' (range)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Say we have 10,000 users. Users 1 to 5000 go to P1, and users 5001 to 10000 go to P2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hotspot&lt;/strong&gt; - a partition getting much more traffic/requests compared to the others, which may lead to node failure. E.g. if a big chunk of the newly active users fall in one range, that partition gets hammered while the other one chills.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Partitioning the data with 'hash' of key&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Based on the value of a hash function, the data is spread more randomly and evenly. U1 hashes to 1 so it goes to P1, U2 hashes to 2 so it goes to P2, U3 to P2, U4 to P1 and so on.&lt;/p&gt;

&lt;p&gt;Issue still persists, what if a single key becomes a hotspot (think a celebrity account with millions of followers)? Plus range queries like "users 1 to 100" now have to check every partition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Partition by Secondary Index (local index)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We often search by something other than the key, like "all users from India". So each partition maintains its own secondary index for its own data. Writes are fast (just update the local index), but a read has to ask every partition and combine the results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Common Global Index&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One global index covering the data of all partitions (and the index itself is partitioned).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better reading - we know exactly which partition to go to&lt;/li&gt;
&lt;li&gt;Slower writing - one write may need to update the index on another partition too&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  CAP Theorem
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;C - Consistency&lt;/strong&gt; - every read gets the latest data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A - Availability&lt;/strong&gt; - the system always responds to every request&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;P - Partition Tolerance&lt;/strong&gt; - the system keeps working even when the network breaks and some nodes can't talk to each other&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Careful, the "P" here is &lt;strong&gt;not&lt;/strong&gt; about partitioning/sharding our data like above. It's about a &lt;em&gt;network partition&lt;/em&gt;, when the connection between nodes breaks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;CAP theorem says that in a distributed system, we can only provide two among the three at a time.&lt;/p&gt;

&lt;p&gt;Combinations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;CA&lt;/strong&gt; - Consistency + Availability, no partition tolerance. Only really possible with a single DB, cause there is no network between nodes to break.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CP&lt;/strong&gt; - Consistency + Partition tolerance. When the connection breaks, the out of sync node stops serving (or the system waits) until it is restored and updated with the latest data. Users may get an error, but never wrong data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AP&lt;/strong&gt; - Availability + Partition tolerance. When the connection breaks, everyone keeps serving, so users may see stale or outdated data for a while.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdhbn22v1vankbk2oincu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdhbn22v1vankbk2oincu.png" alt="CAP theorem - what happens when the network between two DBs breaks" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AP = Instagram&lt;/strong&gt; - seeing a like count a few seconds late is totally fine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CP = Bank&lt;/strong&gt; - showing a wrong balance is NOT fine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CA = Small app&lt;/strong&gt; - single DB, no distribution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In reality, network failures will happen in any distributed system, so P is a must. The actual choice is between C and A when things break.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sync vs Async Requests
&lt;/h2&gt;

&lt;p&gt;Before message queues, we need to know that there are types in requests too, Sync and Async.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sync&lt;/strong&gt; - the client sends a request and waits till the response comes back. Used in finance apps, security checks, and dependencies where one service depends on the result of another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Async&lt;/strong&gt; - fire and forget. The client sends the request and moves on, the work happens in the background. Used for broadcasting, emails, SMS/WhatsApp notifications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example - E-commerce. Once a product is purchased, updating the inventory is sync (we must know the stock is updated), while sending the email and starting the delivery are async.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4piabd3xu0wfkmzbe8ls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4piabd3xu0wfkmzbe8ls.png" alt="Sync and async tasks after a purchase" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Message Queue
&lt;/h2&gt;

&lt;p&gt;What happens when you place an order on a food ordering app? It first takes your order, then sets it to preparing, then packaging, picked up, delivered etc. Or when you book a flight, you get a message, email, WhatsApp, saying thank you for choosing xyz airlines, here is your ticket. Ever wondered how this works? If each of these features is handled by the main server, it will put additional load on our servers, while these don't need to happen instantly.&lt;/p&gt;

&lt;p&gt;So we have a &lt;strong&gt;Message Queue (MQ)&lt;/strong&gt;. Once the ticket is booked, the server pushes the email, message and WhatsApp tasks into the message queue, from where they are picked up by their respective services and the task is done. These aren't as urgent as the booking itself, therefore they can be done asynchronously.&lt;/p&gt;

&lt;p&gt;A message queue is like a buffer placed between the app (backend) and a service, and it's async in nature. It's helpful as it allows the app to shed some load onto the queue. So in case a request fails, the queue handles it on its own (retries), otherwise the backend had to handle it, which would be an additional load on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F30jw3dkqt7l9opu3yg9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F30jw3dkqt7l9opu3yg9o.png" alt="Producer, message queue and consumer" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The one sending messages is the &lt;strong&gt;Producer/Publisher&lt;/strong&gt;, the queue in between is the &lt;strong&gt;Broker&lt;/strong&gt;, and the one picking them up is the &lt;strong&gt;Consumer/Subscriber&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ex - RabbitMQ, Apache Kafka, Amazon SQS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Qualities of MQ
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Can hold requests&lt;/li&gt;
&lt;li&gt;Communicates with services&lt;/li&gt;
&lt;li&gt;Handles failed calls&lt;/li&gt;
&lt;li&gt;Handles the load of services&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  MQ Algos
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2qb0arlqdpg6o2lbckqb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2qb0arlqdpg6o2lbckqb.png" alt="Message queue algorithms - strict FIFO, unordered, pull and push" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. FIFO&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict order&lt;/strong&gt; - messages are processed exactly in order. If one fails, it will not proceed, it will wait for its completion and then only move forward. Blocking in nature. 1 ✓, 2 ✓, 3 ✗... and the queue waits on 3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unordered queue&lt;/strong&gt; - skips the failed request and moves on to the next one, the failed one is retried later (or goes to the DLQ). 1 ✓, 2 ✓, 3 ✗ (skip), 4 ✓, 5 ✓.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Priority Queue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Attach a priority number with the call and higher priority ones get processed first. E.g. OTP SMS before a promotional email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Pull Based&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Requests are fed into the queue and the consumer pulls the requests on its own, whenever it's ready.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Push Based&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here the MQ pushes the request/call to the consumer as soon as it arrives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pub Sub Model
&lt;/h3&gt;

&lt;p&gt;In a normal queue, one message goes to one consumer. In &lt;strong&gt;Pub/Sub (Publish/Subscribe)&lt;/strong&gt;, publishers publish a message to a &lt;em&gt;topic&lt;/em&gt;, and &lt;strong&gt;every subscriber&lt;/strong&gt; of that topic gets its own copy. Subscribers can get it pushed or pull it themselves.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa6gyi1og2a7p0kdvodoj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa6gyi1og2a7p0kdvodoj.png" alt="Pub/Sub - every subscriber gets its own copy of the message" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ex - an "order placed" event, the email service, the inventory service and the analytics service all subscribe to it and each one does its own job.&lt;/p&gt;

&lt;h3&gt;
  
  
  DLQ - Dead Letter Queue
&lt;/h3&gt;

&lt;p&gt;Calls which couldn't be processed by the consumer, or had issues even after retries, are moved into a separate queue called the Dead Letter Queue. Later a log is given to the producer/developers for analysis of what went wrong and where. This way the bad message doesn't block the main queue, and it's not lost either.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases of MQ
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Async tasks&lt;/li&gt;
&lt;li&gt;Analysis / analytics events&lt;/li&gt;
&lt;li&gt;Load balancing / load levelling - absorbing sudden spikes so services work at their own pace&lt;/li&gt;
&lt;li&gt;Deferred systems

&lt;ul&gt;
&lt;li&gt;do them later&lt;/li&gt;
&lt;li&gt;scheduled processes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;strong&gt;not&lt;/strong&gt; to use it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't use it for a small app with low traffic, a queue is one more service to pay for, run and maintain.&lt;/li&gt;
&lt;li&gt;Real time stuff where the user is waiting for the result right now, big no. If we need an immediate result back from the other service, call it directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Monitoring and Logs
&lt;/h2&gt;

&lt;p&gt;As developers, our work doesn't stop when the project is deployed or handed over, we need to ensure that it's up and running all the time. For that we need logs to see where the issue is, so that we can recreate it and fix it. Creating the app is just step one, maintaining it is the longer journey.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fault/Error
&lt;/h3&gt;

&lt;p&gt;Any condition where the system isn't working the way it's supposed to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hardware based&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Server, DB, RAM failures&lt;/li&gt;
&lt;li&gt;Overheating&lt;/li&gt;
&lt;li&gt;Power supply&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Software issue&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Bad code&lt;/li&gt;
&lt;li&gt;Edge cases&lt;/li&gt;
&lt;li&gt;Config issue&lt;/li&gt;
&lt;li&gt;Deployment issue&lt;/li&gt;
&lt;li&gt;Merge requests/conflicts&lt;/li&gt;
&lt;li&gt;Latency&lt;/li&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Human fault&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Unpredictable&lt;/li&gt;
&lt;li&gt;Unreliable&lt;/li&gt;
&lt;li&gt;Yet this is our power too, which makes us different, humans learn from experience and improve.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Monitoring and Observability
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Monitoring&lt;/strong&gt; tells us &lt;em&gt;that&lt;/em&gt; something is wrong (CPU at 95%, errors increasing). &lt;strong&gt;Observability&lt;/strong&gt; helps us understand &lt;em&gt;why&lt;/em&gt; it's wrong, using the three pillars:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Logs&lt;/strong&gt; - what happened, line by line&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metrics&lt;/strong&gt; - numbers over time (CPU, latency, error rate)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traces&lt;/strong&gt; - the full journey of one request across all the services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Monitoring API&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Throughput&lt;/strong&gt; - how many requests the server is handling per second&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error codes&lt;/strong&gt; (response codes) - how many 4xx/5xx we are throwing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Health check&lt;/strong&gt; - passive or active&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Latency&lt;/strong&gt; - how long a response takes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Machine Monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CPU usage - 75% -&amp;gt; alert&lt;/li&gt;
&lt;li&gt;Memory - 90% -&amp;gt; scale up&lt;/li&gt;
&lt;li&gt;Disk I/O - near limit -&amp;gt; alert&lt;/li&gt;
&lt;li&gt;Network I/O&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What Type of Application Is It?
&lt;/h2&gt;

&lt;p&gt;Before jumping on to designing the system, we first have to understand what type of application it is. Is it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data Intensive&lt;/li&gt;
&lt;li&gt;Compute Intensive&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Data Intensive Applications
&lt;/h3&gt;

&lt;p&gt;Here more focus is on data, how to gather it, store it, move it, operate on it and how to present it. Imagine Instagram, it is filled with data, be it reels, messages, photos, comments etc. WhatsApp messages, banking transactions, analytical dashboards etc.&lt;/p&gt;

&lt;p&gt;From the point of technology, the main things we need to look at are the response from the database, network calls and server upgradation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Primary goal&lt;/strong&gt; - serve data from the DB to the user faster. Not much computation is required.&lt;/p&gt;

&lt;p&gt;Worries&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How fast can we read the data&lt;/li&gt;
&lt;li&gt;How safely are we going to store the data (privacy)&lt;/li&gt;
&lt;li&gt;How many users can access it simultaneously&lt;/li&gt;
&lt;li&gt;What happens when a machine dies&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most of the work happens on the server side, where the data lives:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;DB&lt;/li&gt;
&lt;li&gt;Sharding (partitioning)&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Replication&lt;/li&gt;
&lt;li&gt;Consistency&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Compute Intensive Applications
&lt;/h3&gt;

&lt;p&gt;Challenge - heavy computation. These applications are CPU/GPU bound (computation ↑, data ↓).&lt;br&gt;
Like - image processing, video rendering, ML model training, simulation, cryptography.&lt;/p&gt;

&lt;p&gt;Worries&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How fast can we compute&lt;/li&gt;
&lt;li&gt;Can we parallelize the work&lt;/li&gt;
&lt;li&gt;Reduce computational cost&lt;/li&gt;
&lt;li&gt;Can we use a GPU instead of a CPU&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Mixed Applications
&lt;/h3&gt;

&lt;p&gt;Currently we have mixed applications too, which rely on both, like YouTube:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data intensive&lt;/strong&gt; - serving videos to users&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compute intensive&lt;/strong&gt; - recommending videos&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt; - Ask, where is the time being lost? If time is lost in moving data, it's a data intensive feature. If time is lost in computation, it's a compute intensive feature.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Functional and Non-Functional Requirements
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Functional&lt;/strong&gt; - &lt;strong&gt;WHAT&lt;/strong&gt; the system should do. Product features, the requirement list, all the features/functions of the application. For example in Amazon:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User can register&lt;/li&gt;
&lt;li&gt;User can login&lt;/li&gt;
&lt;li&gt;User can see products&lt;/li&gt;
&lt;li&gt;Search products&lt;/li&gt;
&lt;li&gt;Filter products&lt;/li&gt;
&lt;li&gt;Add to cart, apply coupon, place order etc.&lt;/li&gt;
&lt;li&gt;Make payment&lt;/li&gt;
&lt;li&gt;Track order&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;2. Non-Functional&lt;/strong&gt; - &lt;strong&gt;HOW&lt;/strong&gt; the system should do it. Product properties, user expectations.&lt;/p&gt;

&lt;p&gt;Example of Amazon (assume 1 million users):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Should handle 1 million users&lt;/li&gt;
&lt;li&gt;Response time &amp;lt; 200ms&lt;/li&gt;
&lt;li&gt;99.9% availability&lt;/li&gt;
&lt;li&gt;Data encryption&lt;/li&gt;
&lt;li&gt;Scalable up to 10x traffic (sales)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Common non-functional requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt; - horizontal/vertical&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Availability&lt;/strong&gt; - uptime&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliability&lt;/strong&gt; - no data loss&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt; - latency&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt; - authentication (who are you), authorization (what are you allowed to do), encryption&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt; - code modularity&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability&lt;/strong&gt; - monitoring and logging
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Functional -&amp;gt; WHAT        Non-Functional -&amp;gt; HOW
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  DNS - Domain Name System
&lt;/h2&gt;

&lt;p&gt;Ever wondered what happens when you type &lt;a href="http://www.example.com" rel="noopener noreferrer"&gt;www.example.com&lt;/a&gt; in your browser and press enter? Computers don't understand names, they understand IP addresses (like &lt;code&gt;19.28.38.41&lt;/code&gt;). DNS is the phone book of the internet, it converts the name into the IP address. Explaining it in short:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ra2coyico2yqelhksut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ra2coyico2yqelhksut.png" alt="How DNS finds the IP address of a website" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First the browser checks its own cache, then the OS cache. If we visited the site recently, we already have the IP. Done, super fast.&lt;/li&gt;
&lt;li&gt;If not, the request goes to the &lt;strong&gt;DNS Resolver&lt;/strong&gt; (usually run by your ISP - Internet Service Provider, or something like Google's 8.8.8.8). It also has a cache, if the IP is there, it passes it back to the user.&lt;/li&gt;
&lt;li&gt;Otherwise, the resolver hits the &lt;strong&gt;Root Server&lt;/strong&gt;. There are 13 root server addresses in the world (with well over a thousand physical copies spread across the globe). The root doesn't know the IP, but it knows where the &lt;strong&gt;TLD - Top Level Domain&lt;/strong&gt; server for &lt;code&gt;.com&lt;/code&gt;, &lt;code&gt;.in&lt;/code&gt;, &lt;code&gt;.dev&lt;/code&gt;, &lt;code&gt;.org&lt;/code&gt; etc. is.&lt;/li&gt;
&lt;li&gt;Now we are redirected to the TLD server of the request, e.g. &lt;code&gt;.com&lt;/code&gt;. This TLD server gives us the address of the &lt;strong&gt;Authoritative Name Servers&lt;/strong&gt; of that website.&lt;/li&gt;
&lt;li&gt;The authoritative name server finally gives us the IP address of our required server/site.&lt;/li&gt;
&lt;li&gt;Now the request is made from the user to that IP address and the application is used. The resolver also caches it (for its TTL) for future use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If nothing is cached, all 6 steps can take tens to a few hundred milliseconds, but most of the time step 1 or 2 already has the answer, so it's almost instant.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cache levels
&lt;/h3&gt;

&lt;p&gt;Remember our store counter analogy from the cache layer? Caching isn't only between the server and the DB, it happens at several levels:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser level&lt;/li&gt;
&lt;li&gt;Operating System&lt;/li&gt;
&lt;li&gt;DNS Resolver (already looked into)&lt;/li&gt;
&lt;li&gt;Server side (Redis etc. between server and DB)&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  API - Application Programming Interface
&lt;/h2&gt;

&lt;p&gt;APIs are the bridge which connects the frontend with the backend (or one application with another), and using this the applications work in real time. Now there are two types of APIs based on who can use them:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb8b3t40558wbjt3l4zo9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb8b3t40558wbjt3l4zo9.png" alt="Public APIs vs private APIs" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Access to another (Public / Third party APIs)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You must have come across platforms like Google Gemini, ChatGPT where you saw developer APIs, or IMDb APIs. These are public APIs maintained by these companies, and other developers can use them. Like with IMDb APIs you can look for movies, their ratings, cast, duration and show them in your own app. For this feature, some companies charge an amount, some keep it free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Single App (Private / Internal APIs)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here the APIs are for the application only, no public access. Like your own backend serving your React website and your Swift iOS app.&lt;/p&gt;
&lt;h3&gt;
  
  
  Types of APIs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F260msiqogq80a8gpcvl0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F260msiqogq80a8gpcvl0.png" alt="REST, SOAP, GraphQL, gRPC and WebSockets compared" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. REST - Representational State Transfer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The most widely used API style. It's light, gives freedom and is very easy to work with. Most modern applications use this. It mostly uses the JSON format. (REST is actually a set of rules/style on top of HTTP, not a format, JSON is just the most common choice.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. SOAP - Simple Object Access Protocol&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It was widely used before JSON became popular and still a lot of legacy apps (old banking, enterprise systems) are using it. It uses XML, which is bulky in nature, and it has strict rules about everything from the header to the body. If an app wants to use these APIs, it has to handle the XML format, like a modern app consuming some old bank's APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. GraphQL - a query language for your API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This exposes a single API (endpoint) but a query language is attached. So the frontend asks for exactly the fields it needs. So instead of several APIs each responsible for a single task, we have a single API that does all of the work. We decide what data to choose and we get only that, nothing else. This saves bandwidth plus improves the response (no over-fetching or under-fetching).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. gRPC (Protocol Buffers)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;gRPC is developed by Google and uses &lt;strong&gt;Protocol Buffers&lt;/strong&gt; to send data. Protocol buffers are a binary format, smaller than JSON/XML, that's why they are faster to transfer.&lt;/p&gt;

&lt;p&gt;The downside - since it's binary, it's not human readable (you can't just open and read it like JSON), you need to define a schema (&lt;code&gt;.proto&lt;/code&gt; file) first, and browsers don't support it directly. Therefore it's not great for browser-facing APIs, but it is a great fit inside a microservice architecture. Imagine several microservices talking to each other all the time, they just need small information to work on, there protocol buffers shine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. WebSockets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Best option for real time and persistent connections. In normal APIs, the client asks and the server answers, that's it. In WebSockets, the connection stays open and both sides can send data anytime (bi-directional). Widely used in chat apps where a constant connection is required. Chatting, multiplayer gaming, live scores and the live chat on a stream need WebSockets for real time bi-directional connection. But it is expensive to use, as every open connection eats server resources, and we need additional components to keep it alive since it stays on all the time.&lt;/p&gt;
&lt;h2&gt;
  
  
  RESTful APIs
&lt;/h2&gt;

&lt;p&gt;Now we will be expanding on RESTful APIs as they are used widely in the industry. As mentioned, they use JSON to transfer data from one side to another. JSON holds data in key-value pairs, and a value can be a string, number, boolean, array or another object.&lt;/p&gt;

&lt;p&gt;An endpoint for an API consists of two things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Endpoint = Method + Path
           GET    + example.com/api/v1/resources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Methods in REST APIs
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt; - used to get data from the backend&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt; - used to send data from the frontend to create a new entry in the DB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt; - used to update data in the DB, this replaces the whole object with the data passed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PATCH&lt;/strong&gt; - also used to update, but this updates only the specified fields in the object&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DELETE&lt;/strong&gt; - used to delete the entry from the DB&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ways to Pass Data Through an Endpoint
&lt;/h3&gt;

&lt;p&gt;Now there is a need to pass data from the frontend to the backend, how can we do it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Path (Path Parameter)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can directly use the path to pass data to the backend.&lt;br&gt;
Example - &lt;code&gt;example.com/api/v1/user/{id}&lt;/code&gt;&lt;br&gt;
Here &lt;code&gt;id&lt;/code&gt; is the placeholder for the user's id. Then in the backend we can read the id and use it to retrieve the data from the DB.&lt;br&gt;
Good for - identifiers, like user id, item id, slugs. Also for clear relations by nesting, like &lt;code&gt;user/comments/{id}&lt;/code&gt; or &lt;code&gt;blog/{id}/comments&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Query Parameter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here also we pass the data in the URL, but it comes after a &lt;code&gt;?&lt;/code&gt;, like &lt;code&gt;example.com/blog?page=4&amp;amp;sort=asc&lt;/code&gt;.&lt;br&gt;
Good for - filtering, sorting, searching, pagination. Remember it's exposed in the URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Body of request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this we send the data inside the request body. The better option for big or sensitive data (passwords, forms etc.), it needs slight additional handling. It's not visible in the URL, so it doesn't end up in browser history or server logs. (The actual security comes from HTTPS, which encrypts the whole request.)&lt;/p&gt;
&lt;h3&gt;
  
  
  API Request Example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST          /api/v3/users                                     =&amp;gt; Method + URL
Content-Type: application/json                                  =&amp;gt; Meta data (headers)
Body:         {"name": "Testing", "last_name": "Testing2"}      =&amp;gt; Body
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  API Response Example from Backend
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Status Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It tells about the status of the request in a nutshell, like whether it was a success or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;2xx&lt;/strong&gt; - everything is fine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3xx&lt;/strong&gt; - just redirecting to another place&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;4xx&lt;/strong&gt; - something is wrong from your side (client)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;5xx&lt;/strong&gt; - something is wrong from the server side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;200&lt;/code&gt; - OK, success&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;201&lt;/code&gt; - Created (after a successful POST)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;400&lt;/code&gt; - Bad request&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;401&lt;/code&gt; - Unauthorized (who are you? login first)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;403&lt;/code&gt; - Forbidden (I know you, but you're not allowed)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;404&lt;/code&gt; - Not found&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;500&lt;/code&gt; - Internal server error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Body&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We also send a body in the response, that's how the frontend gets the data. We can send any type of data back, but a good rule of thumb is to &lt;strong&gt;always send the data wrapped inside an object&lt;/strong&gt;, don't directly return an array or a plain value. This way future changes are manageable, tomorrow if you need to add pagination info or a message, just add a new key without breaking the frontend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"users"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"value1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"value2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus - The Five Step Framework
&lt;/h2&gt;

&lt;p&gt;Whenever you sit to design any system (or face a system design interview), follow these 5 steps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fae7dz0z3raswymtyx9ps.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fae7dz0z3raswymtyx9ps.png" alt="The five step framework for designing any system" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clarify Requirements&lt;/strong&gt; - functional and non-functional, what are we building and for how many users?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data + APIs&lt;/strong&gt; - what data do we store (SQL/No-SQL) and what endpoints do we need?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High Level Architecture&lt;/strong&gt; - draw the boxes: frontend, LB, servers, cache, DB, MQ.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scale &amp;amp; Edge Cases&lt;/strong&gt; - replication, partitioning, caching, what if a server dies?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrap Up&lt;/strong&gt; - summarise the trade-offs you made and why.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;System design is the plan before the code, components + a common goal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database&lt;/strong&gt; - SQL for structure and transactions, No-SQL for scale and flexibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server&lt;/strong&gt; - the backbone, exposes the DB through APIs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frontend&lt;/strong&gt; - what the user sees and touches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache&lt;/strong&gt; - small, fast, saves the DB from repeated reads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load Balancer&lt;/strong&gt; - spreads requests across servers and keeps an eye on their health.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replication &amp;amp; Partitioning&lt;/strong&gt; - copy the data for availability, split it for size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CAP&lt;/strong&gt; - when the network breaks, choose consistency or availability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Message Queue&lt;/strong&gt; - do the not-so-urgent work later, asynchronously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring&lt;/strong&gt; - our work doesn't end at deployment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's System Design 101. Your next step: pick an app you use daily, like Instagram or Swiggy, and sketch which of these components it uses and where each one sits.&lt;/p&gt;

&lt;p&gt;I hope you found it helpful.&lt;br&gt;
Until next time.&lt;br&gt;
Peace ✌️&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>architecture</category>
    </item>
    <item>
      <title>AI 101 - A Beginner's Introduction to Artificial Intelligence</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Wed, 14 Oct 2026 17:42:47 +0000</pubDate>
      <link>https://dev.to/blackwatch021/ai-101-a-beginners-introduction-to-artificial-intelligence-3279</link>
      <guid>https://dev.to/blackwatch021/ai-101-a-beginners-introduction-to-artificial-intelligence-3279</guid>
      <description>&lt;p&gt;You have probably used AI today, maybe even before getting out of bed. Unlocked your phone with your face? That's AI. Opened YouTube and it showed you exactly the video you wanted to watch? AI again. AI is everywhere around us, but what actually is it?? Let's understand it from scratch, no fancy maths, no complex words.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Makes Humans Intelligent?
&lt;/h2&gt;

&lt;p&gt;There are many factors, but two important ones are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Autonomy&lt;/strong&gt; - Freedom to act and take our own decisions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adaptability&lt;/strong&gt; - Learning from experience and improving with time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Artificial Intelligence
&lt;/h2&gt;

&lt;p&gt;AI is the process of making computer programs intelligent, just like humans.&lt;/p&gt;

&lt;p&gt;Similarly, the things which make a program intelligent are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Autonomy&lt;/strong&gt; - The program's ability to assist or work independently, without someone telling it every single step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adaptability&lt;/strong&gt; - The program's ability to improve as it analyzes more data.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example - Keyboard suggestions (Predictive text)
&lt;/h3&gt;

&lt;p&gt;As we type, the keyboard suggests the most likely next words (working on its own) and keeps learning from each new sentence we type (adaptability). Ever noticed that after a while it starts suggesting your friend's nickname or the slang you use a lot? That's it learning from you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calculator
&lt;/h3&gt;

&lt;p&gt;Not an AI tech. It can't operate on its own and can't learn from the user's input, it only does what it is asked to do. 2 + 2 will always be 4, no matter how many times you ask it.&lt;/p&gt;

&lt;h2&gt;
  
  
  AI vs Machine Learning vs Deep Learning
&lt;/h2&gt;

&lt;p&gt;People use these words like they all mean the same thing, but they don't. Think of them as circles inside circles.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp90t8vbfml38gn4n7mez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp90t8vbfml38gn4n7mez.png" alt="Deep Learning inside Machine Learning inside AI" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AI&lt;/strong&gt; - The biggest circle. Any technique that makes machines act smart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Machine Learning (ML)&lt;/strong&gt; - A part of AI. Instead of telling the machine every rule, we let it learn from data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep Learning&lt;/strong&gt; - A part of ML that uses "neural networks", loosely inspired by how neurons in our brain are connected. It needs a lot of data and computing power, but it is behind things like face unlock, voice assistants and the AI chatbots we use today.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to go deeper into these layers, check out my article &lt;strong&gt;&lt;a href="https://dev.to/blackwatch021/the-5-layers-behind-every-ai-app-4l5a"&gt;Layers in AI&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How AI Can Be Useful for Us
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It can reduce our workload&lt;/li&gt;
&lt;li&gt;It can do our repetitive work for us&lt;/li&gt;
&lt;li&gt;It can improve the quality of our work&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Some real world uses
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Farmers&lt;/strong&gt; - Using AI based apps to check if their crops are healthy or not, or chatting with AI to improve their yields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Doctors&lt;/strong&gt; - AI helps in analyzing X-rays and scans, so doctors can find tiny signs of sickness or injury that might be hard to see.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  AI you already use (without even noticing)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuif4ybvvhfuwa20g6f4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuif4ybvvhfuwa20g6f4l.png" alt="AI you already use: recommendations, maps, face unlock, spam filter, chatbots" width="800" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Netflix / YouTube&lt;/strong&gt; - Recommending what to watch next&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Maps&lt;/strong&gt; - Predicting traffic and finding the fastest route&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Face unlock&lt;/strong&gt; - Recognizing your face to unlock your phone&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Email spam filter&lt;/strong&gt; - Keeping junk mail out of your inbox&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chatbots&lt;/strong&gt; - ChatGPT, Claude, Gemini etc. answering your questions&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; - AI is a tool, and it's on us how we use it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Algorithms
&lt;/h2&gt;

&lt;p&gt;Ever wondered how we can teach technology to learn new things?? We are trying to make machines like us by giving them intelligence, but still they can't learn just like us. We learn from experiences, from patterns, etc. Machines don't have real world experiences, so how do they learn? For that, we have algorithms.&lt;/p&gt;

&lt;p&gt;An algorithm, in a nutshell, is a set of step by step instructions to achieve or complete something.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example - Make me tea&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Boil the water&lt;/li&gt;
&lt;li&gt;Add sugar and tea&lt;/li&gt;
&lt;li&gt;Add milk&lt;/li&gt;
&lt;li&gt;Let it cook for 5 minutes&lt;/li&gt;
&lt;li&gt;Serve it hot&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See, this is an algorithm too, detailed step by step instructions to make tea. Similarly, we use algorithms to train our programs so that they can understand things.&lt;/p&gt;

&lt;h3&gt;
  
  
  Normal Programs vs Machine Learning
&lt;/h3&gt;

&lt;p&gt;Now here's the interesting part. In the tea example, we wrote every single step ourselves. That's how normal programs work, just like our calculator.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Normal program&lt;/strong&gt; - We write the rules → the computer follows them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Machine learning&lt;/strong&gt; - We give examples → the computer figures out the rules on its own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm2fgcsz9kgg95u3tau9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm2fgcsz9kgg95u3tau9n.png" alt="Normal program vs machine learning: who writes the rules?" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say we want a program to tell if an email is spam or not. With a normal program, we would have to write every rule ourselves, "if it says FREE MONEY, mark it spam", "if it has 20 links, mark it spam" and so on. But spammers keep coming up with new tricks, we can't write rules for all of them.&lt;/p&gt;

&lt;p&gt;With machine learning, we just show it thousands of emails marked as "spam" and "not spam", and it finds the patterns by itself. So here the algorithm's job isn't to make tea, it's to guide the machine on how to learn from the data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Parts of Machine Learning
&lt;/h2&gt;

&lt;p&gt;First let's understand how the human brain works. Let's take an example of &lt;strong&gt;"How you prepare for an examination"&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Brain&lt;/strong&gt; - The place where we store everything we learn.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notes&lt;/strong&gt; - We take notes for understanding and learning purposes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Studying&lt;/strong&gt; - Now we study those notes and start making connections between them, this is the adaptability we talked about.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prepared Brain&lt;/strong&gt; - Now the brain is trained to tackle most of the questions related to that subject.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Brain + Notes + Studying = Prepared Brain&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now coming to machine learning. Programs use mathematical instructions/operations to find patterns in data to make predictions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parts of Machine Learning
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Model (Brain)&lt;/strong&gt; - The storing and learning part of AI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data (Notes)&lt;/strong&gt; - The data given to the model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Training (Studying)&lt;/strong&gt; - The model analyzes the data to find patterns among them and learn from them. There are different ways through which AI learns, we will see them in a moment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trained Model (Prepared Brain)&lt;/strong&gt; - Now the model is ready to be used and make predictions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Model + Data + Training = Trained Model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjvqh8fnxi27jj3sporoc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjvqh8fnxi27jj3sporoc.png" alt="Exam preparation vs machine learning" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Training vs Using the Model
&lt;/h3&gt;

&lt;p&gt;Training is the studying part. It takes a lot of time, data and computing power, and happens once (or from time to time to update the model). Using the trained model to answer questions or do tasks is called &lt;strong&gt;inference&lt;/strong&gt;, it's like writing the exam. You don't re-study the whole book for every question, you just use what you already learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;p&gt;In terms of AI, and in humans too, features are pieces of information that help us tell things apart. Take a circle and a square for example. How do you know which one is which?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhw6wyzyr4lbvfet93ukl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhw6wyzyr4lbvfet93ukl.png" alt="Circle vs square: features tell things apart" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Circle&lt;/th&gt;
&lt;th&gt;Square&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Corners&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Straight sides&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Curved edge&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You never consciously think about this, your brain just does it. But a machine needs these features to tell them apart.&lt;/p&gt;

&lt;p&gt;Same goes for our spam example. Features of an email can be - does it contain "FREE MONEY", is the sender unknown, does it have too many links. The model looks at these features and learns which combination usually means spam. The better the features, the better the model will understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Learning
&lt;/h2&gt;

&lt;p&gt;Remember we said there are different ways through which AI learns? Here are the three main ones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp7lkfsb7knnoj099w2tj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp7lkfsb7knnoj099w2tj.png" alt="Three ways AI learns: supervised, unsupervised, reinforcement" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Supervised Learning - Learning with flashcards
&lt;/h3&gt;

&lt;p&gt;Remember the flashcards from childhood? A picture of an apple with "Apple" written below it. Here, the data comes with the answers (we call them &lt;strong&gt;labels&lt;/strong&gt;). For example, thousands of emails already marked as "spam" or "not spam". The model learns from these and then predicts for new emails.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Unsupervised Learning - Sorting a pile of Lego
&lt;/h3&gt;

&lt;p&gt;Imagine someone hands you a big pile of mixed Lego pieces and doesn't tell you anything. You will still start grouping them by color, size or shape on your own. Here the data has no labels, and the model finds groups and patterns by itself. For example, an online store grouping its customers by their shopping habits.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Reinforcement Learning - Training a dog with treats
&lt;/h3&gt;

&lt;p&gt;When a dog does the right thing, it gets a treat. When it does the wrong thing, no treat. Slowly it learns what to do. Same way, the AI tries something, gets a reward or a penalty, and improves by trial and error. This is how AI learns to play games like chess, and how robots learn to walk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will AI Always Give the Correct Answer??
&lt;/h2&gt;

&lt;p&gt;Now we can ask questions from the model or ask it to perform tasks. But the real question is, will it return the correct answer all the time??&lt;/p&gt;

&lt;p&gt;The answer is no. There is always a possibility of AI producing a wrong answer, and most of the time it is because of the data provided. That's why the more data, and of good quality, the better the accuracy of the model.&lt;/p&gt;

&lt;p&gt;Imagine a model trained &lt;strong&gt;only&lt;/strong&gt; on the English language, and users are asking questions in their native language, like Spanish, Chinese, Hindi or Japanese. Do you think it will be able to produce correct answers?&lt;/p&gt;

&lt;p&gt;Just for a moment, imagine yourself in this situation. Will you be able to answer a question asked to you in a language you don't understand??&lt;/p&gt;

&lt;p&gt;It's just like the old story of the blind men and the elephant. Each one touches only one part and is sure he knows the whole truth. One says it's a snake, one says it's a wall, one says it's a rope. AI is the same, it only knows what its data showed it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F06jdmcwu9avwzfzkdbfr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F06jdmcwu9avwzfzkdbfr.png" alt="The blind men and the elephant - Sketchplanations" width="637" height="542"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Image credit: &lt;a href="https://sketchplanations.com" rel="noopener noreferrer"&gt;Sketchplanations&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And sometimes AI makes mistakes very confidently. Chatbots can give an answer that sounds totally correct but is completely made up, like a fake fact or a book that doesn't even exist. This is called &lt;strong&gt;hallucination&lt;/strong&gt;. So remember, sounding confident doesn't mean being correct.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ssozs9q8luw5h26lv47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ssozs9q8luw5h26lv47.png" alt="What a hallucination looks like" width="799" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bias
&lt;/h2&gt;

&lt;p&gt;Remember how AI learns from the data we give it? So if the data is unfair, the AI will be unfair too. This is called &lt;strong&gt;bias&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Real example - Amazon once built an AI tool to screen job resumes. It was trained on resumes from the past 10 years, and most of those came from men, since the tech industry was mostly male. So the AI learned that male candidates are "better" and started ranking down resumes that had the word "women's" in them, like "women's chess club captain". Amazon had to scrap the tool.&lt;/p&gt;

&lt;p&gt;The AI wasn't evil, it just copied the pattern from the data it was given.&lt;/p&gt;

&lt;h2&gt;
  
  
  Safety and Responsibilities
&lt;/h2&gt;

&lt;p&gt;AI is now widely used and has become a part of our day to day life. But there are still risks in depending on it too much, because the possibility still exists that AI may not produce accurate results, which may lead to wrong decisions.&lt;/p&gt;

&lt;p&gt;Imagine a medical model isn't trained on enough data. It can create false emergencies, saying a patient is sick although the patient is fine.&lt;/p&gt;

&lt;p&gt;So it's always advised to check the AI's result ourselves, as a second check, to make sure no false result is delivered to the other side. There have been real cases of false arrests because of this. In 2020, Robert Williams in Detroit, USA was wrongly arrested because a facial recognition system matched his photo to a suspect's, and it was trusted without checking properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  When mistakes happen, who is responsible, the AI or humans?
&lt;/h3&gt;

&lt;p&gt;We need to understand one thing here, AI is just a tool, built by a human and used by a human. So ultimately the whole responsibility lies on humans. AI doesn't have feelings and emotions, it works on the data it is trained on. And who gives the data? Humans. Who uses the AI? Humans.&lt;/p&gt;

&lt;p&gt;So we are responsible for whatever happens because of AI.&lt;/p&gt;

&lt;h3&gt;
  
  
  So how can we reduce the harm?
&lt;/h3&gt;

&lt;p&gt;Now that's the question we need to look into. We can do it from two perspectives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Developer/Creator of AI tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As the creator of the tool, it's my responsibility to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Feed high quality and diverse data for the AI to find patterns and learn from. High quality data helps in understanding, while diverse data reduces bias, which is really important.&lt;/li&gt;
&lt;li&gt;Listen to users' feedback to improve on the go.&lt;/li&gt;
&lt;li&gt;Be honest about the limitations of the tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. User of AI tools&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not trust the tool blindly. Always double check the output, as it may produce wrong results.&lt;/li&gt;
&lt;li&gt;Always make sure you are using the tool for a good cause and not to harm others in any way.&lt;/li&gt;
&lt;li&gt;If you find any bug or any place for improvement, give feedback to the creator so they can improve it.&lt;/li&gt;
&lt;li&gt;Avoid sharing your personal details like passwords, bank details or ID numbers with AI tools, as your chats may be stored and even used to train the model.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common Myths About AI
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;"AI thinks and feels like humans"&lt;/strong&gt; - No. It finds patterns in data. It doesn't have feelings, and it doesn't understand things the way we do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"AI is always right"&lt;/strong&gt; - We just saw that it isn't. Wrong data, bias and hallucinations can all lead to wrong answers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"AI will replace all jobs"&lt;/strong&gt; - AI will change a lot of jobs and take over many repetitive tasks, but people who know how to use AI well will have a big advantage. Calculators didn't replace mathematicians.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"AI is something new"&lt;/strong&gt; - The term "Artificial Intelligence" was actually coined back in the 1950s. It just became popular recently because we now have a lot more data and computing power.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AI&lt;/strong&gt; - Making programs intelligent, with autonomy and adaptability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Machine Learning&lt;/strong&gt; - A part of AI where machines learn from data instead of fixed rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep Learning&lt;/strong&gt; - A part of ML that uses neural networks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Algorithm&lt;/strong&gt; - Step by step instructions to complete something.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model&lt;/strong&gt; - The "brain" that learns and makes predictions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Training&lt;/strong&gt; - The model studying the data to find patterns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inference&lt;/strong&gt; - Using the trained model to answer questions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Features&lt;/strong&gt; - Pieces of information that help tell things apart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Labels&lt;/strong&gt; - The correct answers attached to the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bias&lt;/strong&gt; - When AI becomes unfair because of unfair data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hallucination&lt;/strong&gt; - When AI confidently gives a made up answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;Now that you know the basics, you can check out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://dev.to/blackwatch021/the-5-layers-behind-every-ai-app-4l5a"&gt;The 5 Layers Behind Every AI App&lt;/a&gt;&lt;/strong&gt; - To understand the different layers of AI in depth.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://dev.to/blackwatch021/prompt-engineering-how-to-actually-get-what-you-want-from-ai-4e02"&gt;Prompt Engineering&lt;/a&gt;&lt;/strong&gt; - To learn how to talk to AI tools and get better answers from them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI is just a tool, a really powerful one. The more we understand it, the better we can use it, and the better we can make sure it's used for good.&lt;/p&gt;

&lt;p&gt;This was a small walkthrough of AI and how it works. I hope you found it helpful.&lt;/p&gt;

&lt;p&gt;Until next time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Peace ✌️&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>machinelearning</category>
      <category>learning</category>
    </item>
    <item>
      <title>The 5 Layers Behind Every AI App</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Wed, 30 Sep 2026 11:22:08 +0000</pubDate>
      <link>https://dev.to/blackwatch021/the-5-layers-behind-every-ai-app-4l5a</link>
      <guid>https://dev.to/blackwatch021/the-5-layers-behind-every-ai-app-4l5a</guid>
      <description>&lt;p&gt;It's been some time since the introduction of AI and now it has become an essential part of our life, isn't it?&lt;/p&gt;

&lt;p&gt;As a developer I can vouch for this. My day now starts with AI and ends with AI too 😁.&lt;/p&gt;

&lt;p&gt;AI gave us the power to be a one man army, to do things which typically needed a whole team in the past. Now we can do those things on our own. Just a subscription to an AI model and we are done.&lt;/p&gt;

&lt;p&gt;But things aren't this easy. To use AI to its full extent, we need to understand how it works.&lt;/p&gt;

&lt;p&gt;As a human, the best thing we can do is give a well defined prompt, informative enough for the AI to understand our requirement, and it's good to have some examples of what we expect.&lt;/p&gt;

&lt;p&gt;Then comes one of the most important parts in this, iterations. Even AI can miss some things, or what if you change your mind? That's why it's good to iterate until we reach the goal we set in our mind.&lt;/p&gt;

&lt;p&gt;To write an effective prompt I already wrote an article, &lt;a href="https://dev.to/blackwatch021/prompt-engineering-how-to-actually-get-what-you-want-from-ai-4e02"&gt;Prompt Engineering: How to Actually Get What You Want from AI&lt;/a&gt;. You can refer to this to improve your prompts.&lt;/p&gt;

&lt;p&gt;Now taking one step further. In this article we will dive into AI's layers and understand the stack behind it.&lt;/p&gt;

&lt;p&gt;One quick clarification before we start. I am not talking about the layers inside a neural network, the input and hidden and output ones. I am talking about the layers of the stack that every AI product you use is built on top of.&lt;/p&gt;

&lt;p&gt;So, majorly there are five layers behind the AI tools we use today. We will go top-down, starting with what you see and ending with what it all runs on.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gen AI Apps (Application Layer)&lt;/li&gt;
&lt;li&gt;Agent Layer&lt;/li&gt;
&lt;li&gt;Platform Layer&lt;/li&gt;
&lt;li&gt;Models Layer&lt;/li&gt;
&lt;li&gt;Infrastructure Layer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhcbtkb3fjcvxtl4ar9ju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhcbtkb3fjcvxtl4ar9ju.png" alt="The AI stack: Gen AI Apps, Agent Layer, Platform Layer, Models, and Infrastructure, from the user at the top down to the hardware" width="799" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now one layer at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Gen AI Apps or Application Layer
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;User centric apps which the user uses to interact and work with AI&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Have you worked with ChatGPT, Gemini, DeepSeek, Claude etc? Then most of the time you must have used some sort of app or the official website.&lt;/p&gt;

&lt;p&gt;Here is the thing though. You are not talking to the model directly. There is an app sitting in between, managing your chat history, rendering the response, handling your file uploads. The model itself has no idea what a conversation is.&lt;/p&gt;

&lt;p&gt;That app is the Gen AI app, and it is the layer the user directly interacts with.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Agent Layer
&lt;/h2&gt;

&lt;p&gt;This layer utilizes the capabilities of the model layer to perform more complex actions. Think of them as digital workers with a brain.&lt;/p&gt;

&lt;p&gt;You specify a task to them, they gather information from the user, interact with the environment to gather more information, make decisions, and execute actions based on the info received.&lt;/p&gt;

&lt;p&gt;They differ from simple AI in the fact that they can actually do the work. Like searching on the internet to look for the best restaurant, checking your schedule for free time, booking the table, and sending a mail or message to the other party for the invitation. Rather than just giving you the steps, it actually does the work.&lt;/p&gt;

&lt;p&gt;This is the agent layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Platform Layer
&lt;/h2&gt;

&lt;p&gt;This is one of the most important layers in AI. It acts as a bridge between the models and the layers above.&lt;/p&gt;

&lt;p&gt;Here is the problem it solves. A trained model on its own is just a huge file of numbers sitting on some machine. It has no way for you to reach it, no idea who you are, and no way to handle a thousand people asking it things at the same time.&lt;/p&gt;

&lt;p&gt;The platform layer is everything that turns that file into something an app can actually call.&lt;/p&gt;

&lt;p&gt;So what does it actually do?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;APIs&lt;/strong&gt; — it gives you an endpoint to hit. This is the thing that makes a model usable from your code instead of just from a website.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication&lt;/strong&gt; — it checks who you are. This is where your API key comes in, and it is also how the provider knows which account to bill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate limits and quotas&lt;/strong&gt; — it decides how much you are allowed to use, and how fast. Anyone who has seen a &lt;code&gt;429&lt;/code&gt; error while building with AI has met this part of the layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Routing and scaling&lt;/strong&gt; — your request has to land on a machine that has a free GPU. The platform picks one, sends your request there, and spins up more machines when traffic goes up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring and logs&lt;/strong&gt; — it keeps the record. How many tokens you used, how long the request took, what failed and why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extra tooling&lt;/strong&gt; — most platforms also give you the surrounding pieces, like vector databases for storing your own data, guardrails to filter unwanted output, and tools to evaluate or fine tune a model.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  An example
&lt;/h3&gt;

&lt;p&gt;Remember the restaurant agent from the last layer? Let's see what the platform does while that agent is working.&lt;/p&gt;

&lt;p&gt;The agent decides it needs to think about your request, so it sends it to a model. That request hits an API endpoint. The platform checks your API key, confirms your account is allowed to use that model, and checks you haven't crossed your rate limit.&lt;/p&gt;

&lt;p&gt;Then it finds a machine with a free GPU and sends your request there. The model does its thing and the answer comes back. On the way out, the platform counts the tokens you used, writes it to your usage log, and bills your account.&lt;/p&gt;

&lt;p&gt;The agent gets its answer and moves on to the next step. All of that happened in between, and neither you nor the agent had to think about it.&lt;/p&gt;

&lt;p&gt;Some platforms you may have heard of are Amazon Bedrock, Google Vertex AI and Microsoft Foundry, along with the APIs that the model providers run themselves.&lt;/p&gt;

&lt;p&gt;If you have ever built a backend, this layer will feel familiar. It is the same set of concerns you already deal with every day, auth, rate limiting, scaling, logging. The only difference is that there is a model at the other end instead of a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Models
&lt;/h2&gt;

&lt;p&gt;The elephant in the room, the brain of any AI. These give the power to AI to do the things that they are able to do nowadays, from simple conversations to generating videos.&lt;/p&gt;

&lt;p&gt;There are different types of models as per the user's requirement.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LLMs (Large Language Models)&lt;/strong&gt; — good for conversations or text based things. From translation to creating mails for you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Diffusion Models&lt;/strong&gt; — LLMs are good for text, meanwhile diffusion models are for generating new media. They start from random noise and refine it step by step until an image or a video comes out.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Infrastructure Layer
&lt;/h2&gt;

&lt;p&gt;We talked about most of the things, but all these things need infrastructure to run.&lt;/p&gt;

&lt;p&gt;That physical part comes under this layer, from CPUs to GPUs to RAM and networking.&lt;/p&gt;

&lt;p&gt;We can blame this layer for the increasing RAM and SSD prices currently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So the next time you type something into a chat box, remember how much is sitting underneath it. Your message passes through the app, the agent and the platform, and then it gets turned into actual math running on actual hardware.&lt;/p&gt;

&lt;p&gt;Five layers. The model gets all the attention, but it is only one of them.&lt;/p&gt;

&lt;p&gt;This is a small guide on the layers behind AI. I hope you found it helpful.&lt;/p&gt;

&lt;p&gt;Until next time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Peace ✌️&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>programming</category>
      <category>agents</category>
    </item>
    <item>
      <title>Prompt Engineering: How to Actually Get What You Want from AI</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Tue, 08 Sep 2026 15:31:56 +0000</pubDate>
      <link>https://dev.to/blackwatch021/prompt-engineering-how-to-actually-get-what-you-want-from-ai-4e02</link>
      <guid>https://dev.to/blackwatch021/prompt-engineering-how-to-actually-get-what-you-want-from-ai-4e02</guid>
      <description>&lt;p&gt;You ask an AI to help you prepare for an interview. You get back twenty generic questions you could have found on any blog.&lt;/p&gt;

&lt;p&gt;You ask again. This time you mention that you are an entry-level developer, that you know the MERN stack, and that you want the answers in a table. Suddenly it is useful.&lt;/p&gt;

&lt;p&gt;Same model. Same knowledge. Different prompt.&lt;/p&gt;

&lt;p&gt;That gap, between the generic answer and the useful one, is what prompt engineering is about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prompts, and why they matter
&lt;/h2&gt;

&lt;p&gt;AI has been with us far longer than most people realise. Ever wondered how your mobile keyboard suggests the next word while you are typing? That is AI too, just a very narrow use case of it.&lt;/p&gt;

&lt;p&gt;Compare that with today's AI, which can answer our day-to-day questions, help with our tasks, generate articles, taglines and summaries, and even produce images and video, all from a simple text instruction.&lt;/p&gt;

&lt;p&gt;That instruction is known as a &lt;strong&gt;prompt&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now take a child as an example. When they are young, they speak gibberish which is hard to understand. But as they grow up, they communicate better, and it becomes easier to understand what they want.&lt;/p&gt;

&lt;p&gt;A similar idea applies to Generative AI. If our instructions are unclear, the AI may misunderstand our requirement and produce a result that is off-target, irrelevant, or incomplete.&lt;/p&gt;

&lt;p&gt;So how do we communicate with AI more effectively?&lt;/p&gt;

&lt;p&gt;That is where &lt;strong&gt;prompt engineering&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Prompt Engineering?
&lt;/h2&gt;

&lt;p&gt;Prompt engineering is a method of writing prompts in a structured manner so that we provide the AI with the required information, relevant context, examples, and clear instructions.&lt;/p&gt;

&lt;p&gt;This helps the AI generate a result that is closer to our expectations.&lt;/p&gt;

&lt;p&gt;We can also use &lt;strong&gt;prompt chaining&lt;/strong&gt;, where we break a big task into steps and feed the output of one prompt into the next.&lt;/p&gt;

&lt;p&gt;Although there isn't one specific rule for creating the perfect prompt, a structure taught in Google's prompting course that may be useful is &lt;strong&gt;TCREI&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;T — Task&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;C — Context&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;R — References&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;E — Evaluate&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;I — Iterate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjtr0wdfiihheo31l0f0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjtr0wdfiihheo31l0f0m.png" alt=" " width="799" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand each of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Task
&lt;/h2&gt;

&lt;p&gt;This is where we specify the task we want the AI to perform.&lt;/p&gt;

&lt;p&gt;It can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate a report&lt;/li&gt;
&lt;li&gt;Create a tagline&lt;/li&gt;
&lt;li&gt;Analyze data&lt;/li&gt;
&lt;li&gt;Write an email&lt;/li&gt;
&lt;li&gt;Create a presentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The task is the main instruction, the one thing you want the AI to do.&lt;/p&gt;

&lt;p&gt;It can also have two additional parts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Persona
&lt;/h3&gt;

&lt;p&gt;Here we associate a role or personality with the AI, so that it approaches the task from that perspective.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Assume you are a software developer with 15 years of experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Assume you are a store manager with 10 years of experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A persona reliably changes &lt;em&gt;how&lt;/em&gt; the answer is written: the vocabulary, the depth, and what the model assumes you already know. "Explain this as a senior developer would" and "explain this as a teacher would" produce genuinely different answers.&lt;/p&gt;

&lt;p&gt;What a persona does not do is make the model &lt;em&gt;know&lt;/em&gt; more. "You are an expert cardiologist" does not add medical knowledge it did not already have. Use a persona to shape the framing, not to buy accuracy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Format
&lt;/h3&gt;

&lt;p&gt;It is also a good idea to specify the format in which we want the result.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Give me a table with five points.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Provide the answer in three paragraphs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Format the output as CSV with the columns: Question, Topic, Difficulty, Answer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This helps us get an output that is more useful and closer to what we actually need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;Context is important for AI because it helps create the background related to the task.&lt;/p&gt;

&lt;p&gt;It is just like the background we provide before narrating a story to someone.&lt;/p&gt;

&lt;p&gt;Imagine this prompt:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Analyze the given data and generate a profit and loss summary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Good. But it only tells the AI what to do. It doesn't tell it much about the situation.&lt;/p&gt;

&lt;p&gt;Now consider this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here is a company's sales data for the last year, including revenue, purchases, salaries, and other operating expenses. Analyze it and give me a profit and loss summary as a table.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we have also provided a background related to the task. This makes the requirement more specific and gives the AI additional information it can use.&lt;/p&gt;

&lt;p&gt;Prompt 2 gives the AI something to work with. Prompt 1 makes it guess.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;"References" actually covers two different things, and they are worth separating.&lt;/p&gt;

&lt;p&gt;One is &lt;strong&gt;examples&lt;/strong&gt;, where we show the pattern we want. The other is &lt;strong&gt;source material&lt;/strong&gt;, where we paste in the actual document, spec, or data the answer should come from.&lt;/p&gt;

&lt;p&gt;The second one is powerful. The model cannot make up an answer if you hand it the real one.&lt;/p&gt;

&lt;p&gt;Let's look at examples first, since that is where most of the technique lives.&lt;/p&gt;

&lt;p&gt;Instead of simply saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Explain React.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can provide an example of the expected format:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Define React → Answer&lt;br&gt;
How React works → Answer&lt;br&gt;
What is ES6 → Answer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now the AI has a reference for how we want the output to look.&lt;/p&gt;

&lt;p&gt;There are different variants of prompting based on how many examples we provide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zero-Shot Prompting
&lt;/h3&gt;

&lt;p&gt;In this approach, we don't provide any example. The AI has to rely on its pre-trained knowledge and the instruction we gave it.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Explain what React is in simple terms.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is no example here.&lt;/p&gt;

&lt;p&gt;Zero-shot works well for simple, direct questions and everyday tasks. When the output isn't coming back in the shape you want, that is the signal to start adding examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  One-Shot Prompting
&lt;/h3&gt;

&lt;p&gt;In this approach, we provide &lt;strong&gt;one example&lt;/strong&gt; in the prompt. The example acts as a blueprint for the desired output.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Convert the following sentence into a professional tone.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
"Hey, send me the report ASAP." → "Please share the report at your earliest convenience."&lt;/p&gt;

&lt;p&gt;Now convert:&lt;br&gt;
"Hey, fix this issue quickly."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here the AI can understand the expected transformation from the single example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Few-Shot Prompting
&lt;/h3&gt;

&lt;p&gt;In this case we provide multiple examples so that the AI can identify the pattern and generate the expected output.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React → Frontend Library&lt;br&gt;
Express → Backend Framework&lt;br&gt;
MongoDB → Database&lt;br&gt;
Node.js → ?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The AI picks up the pattern and answers "JavaScript Runtime".&lt;/p&gt;

&lt;p&gt;Though watch out for something here. My three examples were all library, framework and database, so a model following that pattern too literally might answer "Backend Framework", which is wrong. Node.js is a runtime.&lt;/p&gt;

&lt;p&gt;That is the real risk with few-shot: examples don't just show the format, they &lt;strong&gt;bias the answer&lt;/strong&gt;. Choose examples that cover the range of answers you actually expect.&lt;/p&gt;

&lt;p&gt;One more thing on quantity. Providing irrelevant or contradictory information hurts, because the model has no way to know which parts to trust. But &lt;em&gt;good&lt;/em&gt; examples generally don't. The old advice was "two to five examples, no more", and that was reasonable when context windows were small. With today's much larger context windows, more consistent examples usually help rather than hurt.&lt;/p&gt;

&lt;p&gt;So the goal is not fewer examples, and not more examples. It is &lt;strong&gt;consistent, relevant&lt;/strong&gt; examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evaluate
&lt;/h2&gt;

&lt;p&gt;By this point we have the output generated by the AI.&lt;/p&gt;

&lt;p&gt;Now our task is to evaluate whether it meets our requirements and follows the instructions in the prompt.&lt;/p&gt;

&lt;p&gt;This is a crucial stage. We should check things like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Did the AI perform the correct task?&lt;br&gt;
Did it follow the requested format?&lt;br&gt;
Is the information relevant?&lt;br&gt;
Is anything missing?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And the one that matters most:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Is it actually correct?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A well-formatted, on-topic, confident answer can still be wrong. If the output contains facts, figures, version numbers, or API details you are going to rely on, check them somewhere else.&lt;/p&gt;

&lt;p&gt;This evaluation becomes especially important when we continue the conversation using the generated result as context. If we don't identify problems early, we end up building on an incorrect result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterate
&lt;/h2&gt;

&lt;p&gt;Now we have evaluated our result. If the result is what we wanted, we are done.&lt;/p&gt;

&lt;p&gt;But if something went wrong, this is where we correct the AI. We tell it what went wrong and what we want instead.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The format is correct, but you included unnecessary technical details. Keep the explanation beginner-friendly and limit it to five points.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This also gives us an opportunity to improve our own prompt, because sometimes the problem is not with the AI. Sometimes we simply didn't provide enough information, context, or clear instructions in the first place.&lt;/p&gt;

&lt;p&gt;So prompt engineering is also an &lt;strong&gt;iterative process&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Practical Example
&lt;/h2&gt;

&lt;p&gt;Let's put everything together.&lt;/p&gt;

&lt;p&gt;Suppose I am preparing for a frontend interview. I could give the AI a prompt like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are a senior software developer with 10 years of experience.&lt;/p&gt;

&lt;p&gt;I want you to help me prepare for my upcoming frontend interview. I am an entry-level developer with MERN stack experience and basic AWS knowledge.&lt;/p&gt;

&lt;p&gt;List the questions I am most likely to be asked based on current frontend interview trends.&lt;/p&gt;

&lt;p&gt;Format the output as CSV with the columns: Question, Topic, Difficulty, Answer.&lt;/p&gt;

&lt;p&gt;Use the following examples to understand the type of questions I am looking for:&lt;/p&gt;

&lt;p&gt;Define React → Answer&lt;br&gt;
How does React work? → Answer&lt;br&gt;
What is ES6? → Answer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now look at what we have included:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Persona:&lt;/strong&gt; Senior software developer with 10 years of experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Help with frontend interview preparation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Entry-level, MERN expertise, basic AWS knowledge.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Format:&lt;/strong&gt; CSV with specific columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt; Examples of the expected questions.&lt;/p&gt;

&lt;p&gt;Now notice one weak spot in my own prompt. I asked for "current trends". Unless the tool I am using has web search turned on, the model has no idea what is current. It will produce something plausible from its training data and present it confidently.&lt;/p&gt;

&lt;p&gt;This is exactly the EVALUATE step doing real work. I should either turn on web search, paste in a few recent job descriptions as source material, or drop the word "current" and accept that I am getting general fundamentals.&lt;/p&gt;

&lt;p&gt;That is the whole idea.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Few More Things That Help
&lt;/h2&gt;

&lt;p&gt;The TCREI structure covers the bulk of it, but a few smaller habits make a noticeable difference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Separate your instructions from your data.&lt;/strong&gt; If you are pasting in a long document, wrap it so the model knows where your instruction ends and the content begins:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Summarize the document below in five bullet points.&lt;/p&gt;

&lt;p&gt;--- DOCUMENT ---&lt;br&gt;
(paste here)&lt;br&gt;
--- END DOCUMENT ---&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without a separator, the model can read your pasted content as part of the instruction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Say what you want, not what you don't.&lt;/strong&gt; "Don't use jargon" performs worse than "use plain language a first-year student would understand." Positive framing consistently beats negative framing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Think step by step" is no longer a universal trick.&lt;/strong&gt; It used to be the single most-cited prompting tip. On today's reasoning models, which already work through problems internally, it is often redundant and can occasionally make things worse by forcing a rigid structure. Try the prompt without it first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Prompt engineering is not about writing extremely complicated prompts.&lt;/p&gt;

&lt;p&gt;It is about &lt;strong&gt;communicating clearly with AI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The more clearly we communicate our task, context, references, expectations, and desired format, the easier it becomes for the AI to produce a useful result.&lt;/p&gt;

&lt;p&gt;TCREI is one framework that can help us structure that communication:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task → Context → References → Evaluate → Iterate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And whatever the prompt, always verify the parts you are going to rely on.&lt;/p&gt;

&lt;p&gt;This is a small guide on prompt engineering. I hope you found it helpful.&lt;/p&gt;

&lt;p&gt;Until next time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Peace ✌️&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>promptengineering</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Top Frequently Used AWS Services: A Beginner-Friendly Guide</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Sun, 09 Aug 2026 12:02:31 +0000</pubDate>
      <link>https://dev.to/blackwatch021/top-frequently-used-aws-services-a-beginner-friendly-guide-5df1</link>
      <guid>https://dev.to/blackwatch021/top-frequently-used-aws-services-a-beginner-friendly-guide-5df1</guid>
      <description>&lt;p&gt;AWS is one of the most popular cloud platforms out there and is used widely. The thing is, AWS comes with &lt;strong&gt;200+ services&lt;/strong&gt;, and knowing all of them is quite a big task on its own.&lt;/p&gt;

&lt;p&gt;But for the majority of users, they are only going to use a handful of services most of the time. The aim of this article is to give a brief idea about those services.&lt;/p&gt;

&lt;p&gt;In case you are already a champ in AWS, then you can also use this as a quick revision.&lt;/p&gt;




&lt;p&gt;Once you log in to your AWS account, you will see a dashboard looking like a &lt;strong&gt;space shuttle operating system&lt;/strong&gt;. So many things to look at.&lt;/p&gt;

&lt;p&gt;But trust me on this one, you are majorly going to use the &lt;strong&gt;SEARCH&lt;/strong&gt; bar to navigate through different services.&lt;/p&gt;

&lt;p&gt;On the same nav bar, you will see &lt;strong&gt;REGION&lt;/strong&gt;, which is a drop-down list of regions available to choose from.&lt;/p&gt;

&lt;p&gt;These are actual AWS regions. Assuming you created some service in &lt;strong&gt;Region A&lt;/strong&gt;, then you have to select that region to work with it. If you choose any other region, that service won't show up there.&lt;/p&gt;

&lt;p&gt;So, make sure you remember which regions you are working in.&lt;/p&gt;

&lt;p&gt;Otherwise, it may happen that you spin up a service in Region A and completely forget about it.&lt;/p&gt;

&lt;p&gt;But the AWS billing team will &lt;strong&gt;not&lt;/strong&gt; forget it. They will still charge you for it. 🙂&lt;/p&gt;

&lt;p&gt;Now let's start with our services.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. IAM — Identity and Access Management
&lt;/h2&gt;

&lt;p&gt;Imagine you are running a team of 20 people, including interns.&lt;/p&gt;

&lt;p&gt;Now you want to give access to your company's AWS account to other users. Here you can't just pass down your &lt;strong&gt;root credentials&lt;/strong&gt;, because then that person could potentially have full access and authority to do whatever they want.&lt;/p&gt;

&lt;p&gt;For that, we have &lt;strong&gt;IAM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;IAM is used to control &lt;strong&gt;who can access your AWS resources and what they are allowed to do&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here we can work with &lt;strong&gt;users, groups, roles and policies&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, I don't want an intern to have permission to manage a database. But I want them to access database logs.&lt;/p&gt;

&lt;p&gt;So, I can assign the appropriate permissions/policies to that user or group.&lt;/p&gt;

&lt;p&gt;Now I don't have to worry about them accidentally doing something they shouldn't.&lt;/p&gt;

&lt;p&gt;Even if you are working on a solo project, you should &lt;strong&gt;avoid using the root account for everyday work&lt;/strong&gt; and use appropriate IAM access instead.&lt;/p&gt;

&lt;p&gt;AWS permissions can get complicated very quickly, but the basic idea is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IAM = Who can do what?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. EC2 — Elastic Compute Cloud
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Virtual servers in the cloud."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In any cloud, we need servers where we can do our tasks. It can be running something on the cloud, deploying projects, hosting an API, running a backend, etc.&lt;/p&gt;

&lt;p&gt;These are virtual machines and AWS calls them &lt;strong&gt;EC2 instances&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can create multiple EC2 instances, subject to AWS service quotas. And remember, these are &lt;strong&gt;billed resources&lt;/strong&gt;; they aren't just free machines sitting there waiting for you.&lt;/p&gt;

&lt;p&gt;If you are eligible for a free tier offer, some instance types/usage may be covered depending on the current AWS terms.&lt;/p&gt;

&lt;p&gt;The good thing is the flexibility AWS provides.&lt;/p&gt;

&lt;p&gt;From choosing the operating system to choosing the type of machine, storage, networking and other configurations, we get a lot of control.&lt;/p&gt;

&lt;p&gt;These machines come in different sizes and configurations. From a small machine enough to run a simple application to powerful CPU/GPU instances for demanding workloads.&lt;/p&gt;

&lt;p&gt;Then we can configure the storage as per our needs.&lt;/p&gt;

&lt;p&gt;If you aren't using a free offering and your instance is billable, &lt;strong&gt;running the instance can incur compute charges based on the instance type and pricing model you choose.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Security Groups
&lt;/h3&gt;

&lt;p&gt;Now comes the concept of &lt;strong&gt;Security Groups&lt;/strong&gt; for EC2 instances.&lt;/p&gt;

&lt;p&gt;Here we can specify which ports and traffic are allowed to reach our instance and which outbound traffic is allowed.&lt;/p&gt;

&lt;p&gt;It works somewhat like a &lt;strong&gt;firewall for our instance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, we might allow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Port &lt;code&gt;22&lt;/code&gt; for SSH&lt;/li&gt;
&lt;li&gt;Port &lt;code&gt;80&lt;/code&gt; for HTTP&lt;/li&gt;
&lt;li&gt;Port &lt;code&gt;443&lt;/code&gt; for HTTPS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And block everything else that isn't needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Elastic IP
&lt;/h3&gt;

&lt;p&gt;The moment an EC2 instance is spun up, it can have a public IPv4 address through which it can be accessed.&lt;/p&gt;

&lt;p&gt;Now the thing is, the public IPv4 address can change when the instance is stopped and started.&lt;/p&gt;

&lt;p&gt;For that, we have &lt;strong&gt;Elastic IP&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It provides a static public IPv4 address that we can associate with an AWS resource.&lt;/p&gt;

&lt;p&gt;But there is one important thing to keep in mind:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS charges for public IPv4 addresses&lt;/strong&gt;, including public IPv4 addresses associated with resources. So don't allocate addresses you don't actually need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Load Balancer
&lt;/h3&gt;

&lt;p&gt;Imagine we have several EC2 machines running, but users' requests are hitting only one machine.&lt;/p&gt;

&lt;p&gt;This will create issues because each machine has limited capacity to cater to users. All requests hitting a single machine can overwhelm it and the machine may eventually crash.&lt;/p&gt;

&lt;p&gt;For that, we have a &lt;strong&gt;Load Balancer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Its job is to distribute incoming traffic across multiple healthy machines according to the configured routing rules.&lt;/p&gt;

&lt;p&gt;So no single machine has to do all the heavy lifting.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Users
                  |
            Load Balancer
             /     |     \
            /      |      \
         EC2 #1  EC2 #2  EC2 #3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Auto Scaling
&lt;/h3&gt;

&lt;p&gt;Now imagine only 5 machines are being used for a project.&lt;/p&gt;

&lt;p&gt;But suddenly traffic increases.&lt;/p&gt;

&lt;p&gt;If the existing machines aren't enough to handle the extra requests, &lt;strong&gt;Auto Scaling&lt;/strong&gt; can automatically launch additional EC2 instances according to predefined scaling policies.&lt;/p&gt;

&lt;p&gt;And when the load decreases, it can reduce the number of instances again.&lt;/p&gt;

&lt;p&gt;The important part is that we have to &lt;strong&gt;define these scaling rules beforehand&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If average CPU usage goes above 70%, add more instances.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And when the traffic comes back down:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the load stays low, remove some instances.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The best thing about EC2 is the flexibility given to the user to create and configure their own machines.&lt;/p&gt;

&lt;p&gt;From operating system to storage to networking and security, there is a lot in the user's hands.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. S3 — Simple Storage Service
&lt;/h2&gt;

&lt;p&gt;S3 provides a storage solution on the cloud provided by AWS.&lt;/p&gt;

&lt;p&gt;Now we have deployed our project on EC2, but where will we store our data?&lt;/p&gt;

&lt;p&gt;On the same EC2 machine?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For that, we have &lt;strong&gt;S3 buckets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of S3 as highly scalable &lt;strong&gt;object storage&lt;/strong&gt; on the cloud.&lt;/p&gt;

&lt;p&gt;We can manually store data in it or we can do it programmatically too.&lt;/p&gt;

&lt;p&gt;We can store things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;li&gt;Videos&lt;/li&gt;
&lt;li&gt;PDFs&lt;/li&gt;
&lt;li&gt;Backups&lt;/li&gt;
&lt;li&gt;Logs&lt;/li&gt;
&lt;li&gt;Application files&lt;/li&gt;
&lt;li&gt;Static website assets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also control who can access the objects using IAM policies, bucket policies, access controls and other mechanisms.&lt;/p&gt;

&lt;p&gt;For example, an object could be private and accessible only through authenticated access or a &lt;strong&gt;pre-signed URL&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One important thing to remember:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a bucket doesn't mean you're suddenly paying for a giant storage drive.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You are primarily charged based on things such as the amount of data stored and requests/data transfer depending on the usage and storage class.&lt;/p&gt;

&lt;p&gt;The beauty of S3 is that it is highly scalable and designed to store huge amounts of data.&lt;/p&gt;

&lt;p&gt;Also, the name of a general-purpose S3 bucket has to be &lt;strong&gt;globally unique within the AWS partition&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;No two general-purpose buckets can have the same name in that namespace, even if they are in different AWS regions.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. ECR — Elastic Container Registry
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite — Docker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since Docker is now widely used in the development world, and it makes sense too because it makes development and deployment relatively easy.&lt;/p&gt;

&lt;p&gt;Now, if you have knowledge of Docker, then you must know about &lt;strong&gt;Docker Hub&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is a container registry where we can push and store our Docker images.&lt;/p&gt;

&lt;p&gt;The same basic idea is provided by &lt;strong&gt;Amazon ECR&lt;/strong&gt;, but it is part of AWS.&lt;/p&gt;

&lt;p&gt;We can push our Docker images to ECR and then use those images with AWS services such as ECS and EKS.&lt;/p&gt;

&lt;p&gt;So ECR primarily does two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stores our container images.&lt;/li&gt;
&lt;li&gt;Makes those images available for our AWS container workloads.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dockerfile
    ↓
Docker Image
    ↓
   ECR
    ↓
ECS / EKS
    ↓
Running Container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same as S3, the cost is primarily related to things like storing and transferring images rather than simply creating a repository.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. ECS — Elastic Container Service
&lt;/h2&gt;

&lt;p&gt;Running an image creates a container.&lt;/p&gt;

&lt;p&gt;Now, once the container is up and running, we need to &lt;strong&gt;manage it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For that, we have &lt;strong&gt;ECS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;ECS is AWS's container orchestration service. It makes it easier to run, stop, manage and scale containers.&lt;/p&gt;

&lt;p&gt;For example, imagine you have an application running inside a Docker container.&lt;/p&gt;

&lt;p&gt;You don't want to manually log into a server every time you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start a container&lt;/li&gt;
&lt;li&gt;Stop a container&lt;/li&gt;
&lt;li&gt;Run multiple copies&lt;/li&gt;
&lt;li&gt;Replace a failed container&lt;/li&gt;
&lt;li&gt;Scale the application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ECS helps manage all of this.&lt;/p&gt;

&lt;p&gt;It can work with other AWS services to handle things like load balancing, service discovery and scaling.&lt;/p&gt;

&lt;p&gt;For example, if traffic increases, ECS can run more copies of your container.&lt;/p&gt;

&lt;p&gt;If a container fails, ECS can start a replacement according to the configuration you provide.&lt;/p&gt;

&lt;p&gt;So instead of manually managing individual containers, ECS manages them as part of a service.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. EKS — Elastic Kubernetes Service
&lt;/h2&gt;

&lt;p&gt;Now we have ECS.&lt;/p&gt;

&lt;p&gt;But what if you already know Kubernetes and want to use Kubernetes on AWS?&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;EKS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;EKS is AWS's &lt;strong&gt;managed Kubernetes service&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you already know Kubernetes, then much of your existing Kubernetes knowledge, YAML manifests and tooling can be used with EKS.&lt;/p&gt;

&lt;p&gt;AWS manages the Kubernetes control plane for you, while you still get the Kubernetes ecosystem and flexibility.&lt;/p&gt;

&lt;p&gt;So a very simple way to remember this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;ECS → AWS's container orchestration service&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;EKS → Managed Kubernetes on AWS&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you don't want to deal with Kubernetes and want something more AWS-native, ECS can be a simpler choice.&lt;/p&gt;

&lt;p&gt;If your team already uses Kubernetes or needs the Kubernetes ecosystem, EKS can make more sense.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. VPC — Virtual Private Cloud
&lt;/h2&gt;

&lt;p&gt;Now we have EC2, RDS, load balancers, containers, etc.&lt;/p&gt;

&lt;p&gt;But where do all these resources actually live?&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;VPC&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;VPC allows us to create our own &lt;strong&gt;private network inside AWS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can control things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IP ranges&lt;/li&gt;
&lt;li&gt;Subnets&lt;/li&gt;
&lt;li&gt;Route tables&lt;/li&gt;
&lt;li&gt;Internet access&lt;/li&gt;
&lt;li&gt;Private/public networking&lt;/li&gt;
&lt;li&gt;Network-level security&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, imagine we have a web application.&lt;/p&gt;

&lt;p&gt;We can put the application/load balancer in a public-facing part of the network while keeping our database in a &lt;strong&gt;private subnet&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So users can access the application, but they can't directly access the database from the internet.&lt;/p&gt;

&lt;p&gt;A very simple mental model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Internet
                   |
             Load Balancer
                   |
             Application
                   |
              Private DB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VPC becomes extremely important once your application starts becoming more than just a simple server.&lt;/p&gt;

&lt;p&gt;For now, just remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;VPC = Your private network inside AWS.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  8. Lambda
&lt;/h2&gt;

&lt;p&gt;Okay, we talked about EC2.&lt;/p&gt;

&lt;p&gt;But what if I don't want to manage a server at all?&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;AWS Lambda&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;Lambda allows us to run code without managing the underlying servers ourselves.&lt;/p&gt;

&lt;p&gt;For example, imagine you have an application where a user uploads an image to S3.&lt;/p&gt;

&lt;p&gt;You want to automatically resize that image after it is uploaded.&lt;/p&gt;

&lt;p&gt;Instead of keeping an EC2 server running 24/7 just for this task, you can trigger a Lambda function whenever a new image is uploaded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User uploads image
        ↓
       S3
        ↓
     Lambda
        ↓
Image gets resized
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AWS handles the underlying compute infrastructure and the function runs when it is invoked.&lt;/p&gt;

&lt;p&gt;So a simple way to remember it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;EC2 → You manage the server&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Lambda → AWS manages the underlying server infrastructure&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lambda is especially useful for event-driven workloads, APIs, automation and small pieces of backend logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. CloudWatch
&lt;/h2&gt;

&lt;p&gt;Since now we have talked about several services that AWS offers, how do we know if they are working the way they are supposed to?&lt;/p&gt;

&lt;p&gt;How do we check if a server failed or is facing some issue?&lt;/p&gt;

&lt;p&gt;For that, we have &lt;strong&gt;CloudWatch&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;CloudWatch is AWS's monitoring and observability service.&lt;/p&gt;

&lt;p&gt;It can work with things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Metrics&lt;/li&gt;
&lt;li&gt;Logs&lt;/li&gt;
&lt;li&gt;Alarms&lt;/li&gt;
&lt;li&gt;Dashboards&lt;/li&gt;
&lt;li&gt;Application monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also set alarms.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If CPU usage of an EC2 instance stays above 80% for 5 minutes, trigger an alarm.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the number of errors suddenly increases, notify the team.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also use CloudWatch to monitor application logs and help developers pinpoint issues customers are facing.&lt;/p&gt;

&lt;p&gt;So remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;CloudWatch → What is happening with my applications and AWS resources?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  10. CloudTrail
&lt;/h2&gt;

&lt;p&gt;Now imagine someone created a new database.&lt;/p&gt;

&lt;p&gt;Or someone spun up 20 EC2 machines.&lt;/p&gt;

&lt;p&gt;Or an intern deleted an entire database. 💀&lt;/p&gt;

&lt;p&gt;Now how do we see &lt;strong&gt;who did this?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For this, we have &lt;strong&gt;CloudTrail&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;CloudTrail records activity in your AWS account, including actions performed through the AWS Management Console, CLI, SDKs and AWS APIs.&lt;/p&gt;

&lt;p&gt;So we can use it to investigate things like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Who created this resource?&lt;/p&gt;

&lt;p&gt;Who deleted it?&lt;/p&gt;

&lt;p&gt;When did they do it?&lt;/p&gt;

&lt;p&gt;Which API call was made?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This becomes extremely important when multiple people are working on the same AWS account.&lt;/p&gt;

&lt;p&gt;So the easiest way to remember the difference is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;CloudWatch → What is happening?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;CloudTrail → Who did what?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  11. RDS — Relational Database Service
&lt;/h2&gt;

&lt;p&gt;Now our application is up and running.&lt;/p&gt;

&lt;p&gt;But we don't have a database to store our data.&lt;/p&gt;

&lt;p&gt;For that, we have &lt;strong&gt;RDS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;RDS stands for &lt;strong&gt;Relational Database Service&lt;/strong&gt; and it is a managed service for relational databases.&lt;/p&gt;

&lt;p&gt;We can choose from multiple database engines depending on our requirements.&lt;/p&gt;

&lt;p&gt;We can configure things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database engine&lt;/li&gt;
&lt;li&gt;Instance size&lt;/li&gt;
&lt;li&gt;Storage&lt;/li&gt;
&lt;li&gt;Networking&lt;/li&gt;
&lt;li&gt;Backups&lt;/li&gt;
&lt;li&gt;Availability&lt;/li&gt;
&lt;li&gt;Security&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The good thing is that AWS manages a lot of the underlying database infrastructure for us.&lt;/p&gt;

&lt;p&gt;For example, instead of manually setting up a PostgreSQL database on an EC2 machine and then worrying about backups, patching and other maintenance tasks, RDS can handle many of those responsibilities for us.&lt;/p&gt;

&lt;p&gt;One thing to keep in mind:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RDS can become a significant part of your AWS bill&lt;/strong&gt;, depending on the instance, storage, backups, availability configuration and other options you choose.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. DocumentDB
&lt;/h2&gt;

&lt;p&gt;Need a document database?&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;Amazon DocumentDB&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;DocumentDB is AWS's managed document database service and provides &lt;strong&gt;MongoDB compatibility&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So if you're coming from the MongoDB world, many concepts will feel familiar.&lt;/p&gt;

&lt;p&gt;But one important thing:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DocumentDB isn't simply MongoDB running on AWS.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is a separate AWS service with MongoDB compatibility and there are differences between the two.&lt;/p&gt;

&lt;p&gt;For now, the simple thing to remember is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;RDS → Relational databases&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;DocumentDB → Document database&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  13. ElastiCache
&lt;/h2&gt;

&lt;p&gt;You might think databases are done.&lt;/p&gt;

&lt;p&gt;But what about one more important thing?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes we don't want to repeatedly query our database for the same data.&lt;/p&gt;

&lt;p&gt;Imagine your application has a popular product page.&lt;/p&gt;

&lt;p&gt;Thousands of users are requesting the same product information.&lt;/p&gt;

&lt;p&gt;Instead of hitting the database every single time, we can keep frequently accessed data in a cache.&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;Amazon ElastiCache&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;It is AWS's managed caching service and supports caching engines such as &lt;strong&gt;Valkey and Memcached&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
Application
 ↓
Is data in cache?
 ↙          ↘
YES          NO
 ↓            ↓
Cache       Database
              ↓
           Save in cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cache is generally much faster to access than going all the way to the database.&lt;/p&gt;

&lt;p&gt;So remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;RDS → Store your application's data&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;ElastiCache → Keep frequently accessed data ready for fast access&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  That's pretty much it!
&lt;/h1&gt;

&lt;p&gt;These are some of the AWS services that you are likely to encounter frequently when building and deploying applications.&lt;/p&gt;

&lt;p&gt;Obviously, AWS has &lt;strong&gt;a lot more services&lt;/strong&gt; than these.&lt;/p&gt;

&lt;p&gt;But you don't need to memorize every single AWS service before you can start building things.&lt;/p&gt;

&lt;p&gt;Start with the services you actually need and learn the others as you encounter them.&lt;/p&gt;

&lt;p&gt;For a quick revision, here is a simple cheat sheet:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;AWS Service&lt;/th&gt;
&lt;th&gt;Think of it as&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;IAM&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Who can access what?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;EC2&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Virtual server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;S3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cloud object storage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ECR&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Docker image registry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ECS&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;AWS container orchestration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;EKS&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Managed Kubernetes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;VPC&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Your private network in AWS&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Lambda&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Run code without managing servers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;CloudWatch&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Monitoring and observability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;CloudTrail&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Who did what in your AWS account&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;RDS&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Managed relational database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;DocumentDB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Managed document database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ElastiCache&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Managed caching&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And that's it.&lt;/p&gt;

&lt;p&gt;I tried to keep these explanations &lt;strong&gt;small enough for quick revision&lt;/strong&gt;, but detailed enough to give you an idea of what each service actually does.&lt;/p&gt;

&lt;p&gt;Hopefully, this helps you understand AWS without feeling like you have to memorize an entire spaceship control panel. 🚀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I hope you liked it!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloud</category>
      <category>devops</category>
      <category>beginners</category>
    </item>
    <item>
      <title>RIVAL — I Built an AI That Settles Any Rivalry… Out Loud 🔥</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Mon, 13 Jul 2026 05:56:26 +0000</pubDate>
      <link>https://dev.to/blackwatch021/rival-i-built-an-ai-that-settles-any-rivalry-out-loud-3hkc</link>
      <guid>https://dev.to/blackwatch021/rival-i-built-an-ai-that-settles-any-rivalry-out-loud-3hkc</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for &lt;a href="https://dev.to/challenges/weekend-2026-07-09"&gt;Weekend Challenge: Passion Edition&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;p&gt;Remember being a kid and staging the ultimate showdowns in your head? &lt;em&gt;Goku vs Naruto. Gohan vs Batman. Your dog vs the neighbour's cat.&lt;/em&gt; No rules, no logic — just pure "who would win?!" chaos with your friends until someone rage-quit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RIVAL&lt;/strong&gt; is me trying to bottle that feeling.&lt;/p&gt;

&lt;p&gt;You type in any two rivals — real or fictional, living or non-living, serious or completely ridiculous — pick a &lt;strong&gt;tone&lt;/strong&gt;, and smash the &lt;strong&gt;🔥 IGNITE&lt;/strong&gt; button. RIVAL then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;writes a fiery, over-the-top hype monologue that trash-talks &lt;em&gt;and&lt;/em&gt; celebrates both sides,&lt;/li&gt;
&lt;li&gt;scouts each contender with &lt;strong&gt;💪 Powers&lt;/strong&gt; and &lt;strong&gt;⚠️ Weaknesses&lt;/strong&gt; cards,&lt;/li&gt;
&lt;li&gt;crowns a &lt;strong&gt;winner&lt;/strong&gt; and drops a bold &lt;strong&gt;prediction&lt;/strong&gt;,&lt;/li&gt;
&lt;li&gt;rates the beef on a &lt;strong&gt;passion meter&lt;/strong&gt;,&lt;/li&gt;
&lt;li&gt;and — the best bit — &lt;strong&gt;reads the whole thing out loud&lt;/strong&gt; like a dramatic sports commentator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjiima26l3h1ji6ff0f40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjiima26l3h1ji6ff0f40.png" alt="hero section with complete result" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Barça vs Real Madrid, settled. (The AI sided with Madrid. I'm choosing not to comment.)&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Please don't take the verdicts to heart.&lt;/strong&gt; It's a fun project, not a court ruling. I asked it to settle &lt;em&gt;Batman vs a cat&lt;/em&gt; and… let's just say I'm still not over the result. 🥲&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the whole idea: pick two things, start a fight, enjoy the show.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;🔴 Try it live:&lt;/strong&gt; &lt;a href="https://rivals-tau.vercel.app/" rel="noopener noreferrer"&gt;https://rivals-tau.vercel.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Watch it in action (turn the sound ON — the voice &lt;em&gt;is&lt;/em&gt; the whole point):&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/zhl_VvIZPkk"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And here's the setup — two rivals, six quick presets, five tones, one big red button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fghxbvysyvnlc6xq8uw2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fghxbvysyvnlc6xq8uw2g.png" alt="herosection" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/BlackWatch021" rel="noopener noreferrer"&gt;
        BlackWatch021
      &lt;/a&gt; / &lt;a href="https://github.com/BlackWatch021/Rivals" rel="noopener noreferrer"&gt;
        Rivals
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      My submission for dev.to weekend challenge
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;🔥 RIVAL&lt;/h1&gt;
&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Settle the rivalry.&lt;/h3&gt;
&lt;/div&gt;
&lt;p&gt;Pick &lt;strong&gt;any two rivals&lt;/strong&gt; — real or fictional, living or non-living, serious or completely ridiculous. RIVAL writes fiery trash-talk, scouts each side's &lt;strong&gt;powers &amp;amp; weaknesses&lt;/strong&gt;, crowns a &lt;strong&gt;winner&lt;/strong&gt;, and then &lt;strong&gt;reads the whole thing out loud&lt;/strong&gt; like a dramatic sports commentator.&lt;/p&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/20af8ead8ff55dc76826e6cf06bad7ab29b843c7d8f9ff76e5296d7106415b46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4e6578742e6a732d3030303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d6e657874646f746a73266c6f676f436f6c6f723d7768697465"&gt;&lt;img src="https://camo.githubusercontent.com/20af8ead8ff55dc76826e6cf06bad7ab29b843c7d8f9ff76e5296d7106415b46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4e6578742e6a732d3030303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d6e657874646f746a73266c6f676f436f6c6f723d7768697465" alt="Next.js"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/2ab4127dedd4ccf7d3dbb0d440cacca4f720cbb4f06c74350a37f6925b828523/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f547970655363726970742d3331373843363f7374796c653d666f722d7468652d6261646765266c6f676f3d74797065736372697074266c6f676f436f6c6f723d7768697465"&gt;&lt;img src="https://camo.githubusercontent.com/2ab4127dedd4ccf7d3dbb0d440cacca4f720cbb4f06c74350a37f6925b828523/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f547970655363726970742d3331373843363f7374796c653d666f722d7468652d6261646765266c6f676f3d74797065736372697074266c6f676f436f6c6f723d7768697465" alt="TypeScript"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/1c1f149f0c086b20f1dd23c0c21a29df95fe3adf8202db33fd9a7251b19639ad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5461696c77696e642d3036423644343f7374796c653d666f722d7468652d6261646765266c6f676f3d7461696c77696e64637373266c6f676f436f6c6f723d7768697465"&gt;&lt;img src="https://camo.githubusercontent.com/1c1f149f0c086b20f1dd23c0c21a29df95fe3adf8202db33fd9a7251b19639ad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5461696c77696e642d3036423644343f7374796c653d666f722d7468652d6261646765266c6f676f3d7461696c77696e64637373266c6f676f436f6c6f723d7768697465" alt="Tailwind CSS"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/d41b851f1d0c085c3ab1283b3d3bdec9c36e32a5d8f705650380d9e65fd173ba/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f476f6f676c6525323047656d696e692d3845373542323f7374796c653d666f722d7468652d6261646765266c6f676f3d676f6f676c6567656d696e69266c6f676f436f6c6f723d7768697465"&gt;&lt;img src="https://camo.githubusercontent.com/d41b851f1d0c085c3ab1283b3d3bdec9c36e32a5d8f705650380d9e65fd173ba/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f476f6f676c6525323047656d696e692d3845373542323f7374796c653d666f722d7468652d6261646765266c6f676f3d676f6f676c6567656d696e69266c6f676f436f6c6f723d7768697465" alt="Google Gemini"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/f8b30dd08c9c913953e5bdec59eaba9576ffcdd4e0d5dcefa646156895f63c93/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f456c6576656e4c6162732d3030303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d656c6576656e6c616273266c6f676f436f6c6f723d7768697465"&gt;&lt;img src="https://camo.githubusercontent.com/f8b30dd08c9c913953e5bdec59eaba9576ffcdd4e0d5dcefa646156895f63c93/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f456c6576656e4c6162732d3030303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d656c6576656e6c616273266c6f676f436f6c6f723d7768697465" alt="ElevenLabs"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/34bfa473ed2cc56da8aead699dd5d36ccd92466b9629d1510bd8cfc222327dbf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f56657263656c2d3030303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d76657263656c266c6f676f436f6c6f723d7768697465"&gt;&lt;img src="https://camo.githubusercontent.com/34bfa473ed2cc56da8aead699dd5d36ccd92466b9629d1510bd8cfc222327dbf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f56657263656c2d3030303030303f7374796c653d666f722d7468652d6261646765266c6f676f3d76657263656c266c6f676f436f6c6f723d7768697465" alt="Vercel"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://rivals-tau.vercel.app/" rel="nofollow noopener noreferrer"&gt;🔴 Live Demo&lt;/a&gt; · &lt;a href="https://dev.to/blackwatch021/rival-i-built-an-ai-that-settles-any-rivalry-out-loud-3hkc" rel="nofollow"&gt;📖 Read the story&lt;/a&gt; · &lt;a href="https://www.youtube.com/watch?v=zhl_VvIZPkk" rel="nofollow noopener noreferrer"&gt;🎬 Watch the demo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;a rel="noopener noreferrer" href="https://github.com/BlackWatch021/Rivals/public/images/herosection_with_complete_result.png"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FBlackWatch021%2FRivals%2FHEAD%2Fpublic%2Fimages%2Fherosection_with_complete_result.png" width="820" alt="RIVAL settling Barça vs Real Madrid — monologue, passion meter, and powers/weaknesses cards"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;✨ What it does&lt;/h2&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;🥊 &lt;strong&gt;Any matchup&lt;/strong&gt; — &lt;code&gt;Goku vs Naruto&lt;/code&gt;, &lt;code&gt;iOS vs Android&lt;/code&gt;, &lt;code&gt;Cats vs Dogs&lt;/code&gt;, &lt;code&gt;Coffee vs Tea&lt;/code&gt;… if you can name two things, you can pit them.&lt;/li&gt;
&lt;li&gt;✍️ &lt;strong&gt;Fiery AI monologue&lt;/strong&gt; that trash-talks &lt;em&gt;and&lt;/em&gt; hypes both sides.&lt;/li&gt;
&lt;li&gt;💪 &lt;strong&gt;Scouting cards&lt;/strong&gt; — Powers &amp;amp; Weaknesses for each contender.&lt;/li&gt;
&lt;li&gt;🏆 &lt;strong&gt;A winner + a bold prediction&lt;/strong&gt;, plus a &lt;strong&gt;passion meter&lt;/strong&gt; for the beef.&lt;/li&gt;
&lt;li&gt;🎙️ &lt;strong&gt;Dramatic spoken commentary&lt;/strong&gt; — the verdict, read aloud.&lt;/li&gt;
&lt;li&gt;🎭 &lt;strong&gt;Five tones&lt;/strong&gt; — Hype, Savage, Sarcastic, Professional, Wholesome.&lt;/li&gt;
&lt;li&gt;⚡ &lt;strong&gt;Six&lt;/strong&gt;…&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/BlackWatch021/Rivals" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  How I Built It
&lt;/h2&gt;

&lt;p&gt;The plan sounded easy: &lt;em&gt;"call two AI APIs, done by lunch."&lt;/em&gt; Reader, it was not done by lunch. Here's the fun of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧱 The stack
&lt;/h3&gt;

&lt;p&gt;Next.js (App Router) + TypeScript + Tailwind, deployed on &lt;strong&gt;Vercel&lt;/strong&gt;. Two tiny server-side &lt;strong&gt;Route Handlers&lt;/strong&gt; do all the secret-key work — &lt;code&gt;/api/generate&lt;/code&gt; for the text and &lt;code&gt;/api/voice&lt;/code&gt; for the audio — so my API keys &lt;strong&gt;never&lt;/strong&gt; touch the browser. The frontend only ever talks to &lt;em&gt;my&lt;/em&gt; server, and &lt;em&gt;my&lt;/em&gt; server talks to the AIs. No key ever ships to the client.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧠 The brain — Google Gemini
&lt;/h3&gt;

&lt;p&gt;Gemini does all the &lt;em&gt;writing&lt;/em&gt;: the monologue, a shorter spoken version, the powers/weaknesses for each side, the winner, the prediction, and a passion score — all returned as clean JSON, and all steered by whichever &lt;strong&gt;tone&lt;/strong&gt; you picked (Hype, Savage, Sarcastic, Professional, Wholesome).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frlj6r3grwjh5tbaftod0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frlj6r3grwjh5tbaftod0.png" alt="scouting cards power weakness" width="705" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cats vs Dogs. "Silent assassins" vs "Separation anxiety." Chef's kiss.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My first attempts kept returning &lt;strong&gt;cut-off, broken JSON&lt;/strong&gt;, and it drove me up the wall. Turns out the model I'm on is a &lt;em&gt;thinking&lt;/em&gt; model — it was quietly burning its entire token budget "thinking" and running out of room mid-sentence. The fix was to tell it to stop overthinking and just answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;generationConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;responseMimeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;thinkingConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;thinkingBudget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// stop pondering, just reply&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes the AI just needs to be told to shut up and hand over the goods.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔊 The voice — ElevenLabs
&lt;/h3&gt;

&lt;p&gt;This is the "wow." ElevenLabs takes the text and reads it back in a dramatic broadcaster voice. I feed it a tight, &lt;strong&gt;~40-second&lt;/strong&gt; spoken version instead of the full monologue, so it lands like a punchy highlight reel instead of a two-minute TED talk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frf2tptuv2wgsajosjgax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frf2tptuv2wgsajosjgax.png" alt="audio player" width="687" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gotcha I learned the hard way: on the free tier you &lt;em&gt;can't&lt;/em&gt; use ElevenLabs' fancy "library" voices through the API — it throws a payment error right in your face. Swapping to a built-in premade voice ("Daniel", a steady broadcaster) fixed it and honestly suits a commentator better anyway.&lt;/p&gt;

&lt;h3&gt;
  
  
  💸 Surviving the free tier (a.k.a. the real project)
&lt;/h3&gt;

&lt;p&gt;Here's the twist nobody warns you about: I'm on the &lt;strong&gt;free tier&lt;/strong&gt; of both AIs, and that quietly became the most interesting engineering constraint of the whole build.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gemini&lt;/strong&gt; gives me roughly &lt;strong&gt;20 requests a &lt;em&gt;day&lt;/em&gt;&lt;/strong&gt;. &lt;strong&gt;ElevenLabs&lt;/strong&gt; gives about &lt;strong&gt;10,000 characters a &lt;em&gt;month&lt;/em&gt;&lt;/strong&gt; — that's ~5 full monologues. Not a lot once strangers on the internet start hammering IGNITE.&lt;/li&gt;
&lt;li&gt;So that short spoken version isn't just nicer to listen to — it's about &lt;strong&gt;4× cheaper&lt;/strong&gt; on characters (≈2,000 → ≈340 per go).&lt;/li&gt;
&lt;li&gt;The six &lt;strong&gt;preset&lt;/strong&gt; matchups are &lt;strong&gt;pre-generated&lt;/strong&gt; (text &lt;em&gt;and&lt;/em&gt; audio) and served as static files. Clicking a preset costs &lt;strong&gt;zero&lt;/strong&gt; API calls — instant, free, and infinitely replayable. That's the guaranteed-to-work demo path.&lt;/li&gt;
&lt;li&gt;And when a quota &lt;em&gt;does&lt;/em&gt; run dry, you don't get a scary red error — you get a cheeky popup, and the app gently nudges you toward a preset instead. Failing with a smile beats failing with a stack trace.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsdt7p3zswzlahv82kvo7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsdt7p3zswzlahv82kvo7.png" alt="quotapopup" width="800" height="598"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The most polite "I'm broke, please try a free one" message I could write.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prize Categories
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Best Use of Google AI&lt;/strong&gt; — Gemini writes &lt;em&gt;every&lt;/em&gt; word: the trash-talk, the scouting report, the verdict, and the prediction, tuned to whatever tone you pick.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Best Use of ElevenLabs&lt;/strong&gt; — it turns all of that into dramatic, spoken-word commentary that plays right on the page.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Built solo, over one caffeine-heavy weekend. 🔥&lt;/p&gt;

&lt;p&gt;I had a whole list of features I didn't get to (I started late — classic), so if RIVAL made you smile and you'd like to see it grow, &lt;strong&gt;drop a comment with your dream matchup or the feature you'd add&lt;/strong&gt; — I'm reading every single one.&lt;/p&gt;

&lt;p&gt;Now go settle something ridiculous. Batman deserves a rematch. 🦇🐱&lt;/p&gt;

</description>
      <category>devchallenge</category>
      <category>weekendchallenge</category>
    </item>
    <item>
      <title>Your Node.js Server is Using Just One CPU. Here's How to Fix It.</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Sun, 24 May 2026 06:10:34 +0000</pubDate>
      <link>https://dev.to/blackwatch021/your-nodejs-server-is-using-just-one-cpu-heres-how-to-fix-it-4ee</link>
      <guid>https://dev.to/blackwatch021/your-nodejs-server-is-using-just-one-cpu-heres-how-to-fix-it-4ee</guid>
      <description>&lt;h2&gt;
  
  
  CLUSTERING
&lt;/h2&gt;

&lt;p&gt;You created your node application, it's ready, you have chosen an 8 vCPU instance to deploy it. You are done with deployment. Everything is working fine, but unknowingly you aren't using the full potential of the deployment. We know that node.js runs on a SINGLE THREAD, which means our node application uses only one vCPU at a time — but you took an 8 vCPU instance, so aren't the other 7 vCPUs sitting there idle?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3glf3wf337yjk2uu0319.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3glf3wf337yjk2uu0319.png" alt=" " width="800" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The solution for this is CLUSTERING. It's a concept of running multiple instances of an application, where each works as an individual entity but still gets the work done and runs on the same port. Now the question is — how will this work? Isn't it going to cause issues among the instances? The simple and short answer is no.&lt;/p&gt;

&lt;h2&gt;
  
  
  HOW IT WORKS
&lt;/h2&gt;

&lt;p&gt;When clustering is done, we end up with multiple processes. There are two kinds:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Primary – There is only one primary. It is responsible for spinning up the worker processes, managing them, and if any one of them dies, spawning it back.
The primary is there only to manage — it doesn't run the code (connecting with db, spinning up server, etc.)
Note – If the primary is down, the entire cluster will crash.&lt;/li&gt;
&lt;li&gt;Workers – These are the actual instances where the application runs — they serve the users.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Facts&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Since there are 8 vCPUs in our case, there will be 9 total processes — 1 primary + 8 workers.&lt;/li&gt;
&lt;li&gt;Each worker has its own memory – nothing is shared among workers.&lt;/li&gt;
&lt;li&gt;Workers share a single port – connections are distributed across them.&lt;/li&gt;
&lt;li&gt;Primary is intentionally dumb – never runs code or connects with db.&lt;/li&gt;
&lt;li&gt;Workers can't see their siblings — for each worker, only itself exists.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;CODE SNIPPET&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node:cluster&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;os&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node:os&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;connectDB&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/config/database&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;enableCluster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enableCluster&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPrimary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numWorkers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cpus&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numWorkers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;exit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; died — respawning`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;connectDB&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;httpServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;EXPLANATION OF CODE&lt;/strong&gt;&lt;br&gt;
In production we generally use services like pm2 to manage clustering, but here we are doing it using native options. For that, we first need the cluster and os modules of node.&lt;br&gt;
Then we check if the current process is the primary or not. If it is the primary, we spawn new workers as per the number of cores available — it's not hard coded, we may change it as per our convenience, but it should not be more than the number of cores/vCPUs. If it isn't the primary (meaning we are already inside a worker), we run the actual backend code — connecting to the DB and starting the server. So now we have 8 worker instances up and running (plus the primary watching over them).&lt;br&gt;
Using process.pid, we can see the unique id of each worker.&lt;br&gt;
Note – this id, and whatever happens inside an instance, stays there only. Other instances can't access this one's data, process, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PROS/CONS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Uses all CPU cores&lt;/li&gt;
&lt;li&gt;Crash isolation&lt;/li&gt;
&lt;li&gt;Built into Node&lt;/li&gt;
&lt;li&gt;Higher throughput, CPU-bound work&lt;/li&gt;
&lt;li&gt;Auto-respawns dead workers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Each worker has its own RAM (no shared state)&lt;/li&gt;
&lt;li&gt;In-memory caches/sessions break silently&lt;/li&gt;
&lt;li&gt;WebSockets/SSE need extra infrastructure&lt;/li&gt;
&lt;li&gt;Harder to debug – 'which worker logged that?'&lt;/li&gt;
&lt;li&gt;Primary crash = whole cluster dies&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note – load balancing is round-robin on Linux; on Windows, the OS decides routing.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  BIG CAVEAT
&lt;/h2&gt;

&lt;p&gt;This much is enough for simple clustering or for learning purposes, as long as our app is using stateless data (REST APIs backed by a DB).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fge9zh3hj87lyym5b3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fge9zh3hj87lyym5b3s.png" alt=" " width="752" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the DB is the source of truth. Workers don't need to know about each other. Any worker can serve any request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STATEFUL connections (WebSocket)&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Prerequisite — knowledge of websockets.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now things change. Once a connection is established and the HTTP request is upgraded to a WebSocket, the socket connection details (which user is on which socket) are stored in memory, inside that worker. So if User A connects through Worker 1 and User B connects through Worker 2, both are logged in and both users' data is stored in the DB. But the live sockets sit on different workers. Now when A sends a message to B, Worker 1 tries to push it to B's socket — but B's socket lives in Worker 2's memory, not Worker 1's. So the message gets saved to the DB, but real-time delivery to B fails.&lt;br&gt;
Also, workers are standalone, so they can't even talk to each other to ask "do you have this user with you?"&lt;br&gt;
A TCP socket lives inside one process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e3npzqdgb6eypijn7bg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e3npzqdgb6eypijn7bg.png" alt=" " width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STICKY Session&lt;/strong&gt;&lt;br&gt;
Imagine a user lands on Worker A and creates a socket connection. Details regarding the session are stored in Worker A's memory. Somehow, on the next request, the user is shifted to Worker B. Now the user tries to continue the conversation. The worker checks if this session exists or not, but there is no record of it in Worker B (that detail lives in Worker A). So the interaction fails.&lt;/p&gt;

&lt;p&gt;To make it easier to picture, here are two ways to think about it:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analogy 1&lt;/em&gt; (hotel front desk) — You check into Hotel A. The front desk writes your name against Room 204. Later, you walk into Hotel B and ask for your room key. Hotel B has no idea who you are, because your check-in details only exist at Hotel A's front desk.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analogy 2&lt;/em&gt; (locker at a station) — You drop your bag at locker #5 in Station A and get a ticket. Later, you go to Station B and try to use the same ticket. Station B has no locker matching that ticket, because the bag is sitting back in Station A.&lt;/p&gt;

&lt;p&gt;To mitigate this issue, we need Sticky Sessions. It ensures that a user stays on a single worker only — pinning all of one client's requests to the same worker.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One more thing worth knowing&lt;/em&gt; — Socket.IO's connection handshake itself is made of multiple HTTP requests (long-polling fallback) before it upgrades to WebSocket. Without stickiness, those handshake requests can scatter across different workers, and the connection never even establishes. So sticky sessions are needed not just after the user is connected, but during the initial connection itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REDIS ADAPTER for SOCKET&lt;/strong&gt;&lt;br&gt;
Even with stickiness, workers still can't communicate with each other. So User A on Worker 1 has no way to push a message to User B sitting on Worker 2. This is a major issue in applications using sockets or real-time communication. To solve this, we have adapters — one of them is the Redis adapter for Socket.IO. It acts as a coordination layer on pub/sub. With this in place, when Worker 1 emits a message, the adapter publishes that emit to a shared bus (Redis). Every worker is subscribed to this bus, and the worker that actually owns B's socket picks it up and delivers the message locally. Now the application will work just like an application running on a single instance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6pt29r2ahp1zugbz0i1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6pt29r2ahp1zugbz0i1.png" alt=" " width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STICKY + ADAPTER&lt;/strong&gt;&lt;br&gt;
The two solve different problems, and you actually need both together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sticky sessions make sure a user's requests always land on the same worker, so the connection (and the handshake) never breaks mid-way.&lt;/li&gt;
&lt;li&gt;The Redis adapter makes sure that when a worker needs to push a message to a user sitting on a different worker, the message can still reach them through the shared pub/sub bus.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sticky alone — your user stays connected, but messages between users on different workers still don't reach. Adapter alone — workers can broadcast across each other, but the initial connection itself keeps breaking. Together — your clustered app behaves like a single instance from the user's perspective.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fery2kpmprmgjz9in34zw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fery2kpmprmgjz9in34zw.png" alt=" " width="799" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;TL;DR&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Node is single-threaded. Clustering spawns one worker per core. REST scales for free because the DB is shared. Sockets don't — connections live in one worker's RAM. Fix with sticky sessions (so handshakes complete) plus a pub/sub adapter (so workers can deliver each other's messages).&lt;/p&gt;

&lt;p&gt;So this sums up basic clustering in a node.js application.&lt;br&gt;
Thanks for reading.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
    <item>
      <title>DOCKER — A MUST KNOW TOOL FOR EVERY DEVELOPERs</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Tue, 14 Apr 2026 15:34:47 +0000</pubDate>
      <link>https://dev.to/blackwatch021/docker-a-must-know-tool-for-every-developers-5dl3</link>
      <guid>https://dev.to/blackwatch021/docker-a-must-know-tool-for-every-developers-5dl3</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frca9r5huyezd6lhodebe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frca9r5huyezd6lhodebe.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ever came across the meme:&lt;br&gt;
&lt;strong&gt;“But it works on my computer”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you know docker, then you will understand its meaning, a single line but a very big issue is hidden beneath that meme. So lets dive deep into it and lets understand what docker is and why we need it.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you joined a team as a frontend developer, and you were tasked to run the code on your local machine. You followed the instructions. You installed Node in the system, then installed Git. Now you cloned the project and installed all of the dependencies and you hit &lt;strong&gt;&lt;em&gt;npm run dev&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It took its time and you saw errors in console.&lt;br&gt;
&lt;strong&gt;Error → Incompatible Node version&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You connected with your team, they helped you install the correct versions of all the services and environment. Now you ran the code again, and again you ran into an error. Now the issue was the system. You are using macOS while the team is working on Windows—different architecture, different tools.&lt;/p&gt;

&lt;p&gt;Now you solved this issue and you need to deploy the application on cloud. There also you will have to add all of these configurations to make it up and running. Pretty hectic.&lt;/p&gt;

&lt;p&gt;Team has to do this every time a new member joins.&lt;br&gt;
Here comes Docker for our rescue.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;DOCKER&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is a concept of containers in Docker. It’s like an isolated environment which can be configured as per the requirements—from OS-level dependencies to tools needed to run the app.&lt;/p&gt;

&lt;p&gt;Once it's set up, it can be shared among other team members and they can run it easily. Now there will be no issue running the application, because containers have their own environment. Even if team members have Windows, Linux or macOS*&lt;em&gt;—it will still run&lt;/em&gt;*.&lt;/p&gt;

&lt;p&gt;To do so, first we need to install:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker CLI (command line interface)&lt;/li&gt;
&lt;li&gt;Docker Desktop (GUI)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can download Docker from the &lt;a href="https://docs.docker.com/desktop/setup/install/windows-install/" rel="noopener noreferrer"&gt;official site&lt;/a&gt; and follow the instructions to install and run it.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Docker Components&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Docker has two parts, one is the docker Engine (daemon) and second is docker desktop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Docker Engine (Daemon)&lt;/strong&gt;&lt;br&gt;
It is the main crux of docker, it runs in background doing everything, from managing images, containers to run them, managing their lifecycle&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Docker Desktop&lt;/strong&gt;&lt;br&gt;
It a GUI(graphical user interface) which gives ease to user to use and run docker without going into too much technicality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verify Installation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If installed correctly, you’ll see something like:&lt;br&gt;
&lt;em&gt;Docker version 29.x.x&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bylv80t2q3pq7cc3zin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bylv80t2q3pq7cc3zin.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;CONTAINERS and IMAGES&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Whole Docker system revolves around these two.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw593qma8n0n1ccwex9z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw593qma8n0n1ccwex9z.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Containers&lt;/strong&gt;&lt;br&gt;
These are the environment, which is required to run an image and each container is separated for other containers. Means container 1 don’t have access to the files/data of container 2 and vice versa.&lt;/p&gt;

&lt;p&gt;A container is basically a running instance of an image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Images&lt;/strong&gt;&lt;br&gt;
You can think of it as a blueprint which already has everything installed (like node, dependencies, etc). Contains everything needed to run an app&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CUSTOM IMAGES&lt;/strong&gt;&lt;br&gt;
We can also create custom images and push them on docker hub, which is a collection of images for docker, just like github where other users or team members can download these images and run it in their systems(docker)&lt;/p&gt;

&lt;p&gt;Imagine you have a MacBook, which is running macOS inside it. Now the containers act like MacBook and images acts like macOS. Images running inside container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image = blueprint&lt;/li&gt;
&lt;li&gt;Container = running application&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Basic Commands&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Good for the basics, now lets play around with docker commands&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Run container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; image_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Creates + starts a container from an image&lt;/li&gt;
&lt;li&gt;Opens interactive terminal&lt;/li&gt;
&lt;li&gt;Container name will be random&lt;/li&gt;
&lt;li&gt;Pulls image from docker hub if not available locally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. List containers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker container &lt;span class="nb"&gt;ls
&lt;/span&gt;OR
docker container &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;list downs all running containers
-a List downs all of the containers, even the stopped ones&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Start/Stop&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker start container_name
AND
docker stop container_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Execute command inside container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec &lt;/span&gt;container_name &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;It runs the ls command inside a running Docker container and exits after running the command&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; container_name bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;_Opens interactive shell inside container, now you are inside the container _&lt;/p&gt;

&lt;p&gt;&lt;em&gt;exec = execute&lt;br&gt;
-it = interactive&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Exit&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ctrl + D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. List images&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker images 
OR 
docker image &lt;span class="nb"&gt;ls&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;PORT MAPPING&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You might be thinking that the containers are isolated in nature, they don’t interact with other container or outside world(system) then how can we run and test projects on our system. Take an example, you pulled a node application and it is running in the container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; imageName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you might open a tab &lt;em&gt;localhost:9000&lt;/em&gt;. But you will see nothing there, why cause that application is running inside the container, and our system doesn’t know anything about it.&lt;/p&gt;

&lt;p&gt;In this situations, we need port mapping/need to expose the ports to the system.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How we do it, using -p tag followed by system port : container port&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 9000:9000 image_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here we mapped the port 9000 of container to our system’s 9000 port. Now you can check &lt;em&gt;localhost:9000&lt;/em&gt; Application will be up and running.&lt;/p&gt;

&lt;p&gt;We can set it to any port and we can map multiple ports as per the requirement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 4584:9000 &lt;span class="nt"&gt;-p&lt;/span&gt; 2300:3000 image_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;ENVIRONMENT VARIABLES&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now you may ask, we create environment variables in our projects, we store secret keys in it and other important data, which only we have. So how can we set these in the container.&lt;/p&gt;

&lt;p&gt;It's a genuine question and the solution is very easy, all we need to do is to use -e followed by key=value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 4584:9000 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;jwtSecret&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;secretisthis &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;production&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;image_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is all the basics you need to understand what docker is and how to work with it. Now lets move one step further and lets containerize a very simple node server.&lt;/p&gt;

&lt;p&gt;You can create your very own node server or you may use this &lt;a href="https://github.com/BlackWatch021/docker-tutorial" rel="noopener noreferrer"&gt;tutorial app&lt;/a&gt; for this tutorial.&lt;/p&gt;

&lt;p&gt;Your simple server is ready, now we have to containerize, make an image of it so others can use it.&lt;/p&gt;

&lt;p&gt;To do so, we need a Dockerfile where we will be writing down all of our configurations.&lt;br&gt;
Note – the name should be &lt;em&gt;Dockerfile&lt;/em&gt; without any extension&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;DOCKERFILE&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; ubuntu:22.04 &lt;/span&gt;
&lt;span class="c"&gt;# base image, it can be node, redis etc&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="c"&gt;# Set working directory&lt;/span&gt;

&lt;span class="c"&gt;#Install Node.js&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; curl ca-certificates &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://deb.nodesource.com/setup_18.x | bash - &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; nodejs &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;apt-get clean &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;


&lt;span class="c"&gt;#Copy dependency files first (for caching)&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json package-lock.json ./&lt;/span&gt;

&lt;span class="c"&gt;#Install dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="c"&gt;#Copy rest of the app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;#Tells to run these files whenever someone runs the image&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["node", "main.js"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Build Image&lt;/strong&gt;&lt;br&gt;
Now the configuration is complete, all we need do to is to make an image. For that the command is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; docker-tutorial &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note - . as path cause I am in the same directory as of the Dockerfile&lt;/p&gt;

&lt;p&gt;It will take some time, once its done you will see your image in your docker desktop app or you can use the command in terminal too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run Container&lt;/strong&gt;&lt;br&gt;
Now you can run it and test if its working or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;4567 &lt;span class="nt"&gt;-p&lt;/span&gt; 8000:4567 docker-tutorial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here I am passing environment variable PORT and I am mapping that port to system’s port 8000. Now I can see my application running on &lt;em&gt;localhost:8000&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CACHING&lt;/strong&gt;&lt;br&gt;
Each line in Dockerfile = layer&lt;/p&gt;

&lt;p&gt;If one layer changes → all layers after it rebuilds&lt;/p&gt;

&lt;p&gt;That’s why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json ...&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is important for performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;PUSH TO DOCKER HUB&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now you want to share your custom image with your teammates or with the world. To do so, first you need to create an account on &lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;docker hub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you need to create a repository, once its created, then you will see a command like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf7mm4tl3mv384vjdq3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf7mm4tl3mv384vjdq3l.png" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker push your_user_name/docker-tutorial:tagname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the command that we will be using to push our image to the docker hub, but before that we have to create that image as per the notations.&lt;/p&gt;

&lt;p&gt;Create the image, with name your_user_name/repository_name&lt;/p&gt;

&lt;p&gt;In our case it will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt;  your_user_name/docker-tutorial_ &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtma70mlxidk0kih1fe9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtma70mlxidk0kih1fe9.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once image is created, you can check it on docker desktop or using commands in cli&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpn4oy6d3wsymqgji73da.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpn4oy6d3wsymqgji73da.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to run our push command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker push username/docker-tutorial:tagname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All done, check your repository on docker hub, you will see your docker image there. Now you can share this image with others and they can run it directly. No environment setup, no package matching needed. Just run it. That’s it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;DOCKER COMPOSE&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Lets move to more advance part&lt;br&gt;
Imagine you are working on a very big project, which needs nodejs, postgresql, redis, mailhog etc. Now you can either go and run each image one by one, or we can use docker compose. It’s a file where we configure all of the services (images), and with a single command all of them will start/stop. No manual efforts, just a single command to spin up the application.&lt;/p&gt;

&lt;p&gt;To do so, we need to create a docker-compose.yml file, where we will be writing all of our configurations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.8"&lt;/span&gt;

&lt;span class="c1"&gt;#services is where we will add all of the images, that we need to run&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;5432:5432"&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;review&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;password&lt;/span&gt;

  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;6379:6379"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run everything&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stop everything&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pretty much sums up what Docker is, why we need it, and its importance in today’s development world. Hope you learned something useful from this.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;br&gt;
Peace out&lt;/p&gt;

</description>
      <category>docker</category>
      <category>developer</category>
      <category>devops</category>
      <category>cloud</category>
    </item>
    <item>
      <title>How I Built TERMINAL47: Anonymous Real-Time Chat with Live Translation (Next.js + Socket.io + TypeScript + Express)</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Mon, 23 Feb 2026 15:59:48 +0000</pubDate>
      <link>https://dev.to/blackwatch021/how-i-built-terminal47-anonymous-real-time-chat-with-live-translation-nextjs-socketio--2973</link>
      <guid>https://dev.to/blackwatch021/how-i-built-terminal47-anonymous-real-time-chat-with-live-translation-nextjs-socketio--2973</guid>
      <description>&lt;p&gt;Zero-friction chat app—no signup, link-only access, anonymous "Agent-X" names, set duration for room, auto room dispose once time is over, no data is saved/stored, pure anonymity, live language switching (history bulk-translates + new messages stream). Hacker terminal UI. V1 complete &lt;a href="https://github.com/BlackWatch021/lingodev_challenge" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ovjjemu3j2lh5edvmi9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ovjjemu3j2lh5edvmi9.png" alt=" " width="800" height="375"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Live chat with users sidebar, countdown timer, translation status.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hey devs!, I wanted to level up my skills, so I combined Socket.io + real-time multilingual chat + cyberpunk UI. Meet TERMINAL47.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Flow:&lt;/strong&gt; Share link → Join as anonymous agent → Chat → Switch English→Japanese mid-convo → All past messages instantly translate → New messages are translated in real time →Timer expires → Room erased forever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🛠️ Tech Stack (Production Architecture)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Frontend: Next.js 16 + TypeScript 
Backend: Express + Socket.io + Lingo.dev SDK
State: useState (local) + localStorage (userName)
Storage: None (pure ephemeral)
Deployment: Local dev → GitHub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1: Backend Setup (index.js)&lt;/strong&gt;&lt;br&gt;
Clean Express + Socket.io server with dedicated translation endpoints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js
import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
import socketEvent from "./socket/events.js"; // Room logic here
import languageTranslationBulk from "./utils/translationBulk.js";
import languageTranslationSingle from "./utils/translationSingle.js";

const app = express();
const server = createServer(app);
const io = new Server(server, { cors: { origin: process.env.CLIENT_URL }});

app.use(cors());
app.use(express.json());


app.post("/auth/translation/bulk", async (req, res) =&amp;gt; {
  const { messages, currentLanguage, translateTo } = req.body;
  const languageTranslationFunction = await languageTranslationBulk(
    messages, currentLanguage, translateTo
  );
  res.json({ success: true, data: languageTranslationFunction });
});

app.post("/auth/translation/chunk", async (req, res) =&amp;gt; {
  const { message, currentLanguage, translateTo } = req.body;
  const languageTranslationFunction = await languageTranslationSingle(
    message, currentLanguage, translateTo
  );
  res.json({ success: true, data: languageTranslationFunction });
});

socketEvent(io); // Room management
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Lingo.dev Integration&lt;/strong&gt;&lt;br&gt;
Bulk translation (history on language switch):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// utils/translationBulk.js -
import { LingoDotDevEngine } from "lingo.dev/sdk";

const languageTranslationBulk = async (messages, currentLanguage, translateTo) =&amp;gt; {
  const lingoDotDev = new LingoDotDevEngine({ apiKey: process.env.LINGO_DEV_API_KEY });
  const userMessages = messages.filter((msg) =&amp;gt; msg.text != "");

  const translated = await lingoDotDev.localizeText(userMessages, {
    sourceLocale: currentLanguage,
    targetLocale: translateTo,
  });
  return translated;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Frontend Hook (useChat.ts)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// hooks/useChat.ts
export const useChat = () =&amp;gt; {
  const [socket, setSocket] = useState&amp;lt;Socket | null&amp;gt;(null);
  const [roomStatus, setRoomStatus] = useState&amp;lt;boolean | null&amp;gt;(null);
  const [expiresAt, setExpiresAt] = useState&amp;lt;number | null&amp;gt;(null);

  const joinRoom = useCallback((roomId: string, userName: string) =&amp;gt; {
    socket?.emit("join_room", { roomId, userName }, (result: any) =&amp;gt; {
      if (result.success &amp;amp;&amp;amp; result.room) {
        const expiry = Date.now() + result.room.timeRemaining;
        setExpiresAt(expiry);
      }
    });
  }, [socket]);

  const sendMessage = useCallback((roomId: string, message: string) =&amp;gt; {
    socket?.emit("send_message", { roomId, message });
  }, [socket]);

  return { socket, joinRoom, sendMessage, roomStatus, expiresAt };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: 🔥 Live Translation Magic (Chat.tsx)&lt;/strong&gt;&lt;br&gt;
Hero feature—language switch + bulk/single translation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
  if (!allMessages.length || language === lastLanguage) return;

  setIsTranslating(true);
  const translateChat = async () =&amp;gt; {
    const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/auth/translation/bulk`, {
      method: "POST",
      body: JSON.stringify({
        messages: allMessages,
        currentLanguage: lastLanguage,
        translateTo: language,
      }),
    });
    const data = await response.json();
    setAllMessages(data.data); // All history translated!
  };
  translateChat();
}, [language]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9notctba4ohnjsg6miyy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9notctba4ohnjsg6miyy.png" alt=" " width="800" height="380"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Switch language → History bulk-translates + new messages auto-translate.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: SideNavBar (Users + Timer)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SideNavBar.tsx - Key features
const SideNavBar = ({ userCount, expiresAt, language, setLanguage, isTranslating }) =&amp;gt; {
  const [formattedTime, setFormattedTime] = useState("00:00");

  useEffect(() =&amp;gt; {
    if (!expiresAt) return;
    const tick = () =&amp;gt; {
      const remainingMs = expiresAt - Date.now();
      const totalSeconds = Math.max(0, Math.floor(remainingMs / 1000));
      const minutes = Math.floor(totalSeconds / 60);
      setFormattedTime(`${minutes.toString().padStart(2, '0')}:${(totalSeconds % 60).toString().padStart(2, '0')}`);
    };
    tick();
    const interval = setInterval(tick, 1000);
    return () =&amp;gt; clearInterval(interval);
  }, [expiresAt]);

  return (
    &amp;lt;aside className="w-12 md:w-64 bg-secondaryBackground"&amp;gt;
      &amp;lt;div&amp;gt;USERS- {userCount}&amp;lt;/div&amp;gt;
      &amp;lt;LanguageSelection language={language} setLanguage={setLanguage} /&amp;gt;
      &amp;lt;div className="timer"&amp;gt;{formattedTime}&amp;lt;/div&amp;gt; {/* Red border when &amp;lt;60s */}
    &amp;lt;/aside&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🚀 V1 Complete ✅&lt;/strong&gt;&lt;br&gt;
✅ Real-time chat (Socket.io)&lt;br&gt;
✅ User presence + system messages&lt;br&gt;
✅ Live translation (bulk + streaming)&lt;br&gt;
✅ Room expiry countdown&lt;br&gt;
✅ Hacker terminal UI&lt;br&gt;
✅ Anonymous access (no auth)&lt;br&gt;
✅ TypeScript everywhere&lt;/p&gt;

&lt;p&gt;Next: Typing indicators with Redis V2? Clone the repo and try language switching—feels like magic! 🪄&lt;br&gt;
&lt;a href="https://github.com/BlackWatch021/lingodev_challenge" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>showdev</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>QUICK SQL REVISION</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Sun, 04 Jan 2026 14:33:01 +0000</pubDate>
      <link>https://dev.to/blackwatch021/quick-sql-revision-2cj9</link>
      <guid>https://dev.to/blackwatch021/quick-sql-revision-2cj9</guid>
      <description>&lt;p&gt;While revising SQL, I realized that I keep making the same notes again and again. So I decided to document everything in one place for quick reference.&lt;/p&gt;

&lt;p&gt;As I was writing, I thought—why not share it as an article? It might help others who are learning SQL or revising for interviews.&lt;/p&gt;

&lt;p&gt;This article covers the core SQL concepts and is intended as a quick revision guide as well as an easy &lt;strong&gt;starting point for beginners&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is SQL?&lt;/strong&gt;&lt;br&gt;
SQL stands for Structured Query Language and is the standard language used to interact with relational databases. It allows us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create database structures like tables, views, and indexes.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Perform CRUD operations (Create, Read, Update, Delete) on the data stored in those tables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Define permissions and control access to the data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CRUD expands to:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;C – Create&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;R – Read&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;U – Update&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;D – Delete&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is a database?&lt;/strong&gt;&lt;br&gt;
A database is a structured collection of data stored in a digital system so that it can be efficiently accessed, managed, and updated. It acts as the backbone of most applications because it persists user and system data; without it, most apps would lose their state and real-world usefulness.&lt;br&gt;
Databases are broadly categorized into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relational databases&lt;/li&gt;
&lt;li&gt;Non-relational (NoSQL) databases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Relational databases&lt;/strong&gt;&lt;br&gt;
In relational databases, data is stored in tables (rows and columns) with a predefined schema. This structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Makes it easier and faster to query data using relationships (joins) between tables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Requires designing the schema up front, so a good understanding of the data model is needed before implementation.&lt;br&gt;
Examples:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MySQL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PostgreSQL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SQL Server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Oracle&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;*Relational databases use SQL as their primary query language&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Non-relational databases&lt;/strong&gt;&lt;br&gt;
Non-relational (NoSQL) databases do not rely on fixed tables and schemas like relational databases. Data can be stored as documents, key–value pairs, wide-column stores, or graphs. This:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Makes it easier and faster to get started because the schema can be more flexible or even schema-less.&lt;/li&gt;
&lt;li&gt;Can become harder to reason about as the data model grows if there is no consistent structure.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can scale very well horizontally for certain workloads, though query patterns and consistency guarantee differ from relational systems.&lt;br&gt;
Examples:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MongoDB (document-based)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis (key–value)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cassandra (wide-column)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Data Types&lt;/strong&gt;&lt;br&gt;
Before moving forward, it is important to understand the concept of data types in SQL. Like most programming languages, when creating a table, we must specify the type of each column so the database knows what kind of data it will store and how to validate it.&lt;br&gt;
Data types help:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enforce that data is stored in the correct format (numbers, text, dates, etc.).&lt;/li&gt;
&lt;li&gt;Optimize storage and performance by choosing appropriate sizes and representations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Most Common Data Types&lt;/strong&gt;&lt;br&gt;
SQL offers Different Data Types but here we will discuss about most common data types. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. CHAR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CHAR(n) is a fixed-length character type used to store strings with a maximum length of n characters, for example CHAR (21).&lt;/li&gt;
&lt;li&gt;The database reserves space for exactly n characters for each value; if the string is shorter, it may be padded with spaces internally.&lt;/li&gt;
&lt;li&gt;If you try to insert a string longer than n, it will either be truncated or cause an error, depending on the database and settings.
Use cases: values with fixed length such as country codes, status codes, or fixed-format identifiers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. VARCHAR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;VARCHAR(n) is a variable-length character type used to store strings up to n characters.&lt;/li&gt;
&lt;li&gt;Unlike CHAR, it only uses as much storage as needed for the actual string plus a small overhead, not the full declared length.&lt;/li&gt;
&lt;li&gt;If a value exceeds the specified maximum length, the database will usually throw an error or truncate based on configuration.
Use cases: general text fields like names, emails, addresses, titles, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. INT&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INT is used to store whole numbers (integers).&lt;/li&gt;
&lt;li&gt;Exact range depends on the database system and whether it is signed or unsigned, but it typically supports a large range of positive and negative values suitable for IDs, counts, and similar fields.
Use cases: IDs, counts, quantities, ages, and other whole-number values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. TINYINT&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TINYINT is a smaller integer type with a limited range.&lt;/li&gt;
&lt;li&gt;In MySQL, for example, TINYINT has a range of -128 to 127 when signed, and 0 to 255 when unsigned.&lt;/li&gt;
&lt;li&gt;It is useful when you know values will always remain within a small range, which can save space.
Use cases: small flags, status codes, or compact numeric values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. FLOAT&lt;/strong&gt;    &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FLOAT is a floating-point data type used to store approximate decimal numbers.&lt;/li&gt;
&lt;li&gt;It is called single-precision because it stores numbers using fewer bits, which means less precision compared to DOUBLE.&lt;/li&gt;
&lt;li&gt;In most databases, FLOAT can accurately represent about 6–7 significant digits.&lt;/li&gt;
&lt;li&gt;Because values are stored in binary form, some decimal numbers cannot be represented exactly, which may result in small rounding errors.&lt;/li&gt;
&lt;li&gt;Due to this approximation, FLOAT should not be used for values where exact precision is required (such as money).
Use cases: Measurements, percentages, averages, sensor data, or scientific values where minor rounding differences are acceptable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. DOUBLE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DOUBLE (or DOUBLE PRECISION) is a floating-point data type used to store approximate decimal numbers with higher precision than FLOAT.&lt;/li&gt;
&lt;li&gt;It uses more bits to store each value, allowing for about 15–16 significant digits of precision in most systems.&lt;/li&gt;
&lt;li&gt;DOUBLE supports a wider range and greater accuracy than FLOAT, making it suitable for more complex calculations.&lt;/li&gt;
&lt;li&gt;Like FLOAT, it still stores values approximately, so exact precision is not guaranteed.
Use Cases: Scientific computations, complex calculations, analytics, and scenarios where higher precision is needed but exact decimal accuracy is not mandatory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7. Boolean&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conceptually, a Boolean represents two logical values: true and false.&lt;/li&gt;
&lt;li&gt;Many SQL dialects have a BOOLEAN or BOOL type; however, the actual storage may be implemented internally as a small integer (for example, TINYINT(1) in MySQL).&lt;/li&gt;
&lt;li&gt;In systems that represent Boolean as integers, the common convention is 0 for false and 1 for true.
Use cases: flags like is_active, is_deleted, is_admin, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;8.  DECIMAL / NUMERIC&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DECIMAL (also called NUMERIC in many SQL dialects) is used to store exact decimal numbers.&lt;/li&gt;
&lt;li&gt;Unlike FLOAT and DOUBLE, DECIMAL stores values in base-10, which means decimal numbers are stored exactly as written, without rounding errors.&lt;/li&gt;
&lt;li&gt;DECIMAL (p, s) defines precision (total digits) and scale (digits after decimal).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Use FLOAT/DOUBLE for measurements and calculations, but DECIMAL for money and financial data.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Use cases: Monetary values, prices, salaries, financial calculations, totals, taxes, and any scenario where precision must be exact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Signed and unsigned integers&lt;/strong&gt;&lt;br&gt;
Sometimes you know a column will never store negative values, such as IDs, counts, or quantities.&lt;br&gt;
• Signed integers can store both negative and positive values (for TINYINT in MySQL: -128 to 127).&lt;br&gt;
• Unsigned integers only store non-negative values but shift the range upward (for TINYINT in MySQL: 0 to 255).&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="nb"&gt;TINYINT&lt;/span&gt; &lt;span class="nb"&gt;UNSIGNED&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, _id can only store values from 0 to 255, which is enough for a small set of teacher IDs and saves space compared to a larger integer type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Commands in SQL&lt;/strong&gt;&lt;br&gt;
We know that SQL is a language which is used to interact with database, there are various types of commands with each having its own function, now these commands can be classified into 5 types, and these are –&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. DDL (Data Defining Language)&lt;/strong&gt;&lt;br&gt;
Purpose: Define and manage database structure (tables, schemas).&lt;br&gt;
Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CREATE - create tables/databases&lt;/li&gt;
&lt;li&gt;ALTER - modify existing tables&lt;/li&gt;
&lt;li&gt;DROP - delete tables/databases&lt;/li&gt;
&lt;li&gt;TRUNCATE - delete all data (keep structure)&lt;/li&gt;
&lt;li&gt;RENAME - rename objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. DQL (Data Query Language)&lt;/strong&gt;&lt;br&gt;
This is used to retrieve data from tables. We can apply conditions too, to get specific data.&lt;br&gt;
Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SELECT - fetch data with conditions, joins, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. DML (Data Manipulation Language)&lt;/strong&gt;&lt;br&gt;
This is used to manipulate the data present/stored inside the table. We can CRUD operations;&lt;br&gt;
Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INSERT - add new data&lt;/li&gt;
&lt;li&gt;UPDATE - modify existing data&lt;/li&gt;
&lt;li&gt;DELETE - remove data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. DCL (Data Control Language)&lt;/strong&gt;&lt;br&gt;
This is used to grant and revoke permissions to users. As the data grows, we may also add sensitive data in it or there may be data which isn’t needed by one user. So, we can limit what the user is allowed to access to maintain security.&lt;br&gt;
Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GRANT - give permissions&lt;/li&gt;
&lt;li&gt;REVOKE - remove permissions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. TCL (Transaction Control Language)&lt;/strong&gt;&lt;br&gt;
One of the most important concepts, although it is used massively in applications. Where it ensures that whole operation is done as per the instructions other wise revert back to the last check point&lt;br&gt;
Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BEGIN/COMMIT - save changes&lt;/li&gt;
&lt;li&gt;ROLLBACK - undo changes&lt;/li&gt;
&lt;li&gt;SAVEPOINT - partial rollback points&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Basic Command (Create table)&lt;/strong&gt;&lt;br&gt;
As we know that SQL is a language and everything is done using commands. So here is the most basic command. Which will help us to understand other concepts. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List all Databases&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;DATABASES&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use a specific database&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;USE&lt;/span&gt; &lt;span class="n"&gt;database_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;List all tables in current DB&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;TABLES&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;View table structure&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create table (DDL)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;column_1&lt;/span&gt; &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;column_2&lt;/span&gt; &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;column_3&lt;/span&gt; &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ex-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;- &lt;br&gt;
&lt;em&gt;Here PRIMARY KEY is a key&lt;br&gt;
AUTO_INCREMENT, NOT NULL are constraints&lt;br&gt;
We will learn about these in future.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyt977zw2vujiygzkdqtj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyt977zw2vujiygzkdqtj.jpg" alt="Table Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DELETE DATABASE/TABLE&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;database_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;KEYS&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;PRIMARY KEY (always unique)&lt;/strong&gt;&lt;br&gt;
A column (or set of columns) in a table that uniquely identifies each row (&lt;strong&gt;a unique id&lt;/strong&gt;)&lt;br&gt;
Only one Primary key can be there in a table and it can’t be null.&lt;/p&gt;

&lt;p&gt;Ex-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="err"&gt;……&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;FOREIGN KEY&lt;/strong&gt;&lt;br&gt;
It’s always a good idea to divide data into small tables, rather than keeping everything inside a single table which is a monolith architecture. Now as we divide the tables, we need to establish some sort of relation in between them which allows us to get the data. FOREIGN key in a table refers to the PRIMARY key of another table and helps in establishing that link between tables.&lt;br&gt;
FOREIGN KEY References PRIMARY KEY of another table&lt;br&gt;
A table can have multiple FKs. &lt;br&gt;
Foreign keys may contain null or duplicate values.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parent table = table with PRIMARY KEY&lt;/li&gt;
&lt;li&gt;Child table = table with FOREIGN KEY&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example&lt;br&gt;&lt;br&gt;
A teacher table is in relation with department table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;  

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;dept_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

   &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dept_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;dept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here dept is the Parent table while teacher is the Child table (using Foreign Key) and dept_id in teacher table is the FOREIGN KEY, which establishes relation between these two tables. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CASCADE in FOREIGN key&lt;/strong&gt;&lt;br&gt;
FOREIGN key is used to set up relation between tables. Now there may be a need of change in both tables if anything is changed in parent table, for that we use CASCADING &lt;/p&gt;

&lt;p&gt;So, any change(s) in parent table is automatically reflected in children table too.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ON UPDATE CASCADE → Parent ID changes → Child FK updates automatically&lt;/li&gt;
&lt;li&gt;ON DELETE CASCADE → Parent row deleted → Child rows deleted automatically
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;teachers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;dept_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

   &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dept_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;dept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;
   &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;CONSTRAINTS&lt;/strong&gt;&lt;br&gt;
Now let’s talk about &lt;strong&gt;CONSTRAINTS&lt;/strong&gt;, while creating a table we can specify some rules for the columns in the table. Like the values can never be null, we can specify the length, allow duplicate values or not. All these rules are applied using CONSTRAINTS. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some Commonly used CONSTRAINTS&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;1. NOT NULL&lt;/strong&gt; – This is used to specify that the column can’t be null. There should be a value in it all the time. &lt;br&gt;
&lt;strong&gt;2. UNIQUE&lt;/strong&gt; – In case we want to have unique values in a column, then we use this. &lt;br&gt;
&lt;strong&gt;3. DEFAULT&lt;/strong&gt; – This is used to set a default value, in case no value is provided, then the set default value is used. &lt;br&gt;
&lt;strong&gt;4. CHECK&lt;/strong&gt; - This is used to check or set the permissible value in the column &lt;/p&gt;

&lt;p&gt;Ex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'ABCD'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;SELECT statement&lt;/strong&gt;&lt;br&gt;
Till now we learned about databases, their types, data types, types of commands, how to create a table, concept of keys and constraints. Now we will dive into the ocean of commands, how we can actually use the commands and play with data and to do so, first and foremost thing we have is the SELECT statement, it is used to retrieve data from the table and return it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;col1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col3&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the above command will return whole table as the result, * means all. &lt;/p&gt;

&lt;p&gt;Targeting specify column in a table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above command will return a table with only id, name and city column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Column aliases (AS)&lt;/strong&gt;:&lt;br&gt;
In case we want to change “name” to “first_name” in the resultant table, then we can use AS command to do so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the resultant table, now the columns will be id, first_name, city. But this won’t affect the actual table in anyways. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CONDITIONS – WHERE clause&lt;/strong&gt;&lt;br&gt;
What if we need specific data from the table, which satisfies certain conditions. To do so, we use WHERE clause with the SELECT statement. WHERE clause allows us to set conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;col1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col4&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;   &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WHERE clause doesn’t work alone, conditions are set with the help of &lt;strong&gt;OPERATORS&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OPERATORS&lt;/strong&gt;&lt;br&gt;
OPERATORS are the backbone of SQL, as they allow us to actually set the conditions as per our requirements. &lt;br&gt;
There are various OPERATORS to choose from, and these are the most commonly and important to know OPERATORS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Arithmetic OPERATORS:  *, -, +, /, %&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;age_divided_by_two&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;MINOR&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Comparison OPERATORS: =, !=, &amp;gt;, &amp;gt;=, &amp;lt;, &amp;lt;=&lt;/strong&gt;&lt;br&gt;
These are used to do comparison&lt;br&gt;
*Note: != means not equal to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'New York'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. BITWISE OPERATORS: &amp;amp; (Bitwise AND), | (Bitwise OR)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Logical OPERATORS: AND, OR, NOT, IN, BETWEEN, ALL, LIKE.&lt;/strong&gt;&lt;br&gt;
These are logical in nature&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AND operator&lt;/strong&gt;: This checks if both (right and left) conditions are true, then only it satisfies the conditions.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;OR operator&lt;/strong&gt;: Similar to AND operator but this checks if either of the conditions is true.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'Wick'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BETWEEN operator&lt;/strong&gt;: Used to set a range for the result.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;92&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IN operator&lt;/strong&gt;: It matches any value specified inside it.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'New York'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Moscow'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'New Delhi'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will only return the rows, where the city is New York, Moscow, or New Delhi only. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NOT&lt;/strong&gt;: To negate the given condition
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'New York'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Moscow'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'New Delhi'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will return all the rows except the rows where the city is New York, Moscow, or New Delhi&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LIMIT&lt;/strong&gt;: Used to set the limit of returned rows of the table.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, the result will contain only 5 rows at a time, even if it contains 100 rows. It will only be returning 5 rows at a time. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;OFFSET&lt;/strong&gt;: This one is used to skip certain values and start after them. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;LIMIT&lt;/strong&gt; and &lt;strong&gt;OFFSET&lt;/strong&gt; are used together and these two OPERATORS make pagination in web page possible. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ORDER BY&lt;/strong&gt;: This gives us the facility to sort the result as per our choice. Either Descending or Ascending.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt; 
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;ASC= Ascending&lt;br&gt;
DESC= Descending&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Rome'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Tokyo'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;OFFSET&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;AGGREGATE FUNCTIONS&lt;/strong&gt;&lt;br&gt;
These are prebuilt functions/methods which helps us by easing our work, these returns single value as a result.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;COUNT()&lt;/strong&gt;   -  Counts the number of entries&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MAX()&lt;/strong&gt;  -  Maximum value in column&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIN()&lt;/strong&gt;  -  Minimum value in column&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SUM()&lt;/strong&gt;  -  Total sum of values&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AVG()&lt;/strong&gt;  -  Average of values.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Find the max age-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;MaxAge&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

     &lt;span class="k"&gt;OR&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GROUP BY clause&lt;/strong&gt; &lt;br&gt;
This is used to group rows that have same values into summary rows. &lt;br&gt;
Generally used with some aggregate function. Both of these goes by hand in hand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;SPID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt; 
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;SPID&lt;/span&gt; 
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;SPID&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;HAVING clause&lt;/strong&gt;&lt;br&gt;
Similar to WHERE clause but used when we want to apply condition after grouping.&lt;br&gt;
It was introduced to address the limitations of WHERE clause, which cannot be used with aggregate functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; 
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GENERAL SEQUENCE/ORDER of writing query&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="k"&gt;table_Name&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'A'&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;TABLE Related Queries&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Create Table&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;column_1&lt;/span&gt;  &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;column_2&lt;/span&gt;  &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;column_3&lt;/span&gt;  &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ex-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; 
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Insert data&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;tablename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;col1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;col2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;col3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;.)&lt;/span&gt; 
&lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;val3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;.);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Wick'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'7878787878'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you know the order then you may omit the column names&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Wick'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'7878787878'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we have used AUTO_INCREMENT for id. Which means that the value will increase by its own. To do that, we have to use the columns name and omit the AUTO_INCREMENT column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Wick'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'7878787878'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, id will be populated on its own.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Insert more than one value/row in table&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Wick'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'7878787878'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Super'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Man'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'8456985'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Bat'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Man'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2234234'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Iron'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Man'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'23584839'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Captain'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'America'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'284738473/, 50);
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Update table&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt; 
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;col1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;val1&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; 
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'SUPER'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Delete data&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Delete'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. ALTER TABLE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ADD column&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="n"&gt;datatype&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;grades&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DROP/DELETE Column&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="k"&gt;column_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;grades&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RENAME table&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;newtable_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;newStudents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CHANGE column&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="n"&gt;CHANGE&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;old_name&lt;/span&gt; &lt;span class="n"&gt;new_name&lt;/span&gt; &lt;span class="n"&gt;new_datatype&lt;/span&gt; &lt;span class="n"&gt;new_constraint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; 
&lt;span class="n"&gt;CHANGE&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MODIFY column (modify datatype/constraint)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="n"&gt;new_datatype&lt;/span&gt; &lt;span class="n"&gt;new_constraint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DELETE TABLE data (only table data, not whole table)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;TRUNCATE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;TRUNCATE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;JOINS&lt;/strong&gt;&lt;br&gt;
One of the most important topics, these are used to combine rows from two or more tables, based on a related column between them. &lt;/p&gt;

&lt;p&gt;Types&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inner Join&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Outer Join&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Left Join&lt;/li&gt;
&lt;li&gt;Right Join&lt;/li&gt;
&lt;li&gt;Full Join&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1. INNER JOIN&lt;/strong&gt;&lt;br&gt;
Returns records that have matching values in both tables (only mutual data is returned)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqb2zm2q8w8wmo0c5940a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqb2zm2q8w8wmo0c5940a.png" alt="Inner join diagram"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table_A&lt;/span&gt; 
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;table_b&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;course&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;course&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;This will return only the data which is present in both tables&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTER JOINS&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. LEFT Join&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Returns all records from left table and matched records from the right table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6kfzcqkbh6nkmdo6mo3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6kfzcqkbh6nkmdo6mo3.jpg" alt="Outer join diagram"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tableb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. RIGHT Join&lt;/strong&gt;&lt;br&gt;
Same as LEFT join, only side changes. This return all records from right table and matched records from left table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6s6plaf3bou6l8ug0ad.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6s6plaf3bou6l8ug0ad.jpg" alt="Right join diagram"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tableb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. FULL Join&lt;/strong&gt;&lt;br&gt;
Returns all records even if there is no relation.&lt;br&gt;
By default, MySQL doesn't support FULL JOIN so we do it through another method and with using UNION.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UNION&lt;/strong&gt;&lt;br&gt;
Helps in combining data and returns unique values only.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FULL JOIN = LEFT JOIN _UNION _RIGHT JOIN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ud33a1fvsgcdsh5ooma.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ud33a1fvsgcdsh5ooma.jpg" alt="Full join diagram"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;column&lt;/span&gt;

&lt;span class="k"&gt;UNION&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;??Write SQL command to display the left exclusive join: -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10ljmpklszns04j7obti.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10ljmpklszns04j7obti.jpg" alt="Left exclusive join diagram"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;*Similar for RIGHT exclusive Join.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SELF JOIN&lt;/strong&gt;&lt;br&gt;
A regular join but the table is joined with itself&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;col_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;UNION&lt;/strong&gt;&lt;br&gt;
Used to combine the result-set of two or more SELECT statements.&lt;br&gt;
Gives unique records.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;*Columns must have similar data types.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableA&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tableB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;We also have UNION all, this allows duplicates on top of UNION's data&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SUBQUERY&lt;/strong&gt;&lt;br&gt;
Subquery or inner query or a nested query is a query within another SQL query. This allows us to do complex operations which isn't or would be hard to do otherwise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;col_name&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subquery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;MySQL Views&lt;/strong&gt;&lt;br&gt;
It's a virtual table created from an actual/real table.&lt;br&gt;
Imagine a student table with lots of columns. Now a teacher only needs students roll number, name, marks. Other data isn't needed to the teacher. &lt;/p&gt;

&lt;p&gt;So, a view will be created from the student table which will contain only these columns.&lt;/p&gt;

&lt;p&gt;Although it's a virtual table but it will act just like a real table, operations, select command etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CREATE VIEW&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;view_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;view1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;rollno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;


&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;view1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DROP VIEW&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;view_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;view1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>sql</category>
      <category>database</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Wanna know the working of Video calling apps (zoom, google meet, microsoft teams) how they work, here is a quick article on it. What goes behind a video call, how things work, its architecture. Feel free to give it a try😁</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Thu, 17 Jul 2025 05:51:38 +0000</pubDate>
      <link>https://dev.to/blackwatch021/wanna-know-the-working-of-video-calling-apps-zoom-google-meet-microsoft-teams-how-they-work-2dlp</link>
      <guid>https://dev.to/blackwatch021/wanna-know-the-working-of-video-calling-apps-zoom-google-meet-microsoft-teams-how-they-work-2dlp</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/blackwatch021/webrtc-the-brain-behind-your-virtual-video-calls-1o7" class="crayons-story__hidden-navigation-link"&gt;WebRTC, the Brain Behind Your Virtual Video Calls&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/blackwatch021" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F753337%2F1a960c31-5c88-416d-81b4-aa1c262d5029.jpeg" alt="blackwatch021 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/blackwatch021" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Blackwatch
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Blackwatch
                
              
              &lt;div id="story-author-preview-content-2694210" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/blackwatch021" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F753337%2F1a960c31-5c88-416d-81b4-aa1c262d5029.jpeg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Blackwatch&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/blackwatch021/webrtc-the-brain-behind-your-virtual-video-calls-1o7" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Jul 16 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/blackwatch021/webrtc-the-brain-behind-your-virtual-video-calls-1o7" id="article-link-2694210"&gt;
          WebRTC, the Brain Behind Your Virtual Video Calls
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webrtc"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webrtc&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/beginners"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;beginners&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/blackwatch021/webrtc-the-brain-behind-your-virtual-video-calls-1o7#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            6 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>webrtc</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>WebRTC, the Brain Behind Your Virtual Video Calls</title>
      <dc:creator>Blackwatch</dc:creator>
      <pubDate>Wed, 16 Jul 2025 14:38:39 +0000</pubDate>
      <link>https://dev.to/blackwatch021/webrtc-the-brain-behind-your-virtual-video-calls-1o7</link>
      <guid>https://dev.to/blackwatch021/webrtc-the-brain-behind-your-virtual-video-calls-1o7</guid>
      <description>&lt;p&gt;Ever wondered what goes behind apps like Google Meet or Zoom? How do they make it so easy for us to communicate through video, even when we're sitting on opposite sides of the planet?&lt;/p&gt;

&lt;p&gt;Yeah, I came across this while building an app for myself. That’s when I learned more about WebRTC—how it works, what goes on behind the scenes, what its architecture looks like, its drawbacks, and how to overcome them. In this article, I’ll give you a brief intro to WebRTC and its working principles. So stay tuned to dive deep into the world of video calling.&lt;/p&gt;

&lt;h2&gt;
  
  
  WebRTC—What Is This?
&lt;/h2&gt;

&lt;p&gt;It’s a protocol used to connect users directly so they can communicate, share files, stream video, and more.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;But aren’t there other protocols for that? What’s so special about this one?&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Right, there are many protocols. But WebRTC shines because of &lt;strong&gt;its ease of use&lt;/strong&gt;, and the fact that you &lt;strong&gt;don’t need a dedicated server to monitor or relay the entire communication&lt;/strong&gt;. It’s based on the UDP protocol.&lt;/p&gt;

&lt;p&gt;Yes, you heard it right—WebRTC doesn’t need a third-party or cloud-based server for actual media transfer. It establishes a &lt;strong&gt;direct peer-to-peer connection&lt;/strong&gt; between users, which also makes it faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  So Why Don’t We Use This Everywhere?
&lt;/h2&gt;

&lt;p&gt;You might have the same question in your head. The reason is packet loss.&lt;/p&gt;

&lt;p&gt;WebRTC is based on &lt;strong&gt;UDP&lt;/strong&gt;, which is fast, but not reliable—it doesn’t guarantee that all data packets will reach the destination. That’s fine for things like Zoom meetings or online games, where a small data loss (a freeze or glitch) doesn’t affect much.&lt;/p&gt;

&lt;p&gt;But for &lt;strong&gt;messaging apps&lt;/strong&gt;, we can’t afford to lose even a single packet—missing words or characters can change the entire meaning of a message. So for these cases, we use &lt;strong&gt;TCP&lt;/strong&gt;, which ensures reliable and ordered delivery of data.&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use UDP/WebRTC for real-time media (video/audio).&lt;/li&gt;
&lt;li&gt;Use TCP when all data must be received correctly (text, files).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How It Works – Architecture
&lt;/h2&gt;

&lt;p&gt;Before diving in, let’s imagine a situation:&lt;br&gt;
You want to call your friend, family member, boss (to ask for leave 😁), or your wife (to plan a day out). To do that, you need their phone number. Only then can you call them.&lt;/p&gt;

&lt;p&gt;Similarly, in WebRTC, you first need to know the IP address of the person you want to connect with. But before that, let’s understand &lt;strong&gt;public and private IP addresses&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Public and Private IP Addresses
&lt;/h2&gt;

&lt;p&gt;Just like each SIM card has a unique number, every device needs a unique address on the internet to send and receive data.&lt;/p&gt;

&lt;p&gt;With the rapid growth in internet users, &lt;strong&gt;assigning a unique public IP to every device&lt;/strong&gt; isn't possible (due to IPv4 exhaustion). So instead, most devices are assigned &lt;strong&gt;private IP addresses&lt;/strong&gt; by routers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public IP Address&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is what allows your device to access the internet.&lt;/li&gt;
&lt;li&gt;It’s unique globally and exposed to the outer world.&lt;/li&gt;
&lt;li&gt;Typically, only the router is assigned a public IP, and it communicates with the internet on behalf of all devices connected to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;No two devices on the internet can have the same public IP.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Private IP Address&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assigned to devices inside a local network (by the router).&lt;/li&gt;
&lt;li&gt;Can’t directly access the internet.&lt;/li&gt;
&lt;li&gt;Managed through a process called NAT (Network Address Translation).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So how do you surf the internet with a private IP?&lt;/p&gt;

&lt;p&gt;Here’s how:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When your device makes a request (e.g., opens a website), it goes to the router.&lt;/li&gt;
&lt;li&gt;The router forwards this request using its public IP.&lt;/li&gt;
&lt;li&gt;When the response comes back, it knows which device made the request and sends the data accordingly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feidwxtl0oztrog01jpeh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feidwxtl0oztrog01jpeh.png" alt="Private and Public IP addresses" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TURN/ICE Servers
&lt;/h2&gt;

&lt;p&gt;Now that you know about private and public IPs, let’s talk about &lt;strong&gt;TURN and ICE servers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;After a device gets its private IP, it still doesn’t know its public IP, which is needed to establish a connection. That’s where these come in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;STUN/TURN servers&lt;/strong&gt; are simple services that see the public IP of incoming requests and send it back to the client.&lt;/li&gt;
&lt;li&gt;This way, the client becomes aware of its &lt;strong&gt;public IP address&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ICE (Interactive Connectivity Establishment) is a framework that uses STUN and TURN to find the best route between two peers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fps63i0h82xv2mhz4sg29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fps63i0h82xv2mhz4sg29.png" alt="TURN server" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Signaling Server
&lt;/h2&gt;

&lt;p&gt;At this point, each user has the necessary information to establish a WebRTC connection—like codecs, IPs, ports, etc. This data is packaged in something called a &lt;strong&gt;Session Description Protocol&lt;/strong&gt; (SDP).&lt;/p&gt;

&lt;p&gt;But here's the catch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You don’t have the other user’s SDP.&lt;/li&gt;
&lt;li&gt;They don’t have yours.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, we need a &lt;strong&gt;signaling server&lt;/strong&gt; to exchange this information.&lt;br&gt;
Don’t worry—it’s a &lt;strong&gt;simple server&lt;/strong&gt; that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Takes the SDP of User A and sends it to User B.&lt;/li&gt;
&lt;li&gt;Takes the SDP of User B and sends it to User A.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now both users have each other’s details and can establish a &lt;strong&gt;direct WebRTC connection&lt;/strong&gt;. After that, the signaling server isn’t needed anymore.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfwpnpb3ij9pagmwsuf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfwpnpb3ij9pagmwsuf1.png" alt="Signalling server" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Drawbacks
&lt;/h2&gt;

&lt;p&gt;Since WebRTC is peer-to-peer, &lt;strong&gt;only two users&lt;/strong&gt; can connect at a time by default. You can’t host large video conferences using just this architecture.&lt;/p&gt;

&lt;p&gt;This is perfect for apps like &lt;strong&gt;Omegle&lt;/strong&gt;, where only two users need to connect. But for more users, we need other methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mesh Topology (Not Ideal)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7mtnzixwm8xjoncg56o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7mtnzixwm8xjoncg56o.png" alt="Mesh Topology" width="800" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In mesh, each user connects directly to every other user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: In a 4-user call, each user maintains 3 separate connections.&lt;/p&gt;

&lt;p&gt;This quickly becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bandwidth-heavy&lt;/li&gt;
&lt;li&gt;Hard to scale&lt;/li&gt;
&lt;li&gt;Difficult to keep all streams in sync&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why mesh is rarely used in production apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution – SFU (Selective Forwarding Unit) and Multipoint Control Units (MCUs)
&lt;/h2&gt;

&lt;p&gt;To solve the issue of scalability in WebRTC for group calls, we use &lt;strong&gt;SFU (Selective Forwarding Unit)&lt;/strong&gt; or &lt;strong&gt;MCU (Multipoint Control Unit)&lt;/strong&gt; as per the requirement. In both cases, we introduce a server into the architecture.&lt;/p&gt;

&lt;p&gt;In these setups, a &lt;strong&gt;server acts like a virtual client&lt;/strong&gt;, and all the users are connected to this server using a P2P configuration. This server handles the media streams from all users. In case of SFU, it simply forwards the streams to other users. In MCU, it combines all the streams into one and sends it back. This makes the data easier to manage, keeps it in sync, and helps in scaling the system to support many users at once. This is how big platforms like &lt;strong&gt;Google Meet, Zoom, Discord, Microsoft Teams, etc.&lt;/strong&gt;, manage large group calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  SFU (Selective Forwarding Unit)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0r3xr36cafhykll0e88.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0r3xr36cafhykll0e88.webp" alt="SFU" width="720" height="569"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In SFU, each user sends their video/audio stream to the server, and the server’s job is just to **forward those streams **to the other users. It doesn’t mix or edit the media—it just acts like a smart postman.&lt;/p&gt;

&lt;p&gt;This way, each user only needs to upload their stream once, and the server takes care of distributing it to everyone else. Since the server isn’t doing any processing, this setup is &lt;strong&gt;lightweight and scalable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But there’s a catch. Each user still receives multiple streams—one from each participant—so the client device has to handle that load. Still, it’s much better than mesh topology, where everyone connects with everyone directly.&lt;/p&gt;

&lt;p&gt;That’s why SFU is the go-to choice for &lt;strong&gt;group video calls&lt;/strong&gt; in real-world apps. It's efficient, requires less server power, and performs well even with 10, 20, or more users.&lt;/p&gt;

&lt;h2&gt;
  
  
  MCU (Multipoint Control Unit)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl0xagybyk3s2qv8sdd3r.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl0xagybyk3s2qv8sdd3r.webp" alt="MCU" width="800" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this setup, things work a bit differently. All users send their streams to the server, just like in SFU. But here, the server does a lot more—it &lt;strong&gt;mixes all the streams into one combined video&lt;/strong&gt; (usually a grid of faces) and sends that &lt;strong&gt;single stream back&lt;/strong&gt; to every user.&lt;/p&gt;

&lt;p&gt;This keeps everything perfectly in sync and makes life easier for the client. Each device only needs to handle one incoming stream. But this also means the &lt;strong&gt;server is doing all the heavy lifting&lt;/strong&gt;—mixing, encoding, and streaming—which requires a lot more processing power and resources.&lt;/p&gt;

&lt;p&gt;MCU setups are great for situations like &lt;strong&gt;webinars or classrooms&lt;/strong&gt;, where it’s more important for everything to be in sync, and where users don’t interact much.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With that, this article comes to an end. I hope you learned something new—because this was new to me too, and I thought, why not share my understanding with others?&lt;/p&gt;

&lt;p&gt;Thanks for reading, and I hope you enjoyed the article.&lt;/p&gt;

&lt;p&gt;Till we meet again—seeya, have a great day, keep learning, peace out.&lt;/p&gt;

&lt;p&gt;Signing off.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>webrtc</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
