Backend for Dummies: How to Structure a Program

MVC Architecture meme

turn up

Hey, this blog post is for beginners looking to get their start making their first web application. I’m going to show you one simple way to reason about and structure your program. This approach is inspired by the MVC Architecture. There are many ways to do this, but I find this one both easy and effective.

Structuring your app correctly from the start saves you hours of confusion later. It’s like organizing your workspace before you start building.

When building your first web application, it’s useful to think of your project in three layers:

Database → Service Logic → Controller

The Three Layers

You can think of these layers like a relay team. Each handles its own job, then passes the baton to the next.

Database Layer

This layer stores and retrieves data about your users or anything else your app keeps track of. Simple enough, right?

Service Layer

This is where your app’s main logic lives. It’s what makes your project unique—the rules, calculations, or features that define what it does.

Controller Layer

This is where your code talks to the outside world. Controllers receive and send HTTP requests, acting as the bridge between your users and your backend.

Now that you understand the layers, let’s look at how they fit together in a real example.

Here’s a a Diagram of what we are building!

Diagram showing clients, Spring middleware, and a PostgreSQL database

A Practical Example:

Lets say your making a simple app where you can store users (id’s names, email etc) . We’ll use Java Spring Boot to walk through how these layers connect.

1. The Domain or Entity Layer

This class defines what a User looks like and how it maps to a table in your database.

// User.java
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name; // fields represent a column in your DB
    private String email;

    // Constructors
    public User() {}
    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    // Getters and Setters
    public Long getId() { return id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

2. The Repository Layer

Repositories give you database superpowers without having to write SQL yourself.

// UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

3. The Service Layer

Here we add the main logic that controls how users are handled.

// UserService.java
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public User createUser(User user) {
        return userRepository.save(user);
    }
}

4. The Controller Layer

Here we are defining two endpoints:

  1. GET -> returns All Users
  2. POST -> creates a User
// UserController.java
@RestController
@RequestMapping("/users") // indicates to 'how' to reach these endpoints!
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping // indicates the type of HTTP Request
    public List<User> getUsers() {
        return userService.getAllUsers();
    }

    @PostMapping // indicates the type of HTTP Request
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
}

Data Flow

Now that you’ve seen the structure, here’s how the data moves through your backend:

Diagram tracing a create-user request from client through controller, service and entity to the database

  1. A user clicks a Create User button (the ‘client’ is their phone)
  2. The client sends hits the UserController, triggering a POST request to create a new user. Controller receives (JSON -> Java Object)
  3. UserController calls UserService to do business logic (ie. doStuff(), validate(), whatEverYouWant())
  4. UserService calls the UserRepository to save this new user.
  5. UserRepository extends JPA and writes a SQL query to write to the Database

If you understand that flow, congratulations! You can build a working REST API in Spring.

What’s Next

We simplified a quite a bit here. Things like Lombok, JDBC, and Connection Pooling all come into play when you start building real-world projects.

Once you’re comfortable with this structure, try adding authentication, validation, and error handling. That’s when your backend starts to feel real.

Keep learning!

Originally published on Medium.