Microservices in Rust: How to Get Started and Keep Your Sanity

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Rust is Your Next Step in Microservices?

\n\n

The world of microservices has long been divided between Java (with its Spring Boot), Python (FastAPI, Django), and Go. But in the last couple of years, the voice of Rust has been heard more and more often. Why? Because Rust delivers what once seemed unattainable: C++ performance and memory safety — without a garbage collector.

\n\n

If you write in Python, you're used to development speed. But when the load grows, Python starts to "slow down". Rust solves this problem radically: one microservice in Rust can replace 3–4 services in Python in terms of performance. And the code doesn't "crash" at runtime with a NullPointerException.

\n\n

In this article, we won't philosophize. We'll take and write a microservice in Rust from scratch. You'll see that it's not scary, but actually quite exciting.

\n\n

Preparing the Environment: What to Install

\n\n

Before writing code, let's make sure you have everything you need. If you're new to Rust — don't worry, installation takes 2 minutes.

\n\n

1. Installing Rust

\n

The easiest way is through the official installer rustup:

\n\n
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
\n\n

After installation, check the version:

\n\n
rustc --version\ncargo --version
\n\n

2. Choosing an Editor

\n

I recommend VS Code with the rust-analyzer extension. This will give you autocompletion, error highlighting, and refactoring "out of the box".

\n\n

3. Docker (for Containerization)

\n

Microservices usually live in containers. Install Docker Desktop or Docker Engine. We'll build an image at the end of the article.

\n\n

Choosing a Framework: Actix-web vs Axum vs Rocket

\n\n

In the Rust world, there are three main frameworks for HTTP servers. Here's a brief comparison:

\n\n
    \n
  • Actix-web — the fastest (up to 1 million requests/sec). Mature, lots of examples. Ideal for production.
  • \n
  • Axum — modern, from the creators of Tokio. Integrates best with the async ecosystem.
  • \n
  • Rocket — the most "friendly" syntax, but updates slower.
  • \n
\n\n

In this article, we'll use Actix-web, as it's battle-tested over the years and has the largest community. But if you want a "trendy" stack — feel free to go with Axum.

\n\n

First Microservice: URL Shortener Service

\n\n

To avoid being abstract, let's write a real microservice — a url-shortener. It will accept a long URL and return a short one.

\n\n

Step 1. Create the Project

\n
cargo new url-shortener\ncd url-shortener
\n\n

Step 2. Add Dependencies

\n

Open Cargo.toml and add:

\n\n
[dependencies]\nactix-web = "4"\nserde = { version = "1", features = ["derive"] }\nserde_json = "1"\nuuid = { version = "1", features = ["v4"] }\ntokio = { version = "1", features = ["full"] }
\n\n

Step 3. Write the Code

\n

Replace the contents of src/main.rs with the following code:

\n\n
use actix_web::{web, App, HttpServer, HttpResponse, Responder};\nuse serde::{Deserialize, Serialize};\nuse std::sync::Mutex;\nuse uuid::Uuid;\n\n// Structure for storing links in memory\nstruct AppState {\n    links: Mutex>,\n}\n\n#[derive(Serialize, Deserialize, Clone)]\nstruct Link {\n    id: String,\n    url: String,\n}\n\n#[derive(Deserialize)]\nstruct CreateLinkRequest {\n    url: String,\n}\n\n// POST /shorten — creates a short link\nasync fn shorten(data: web::Data<AppState>, body: web::Json<CreateLinkRequest>) -> impl Responder {\n    let new_link = Link {\n        id: Uuid::new_v4().to_string()[..8].to_string(), // take the first 8 characters of the UUID\n        url: body.url.clone(),\n    };\n\n    let mut links = data.lin                

Blogs

Book Recommendations