DEV Community

Cover image for Your Backend Should Be a Compiler, Not a Collection of Handlers
Drew Marshall
Drew Marshall

Posted on

Your Backend Should Be a Compiler, Not a Collection of Handlers

Most backends are built the same way.

You define routes.
You write handlers.
You wire everything together.

Over time, you end up with dozens—sometimes hundreds—of functions that all follow similar patterns but live in different places.

It works.

But it doesn’t scale cleanly.


The Handler Mindset

A typical backend looks like this:

  • Route definition
  • Controller or handler
  • Validation logic
  • Database query
  • Response formatting

Repeat that structure across your entire application.

Even if you organize it well, you still end up with:

  • Repeated patterns
  • Slight inconsistencies
  • Logic spread across multiple files

You’re not building a system.

You’re assembling one manually.


The Realization

Most handlers don’t do anything unique.

They follow a pattern:

  1. Accept input
  2. Validate it
  3. Execute some operation
  4. Return a result

The structure is always the same.

Only the data changes.


So Why Are We Writing Them Over and Over?

Instead of writing handlers, what if we defined behavior?

Not in code first…

…but as a contract.


Thinking Like a Compiler

A compiler takes a definition and turns it into execution.

It doesn’t care about your specific use case.

It cares about:

  • Structure
  • Rules
  • Transformation

Now imagine your backend doing the same thing.

Instead of this:

app.post('/users', async (req, res) => {
  const { name, email } = req.body;

  if (!name || !email) {
    return res.status(400).json({ error: 'Invalid input' });
  }

  const user = await db.users.insert({ name, email });

  return res.status(201).json(user);
});
Enter fullscreen mode Exit fullscreen mode

You define this:

create:
  user:
    CreateUser:
      input:
        name: string
        email: string
      insert:
        name: $input.name
        email: $input.email
      response:
        id: number
        name: string
        email: string
Enter fullscreen mode Exit fullscreen mode

No handler.

Just a definition.


What the Backend Becomes

Instead of a collection of handlers…

Your backend becomes a compiler.

It:

  • Reads definitions
  • Validates structure
  • Builds execution logic
  • Runs it through a pipeline
  • Returns a response

The code you write once replaces hundreds of repeated patterns.


The Execution Flow

Every request follows the same path:

Request → Load Definition → Validate Input → Build Operation → Execute → Format Response
Enter fullscreen mode Exit fullscreen mode

Nothing is hidden.

Nothing is scattered.

Everything is predictable.


Why This Is Better

Consistency

Every endpoint behaves the same way.

No surprises. No deviations.


Maintainability

Change the system once, and every endpoint benefits.

You’re not hunting through files to fix the same bug.


Clarity

You can read the system without digging through code.

Definitions describe behavior directly.


Scalability

As your application grows, complexity doesn’t explode.

It stays structured.


“But What About Custom Logic?”

Not everything fits into a strict pattern.

That’s fine.

A compiler-based backend can still support:

  • Custom steps in the pipeline
  • Extension points where needed
  • Escape hatches for complex cases

The difference is:

Custom logic becomes the exception, not the default.


Code Moves Up a Level

You’re still writing code.

Just not the same code over and over again.

Instead of writing handlers, you write:

  • The compiler
  • The pipeline
  • The rules of the system

You move from:

“Implement this endpoint”

to:

“Define how endpoints work”


This Is About Leverage

Handler-based systems scale linearly.

More features = more code.

Compiler-based systems scale differently.

More features = more definitions.

The engine does the heavy lifting.


The Bigger Picture

This idea doesn’t stop at APIs.

It connects to everything:

  • UI driven by structure
  • APIs driven by contracts
  • Infrastructure driven by configuration

Each layer becomes:

  • Predictable
  • Composable
  • Aligned

Final Thought

Handlers feel powerful because they give you control.

But they also force you to repeat yourself.

A compiler feels restrictive at first.

But it gives you something better:

Leverage.

That’s why I stopped building backends as collections of handlers…

…and started building them like compilers.

Top comments (0)