The Back End

The back end is the code that runs on a server, keeping the rules of the business and answering the front end through an API, and in this course you'll write it in C# with ASP.NET Core.

Novice · Web Development · ASP.NET Core · Updated

After this lesson you can

  • Describe what the back end of an app does and where it runs
  • Explain why rules such as prices are checked on the back end, not the front end

Key takeaways

  • The back end is code that runs on a server, a computer the user never sees, and it keeps the rules of the business.
  • The back end decides anything that must be trusted, such as a price or who may see an order, because the front end can be changed by the user.
  • An API is how other programs talk to the back end. The front end asks the API a question and the API answers, often in JSON.
  • A back-end developer builds the rules, the API and the connection to the database, and keeps them correct, safe and fast.

When you press Pay in an app, you're trusting something you'll never see: code on a computer far away that checks your order, charges your card exactly once and tells the kitchen to start. That hidden code is the back end. If the front end is the part people judge, the back end is the part they rely on.

What the back end is

The back end is the code that runs on a server: a computer, usually in a data center, that waits for requests and answers them. The user's phone or laptop is the client: it asks, and the server answers. This pairing is called client and server, and almost every app you use works this way.

Back at Build-It Pizzeria, the order screen on your phone is the client. When you press Order, it sends your choices to the pizzeria's server, and the back end takes over:

  1. It checks the order makes sense: the size, the crust and every topping are on the menu.
  2. It works out the price, from the prices in the pizzeria's database, not from the number the phone showed.
  3. It saves the order in the database, which gives it its order number.
  4. It charges your card through a payment company and sends the order to the kitchen's screen.
  5. It answers the phone: Your pizza will be ready at 7:40.

A customer's phone sending a pizza order to the Build-It Pizzeria server, which checks it, works out the price, charges the card, saves the order and sends it to the kitchen screen, then answers the phone with the ready time

Why the rules live on the server

Why not let the phone work out the price? Because the front end runs on the user's device, and the user can change anything on their own device. With a few minutes of searching, anyone can learn to edit a web page in their browser and change $15.00 to $1.00. If the pizzeria believed the phone, it would sell a lot of one-dollar pizzas.

So the rule is: the front end can show a price, but only the back end decides it. The same goes for anything that must be trusted: who is signed in, whose order this is, whether a coupon is still valid. Here's one of the pizzeria's price rules written in C#, the language you'll write the back end in: a stuffed crust costs $2.00 more. You'll learn to read every word of it in the next chapters:

string crust = "stuffed";
decimal price = 13.00m;

if (crust == "stuffed")
{
    price = price + 2.00m;
}

Console.WriteLine(quot;A large stuffed crust costs ${price}");
A large stuffed crust costs $15.00

The API: how the front end talks to the back end

The front end and the back end are separate programs, often on opposite sides of the world, so they need an agreed way to talk. That's an API, short for application programming interface: a list of questions a program can ask, and the shape of each answer. A web API is one you ask over the web.

Think of the pizzeria's drive-through window. You can't walk into the kitchen; you ask at the window, in the way the menu board says, and the answer comes back through the window. An API is that window for programs. The pizzeria's front end might ask "what does a large thin crust with mushrooms and green peppers cost?", and the API answers in JSON, a plain-text format for data that almost every language can read:

{ "size": "large", "crust": "thin", "toppings": ["mushrooms", "green peppers"], "price": 15.00 }

The same API can answer many front ends at once: the website, the phone app, the kitchen screen, even a delivery company's app. Build the rules once, on the back end, and every screen gets the same answer.

What a back end can do

The back end is where the power is. With the skills in this course, a back end can:

  • Keep the business honest: prices, stock, coupons and opening hours, the same for every customer.
  • Keep people's data safe: sign-in, passwords stored so nobody can read them, and each customer seeing only their own orders.
  • Talk to other services: take payments, send a text when the pizza leaves, show a map for delivery.
  • Answer thousands of people at once: a Friday-night rush of orders, each one answered in a fraction of a second.

What a back-end developer does

A back-end developer builds and looks after the server side: the rules, the API, and the code that reads and writes the database. On a typical day they might add a new API question (say, "what are my last five orders?"), find out why a page is slow, fix a rule that let a wrong price through, or write tests that prove a rule works. In this course you'll write the back end in C# with ASP.NET Core: first web pages built on the server with ASP.NET Core MVC in Module 1, then web APIs in Module 3.

Try it. In an app you use, find one thing that must be decided on a server, never on your phone. A game's high-score table, a bank balance and a concert's last tickets are good places to look. Ask yourself what would go wrong if the phone decided it.

Check yourself

2 questions to try on the lesson page.

So what

The back end is where an app's rules and trust live, and it's where you'll write most of your C# in this course.

Key terms

Cheat sheets