Topic 38

Who Actually Talks to the Database

Apps

The Marquee's website is live. A regular pulls out her phone, taps "My bookings", and half a second later her seats for Friday's Night Bus are on the screen. She typed no SQL. Nobody at the Marquee typed any either. And yet a SELECT ran, against the bookings table you know by heart — because somewhere between her tap and the rows, a program spoke SQL on her behalf.

That program is the missing picture this page fills in. Applications talk to databases through connections, and over those connections they send the very same language you have spent eight chapters learning. Nothing new gets invented here; what changes is the map. By the end of this page you will know exactly where your knowledge sits inside a real system.

The Three-Layer Picture

Almost every application you have ever used is built in three layers. At one end is the browser — the customer's phone or laptop, showing buttons and pages. At the other end is the database — the Marquee schema, guarded as ever. And in the middle sits the application server: a server being, as always, a program that runs all the time, waiting for requests and answering them. This one's requests are clicks and taps, and its answers are web pages.

The rule that makes the whole picture snap into place: only the middle layer talks to the database. The browser never does. When the customer taps "My bookings", her phone sends a message to the application, the application queries the database, and the application sends back a finished page. She connects to the app; the app connects to the database. Two different doors, and — as the page after next will show — two different sets of keys.

What Is a Connection?

Think of the phone line between a restaurant and its produce supplier. The kitchen doesn't buy a new telephone for every order; there is one established line, and orders are spoken over it all day long. A database connection is that line: the application dials the database once, using an address and credentials, the database answers and checks who's calling, and from then on SQL is spoken over the open line. Queries go one way, rows come back the other. That is the whole mechanism — from here on, we'll just say connection.

The application doesn't dial by hand. Each programming language has a driver — a small library that knows how to speak a particular database engine's wire language, so the application can say "run this SQL" and the driver handles the rest. You never see the driver's work; you only ever see what goes in (your query) and what comes out (rows).

The dialing information itself is usually packed into a single line called a connection string. It is worth seeing one once, because you will meet them constantly in real projects:

A connection string — who to dial, and as whom (password redacted)
postgresql://marquee_site:••••••••@db.marquee.example:5432/marquee

Read it left to right: which engine to speak (postgresql), which account is calling (marquee_site), the secret that proves it (redacted here, and treated like a house key in real life), which machine to dial and on which port (db.marquee.example:5432), and which database on that machine (marquee). One line, everything the app needs to open its line to the data.

One Page View, Traced

Now the payoff. Here is roughly what the Marquee application does when that "My bookings" tap arrives — written as pseudocode, meaning it is the shape of the program in plain words, not any real programming language:

Pseudocode — the "My bookings" page handler (the shape, not a real language)
when the browser asks for "/my-bookings":
    customer = the logged-in customer            -- her id is 214
    rows = connection.run("
        SELECT f.title, s.starts_at, b.seat
          FROM bookings b
          JOIN screenings s ON b.screening_id = s.screening_id
          JOIN films f      ON s.film_id = f.film_id
         WHERE b.customer_id = 214")
    send back a web page built from those rows

Look at the middle of it. That is not "like" the queries you have written — it is one: a three-table JOIN from Chapter 4, filtered by a customer id, the exact query Chapter 8 made fast with an index. The application contributes the before and after: it figures out who is asking, and it dresses the returned rows in HTML. The question in the middle is yours.

One tap, traced end to end — your query at the center hop
Browsertap → request to the app
App serverbuilds the SELECT
Connectioncarries the query
Databaseruns it, returns rows
App serverrows → web page

One detail worth savoring: the database cannot tell the difference. Whether the query arrives from your fingers or from the Marquee's application, it goes through the same checking, the same planner, the same indexes. There is no separate "application SQL". This is why everything you have learned transfers directly to real systems — there is no secret second door to the data.

Why Connections Are Scarce

One last piece of realism. Opening a connection is not free: the database sets aside memory and bookkeeping for every line it keeps open, so a database can comfortably hold tens or hundreds of connections — not tens of thousands. Meanwhile a popular website serves thousands of visitors at once. One connection per visitor would flatten the database before lunch.

The standard answer is a connection pool: the application opens a modest set of connections — say ten — when it starts, keeps them open, and lends one out for each query. A query borrows a connection for a few milliseconds, the rows come back, the connection returns to the pool for the next borrower. Thousands of customers, ten lines, nobody waiting long. How big to make the pool is a tuning question for another book; the concept is all you need, and now you have it.

Common Confusions
  • "Users connect to the database." Users connect to the application; the application connects to the database. Two different doors with different keys — the customer's login opens the website, and the app's credentials open the data. Topic 40 builds directly on this split.
  • "The app must have some other way to get data." It sends SQL — the very same language, over a connection. There is no secret second door, which is exactly why the SQL literacy you built transfers to every real system you'll ever meet.
  • "More users means more connections, one each." Connections cost the database memory, so apps share a small pool across all their users. A thousand visitors might ride on ten open lines, each borrowed for milliseconds at a time.
  • "The browser could just query the database directly." Then every visitor would hold the database's keys, and any of them could ask for anyone's data. The app in the middle is what decides who may ask what — the next two topics show what happens when that guard is careless.
Why It Matters
  • This page is where the course cashes out: the SQL you learned is what real systems speak on the inside. The query at the center of the trace was yours.
  • The three-layer picture is the working map for every "is it the app or the database?" conversation you will ever have at work — and those conversations happen weekly.

Knowledge Check

In the three-layer picture, who holds a connection to the database?

  • Every customer's browser opens its own connection
  • Only the application server in the middle
  • The database connects outward to each visitor
  • Only Lora, because she owns the Marquee

What does a driver do?

  • It writes the SQL queries so the application doesn't have to
  • It carefully checks whether the application is allowed to run each and every query
  • It's the library that lets a programming language speak to a database engine
  • It converts database rows into finished web pages

Why do applications share a small pool of connections instead of opening one per visitor?

  • Each open connection costs the database memory, and it can only hold so many
  • Because pooled connections can somehow run much faster SQL than all normal ones
  • Because a database only accepts one connection at a time
  • Because database licenses charge per connection opened

A query arrives at the database from the Marquee's application rather than from a person typing. What does the database do differently?

  • It skips the planner, because application queries are pre-approved
  • It relaxes the constraints, since the app is trusted
  • It sends the rows straight to the customer's browser
  • Nothing — a query is a query, wherever it comes from

You got correct