Skip to main content

Next.js and Rust: A Modern Technology Stack for Web Application Development

How a Next.js frontend, a Rust service and PostgreSQL fit together, with a small Axum example and a frank look at when Rust is worth the extra effort.

5 min read
Next.js and Rust: A Modern Technology Stack for Web Application Development

Next.js and Rust work well together when each does the job it is best at: Next.js renders fast pages and acts as the friendly front door, while a small Rust service handles heavy, performance-sensitive or safety-critical work, with PostgreSQL storing the data. Tarka Raj Jaishi, a full-stack developer in Lamki, Kailali, Nepal, uses both, and the honest answer is that most business applications need Rust only in specific places, not everywhere. This article explains the architecture and how to tell when Rust earns its place.

The architecture at a glance

#

A modern application built with this stack usually has three or four parts, each with a clear responsibility:

PartTechnologyResponsibility
Frontend and BFFNext.js (React, TypeScript)Renders pages, handles sessions and forms, calls internal services on the user's behalf
Core servicesRust with Axum or Actix WebCPU-heavy calculations, high-volume data ingestion, strict business rules
DatabasePostgreSQLDurable storage, transactions, reporting queries
DeploymentDocker, KubernetesRuns each part in its own container, restarts failures, rolls out updates

"BFF" stands for backend for frontend. The browser talks only to Next.js. Next.js then talks to the Rust service and the database over a private network. Customers never reach the Rust service directly, which keeps the attack surface small and lets the internal API change without breaking anyone's phone.

Why Next.js makes a good front door

#
  • Server rendering: pages arrive already filled with content, which helps on slower mobile connections and helps search engines read them.
  • One language for UI and glue code: TypeScript catches many mistakes before they reach users.
  • Server actions and route handlers: simple form submissions, logins and admin screens can be handled inside Next.js itself, with no separate service at all.
  • Unicode-friendly: Nepali (Devanagari) text in pages and forms works without special effort.

Tarka's own site, tarkarajjaishi.com.np, runs on Next.js and PostgreSQL, self-hosted on Kubernetes, with its blog, admin panel, accounts and comments all handled at the Next.js layer. That is a useful reminder: a typical content and accounts site does not need Rust.

What Rust adds, in plain terms

#

Rust is a systems language that compiles to fast native code without a garbage collector, and its compiler refuses many classes of bugs (such as using data after it has been freed, or two threads changing the same data unsafely). For a business owner, that translates into three practical benefits:

  1. Predictable speed under load. A Rust service can process large batches of data, like thousands of price updates, using modest server resources.
  2. Lower hosting needs for heavy work. Efficient code can mean a smaller server for the same job, which matters when you self-host.
  3. Fewer runtime surprises. Strict types and explicit error handling force edge cases to be dealt with during development rather than discovered by customers.

A small illustrative example

#

Here is a simplified Axum handler that returns a stock's moving average. It is not production code; it is here to show how readable a small Rust service can be.

use axum::{extract::{Path, State}, Json};
use serde::Serialize;
use sqlx::PgPool;

#[derive(Serialize)]
struct Average { symbol: String, days: i32, value: f64 }

async fn moving_average(
    State(pool): State<PgPool>,
    Path((symbol, days)): Path<(String, i32)>,
) -> Result<Json<Average>, AppError> {
    let value: f64 = sqlx::query_scalar(
        "SELECT AVG(close) FROM (
           SELECT close FROM prices WHERE symbol = $1
           ORDER BY day DESC LIMIT $2) recent",
    )
    .bind(&symbol)
    .bind(days as i64)
    .fetch_one(&pool)
    .await?;

    Ok(Json(Average { symbol, days, value }))
}

Notice the parameters $1 and $2: values are bound separately from the SQL text, which blocks SQL injection. The ? operator means any database error is returned as a proper error response instead of crashing. Next.js would call this endpoint privately, then render the result as a chart.

When Rust is worth it, and when it isn't

#

This is the most important decision, and it should be made per feature, not for the whole project.

Good reasons to reach for Rust

#
  • Processing a continuous stream of market data, sensor readings or logs. A platform like nepsetrading.com, which works with live NEPSE data, screeners and technical analysis, is the kind of workload where computation-heavy parts benefit from a fast language.
  • Generating large reports or recalculating thousands of records on a schedule.
  • A component that handles money or permissions where you want the compiler to enforce strict rules.
  • Image, file or PDF processing done in bulk.

Reasons to stay with Next.js and Node.js alone

#
  • Your app is mostly forms, lists and dashboards, such as a booking system for a clinic in Dhangadhi or a stock register for a shop in Lamki.
  • The team maintaining it later only knows JavaScript or TypeScript.
  • You need to launch quickly and validate the idea first.
  • Traffic is modest and the database, not the code, is the real limit.

Adding a second language adds a second build, a second deployment and a second set of skills to maintain. If the benefit is not clear, it is cost without return.

How the pieces get deployed

#

Each part is packaged as a Docker image. Rust compiles to a single small binary, so its container is lightweight. On Kubernetes, the Next.js app and the Rust service run as separate deployments, with PostgreSQL either in the cluster with persistent storage and backups or on a managed database. Health checks restart a service that stops responding, and updates roll out one container at a time so users do not see downtime. For the broader practices around this, see building fast, secure and scalable web applications.

A sensible adoption path

#
  1. Build the whole first version in Next.js with PostgreSQL.
  2. Measure. Find the specific job that is slow or expensive.
  3. Move only that job into a Rust service with a narrow, documented API.
  4. Keep Next.js as the single entry point so the user experience does not change.

This keeps your options open without paying for complexity on day one. If you are still deciding between stacks generally, this comparison of web application technologies may help, and this piece on scalable web application development covers growth planning.

Frequently asked questions

#

Can Next.js and Rust share the same PostgreSQL database?

#

Yes, and it is common. The safest pattern is to give each service clear ownership of certain tables, or to have one service handle writes for a given area. Database migrations should live in one place so both sides agree on the structure.

Is Rust harder to maintain than JavaScript?

#

Rust has a steeper learning curve, so fewer developers know it. That is why it should be limited to the parts that genuinely benefit. A small, well-documented Rust service with a clear API is manageable; an entire app in Rust for no reason is not.

Will Rust make my website load faster for customers?

#

Not by itself. Page speed on a phone mostly depends on frontend size, images, caching and network distance. Rust speeds up heavy server-side work, which helps when that work was the bottleneck.

Should I use Axum or Actix Web?

#

Both are mature Rust web frameworks. Axum integrates closely with the Tokio async ecosystem and is often simpler to read; Actix Web is also fast and well established. The choice matters far less than good database design and testing.

Discuss your project with Tarka Raj Jaishi

#

If you are unsure whether part of your application would benefit from Rust, Tarka can review the workload with you and recommend the simplest stack that will do the job.

Share this article

Newsletter

New posts in your inbox. Confirm by email, unsubscribe any time.

Comments

Posting as a guest

View all