notesonly.in

One notebook for every subject — open it anywhere.

Log in

Databases for Web Applications: Complete Beginner's Guide

A database is the organized store where a web application keeps its data — user accounts, posts, orders, and everything else that must survive after the user closes the browser. While the frontend shows information and the backend processes it, the database is the permanent memory. Choosing and using the right database correctly decides whether an app is fast, reliable, and able to grow.

Data is the most valuable part of most apps, and database mistakes are the hardest to fix: a missing index turns a fast page into a 30-second wait; a lost write loses a customer's order. Every web developer needs to understand how databases store, find, and protect data.

Key Concepts

  • Relational (SQL) databases: data in tables with rows and columns, queried with SQL. Examples: PostgreSQL, MySQL. Best for structured data and complex relationships.
  • NoSQL databases: flexible formats — documents (MongoDB), key-value (Redis), graphs. Best for changing or hierarchical data.
  • Primary key: a unique identifier for each row, like a user id, used to find and link records.
  • Index: a lookup structure (usually a B-tree) finding rows without scanning the whole table — like a book's index.
  • ACID: the guarantee that transactions are Atomic, Consistent, Isolated, and Durable — critical for money and orders.
  • Connection pool: a set of reused database connections shared by the app, since opening a new connection per request is slow.

How It Works

  1. The app borrows a connection from the pool instead of opening a new one.
  2. It sends a query, such as finding a user by email.
  3. With an index on email it jumps straight to the row; without one, it scans every row.
  4. For writes, it records the change in a transaction log first, so a crash cannot lose committed data (the D in ACID).
  5. The result returns to the app, and the connection goes back to the pool for reuse.

Worked Example

A busy site receives 200 requests per second, and each request holds a database connection for 100 milliseconds (0.1 seconds). Concurrent connections needed = 200 × 0.1 = 20. A pool of 25 connections handles this comfortably with headroom for spikes. If traffic doubles to 400 requests per second, the need becomes 400 × 0.1 = 40 connections, so the pool must grow — or requests will queue and the site will slow down. This simple multiplication (known as Little's Law) is how engineers size pools instead of guessing.

Common Misconceptions

  • "NoSQL is always faster than SQL." Each excels at different shapes of data. A document store is fast for nested profiles; a relational database with proper indexes is often faster for reporting across related tables.
  • "More indexes always speed things up." Indexes speed up reads but slow down every write, because each index must be updated too. Index the columns you search by; do not index everything.
  • "The database handles backups automatically." Managed services can automate backups, but you must enable and verify them — an untested backup is not a backup.

Real-World Applications

Relational databases run banking, e-commerce checkouts, and booking systems where correctness matters most — PostgreSQL and MySQL dominate here. Document databases like MongoDB power content apps and catalogs with flexible data. Redis caches sessions and leaderboards for speed at companies like Twitter. Students typically start with SQLite for learning and PostgreSQL for projects, since the skills transfer directly to industry jobs.

To recap: databases are a web app's permanent memory. SQL suits structured, relational data with ACID guarantees; NoSQL suits flexible, hierarchical data. Index what you search, pool your connections, size the pool with simple math, and always verify your backups.