Leanpub Header

Skip to main content

Rust from Sockets to Servers

Building a High-Performance Web Server in Rust from First Principles

This book is 100% completeLast updated on 2026-07-14

Most developers can build a web server with a framework. Far fewer understand what happens underneath. Rust from Sockets to Servers takes you from raw TCP sockets to a production-ready web server built in Rust, giving you a deep understanding of HTTP, async programming, TLS, routing, and the foundations behind every modern web application.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
189
Pages
About

About

About the Book

You have used web frameworks. You know how to spin up an Axum or Actix server in minutes. But do you understand what happens when a browser sends a request and your server sends back a response? This book takes you on a hands-on journey from raw TCP sockets through the full stack of HTTP/1.1, HTTP/2, TLS, routing, middleware, templating, REST APIs, testing, benchmarking, and deployment. Every chapter adds a layer to a production-quality web server built entirely in Rust using Tokio and carefully chosen crates. By the end, you will not only have a working server but also the deep intuition that makes you a better developer no matter what tools you eventually use.

Bundle

Bundles that include this book

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 400 engineers and researchers from Ukraine, Belarus and Russia. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Building a High-Performance Web Server in Rust from First Principles

Introduction: Why Build Your Own Server?

  1. What You Will Build
  2. Prerequisites and Setup
  3. The Architecture at a Glance
  4. How to Read This Book

Chapter 1: Networking Fundamentals

  1. The Internet Protocol Stack
  2. IP Addresses and Ports
  3. TCP Connections and the Three-Way Handshake
  4. Raw Sockets in Rust
  5. Your First Listening Socket
  6. Exercise: Echo Server

Chapter 2: Understanding HTTP/1.1

  1. Anatomy of an HTTP Request
  2. Anatomy of an HTTP Response
  3. HTTP Methods and Status Codes
  4. Parsing the First Request
  5. Sending Your First Response
  6. Exercise: Minimal HTTP Server

Chapter 3: Asynchronous Programming with Tokio

  1. The Problem with Blocking I/O
  2. Futures and the Event Loop
  3. Tokio Runtime Architecture
  4. Async TCP Listeners
  5. Spawning Concurrent Tasks
  6. Exercise: Async Echo Server

Chapter 4: Building the Request and Response Types

  1. The HttpRequest Struct
  2. Parsing Headers into a HashMap
  3. The HttpResponse Struct
  4. Status Code Enum
  5. Converting to Wire Format
  6. Exercise: Round-Trip Request/Response

Chapter 5: Connection Handling and Keep-Alive

  1. The Connection Lifecycle
  2. HTTP Keep-Alive Semantics
  3. Reading Multiple Requests Per Connection
  4. Timeout and Idle Connection Management
  5. Backpressure and Flow Control
  6. Exercise: Persistent Connection Server

Chapter 6: Routing and Endpoint Registration

  1. The Router Data Structure
  2. Exact Path Matching
  3. Parameterized Routes
  4. Exercise: Multi-Route Server

Chapter 7: Middleware and the Request Pipeline

  1. The Middleware Pattern
  2. Building a Middleware Chain
  3. Common Middleware: Logging and Timing
  4. CORS Middleware
  5. Error Handling Middleware
  6. Exercise: Custom Rate Limiter

Chapter 8: Static File Serving

  1. Detecting Static Routes
  2. Reading Files Asynchronously
  3. MIME Type Detection
  4. Content-Encoding and Compression
  5. Cache-Control and ETag Headers
  6. Exercise: Production Static Server

Chapter 9: Templating and Dynamic HTML

  1. Why Server-Side Rendering Still Matters
  2. The Tera Template Engine
  3. Passing Context Data
  4. Layouts and Partials
  5. Security: Escaping and XSS Prevention
  6. Exercise: Blog Frontend

Chapter 10: Building REST APIs

  1. REST Design Principles
  2. JSON Serialization with serde
  3. The In-Memory Store Pattern
  4. CRUD Endpoint Implementation
  5. Error Responses and Validation
  6. Exercise: Task Manager API

Chapter 11: Concurrency Patterns and Performance

  1. Tokio’s Work-Stealing Scheduler
  2. Channels for Inter-Task Communication
  3. Shared State with Arc and Mutex
  4. Connection Limits and Graceful Shutdown
  5. Load Testing with wrk
  6. Exercise: Stress-Tested Server

Chapter 12: HTTP/2 Fundamentals

  1. Why HTTP/2
  2. Binary Framing and Multiplexing
  3. HPACK Header Compression
  4. The h2 Crate
  5. Upgrading from HTTP/1.1 to HTTP/2
  6. Exercise: Dual-Protocol Server

Chapter 13: TLS and HTTPS

  1. Why Encryption Matters
  2. The TLS Handshake
  3. Generating Self-Signed Certificates
  4. Rustls vs native-tls
  5. Integrating TLS with Tokio
  6. Exercise: HTTPS Server

Chapter 14: Security Hardening

  1. Common HTTP Attacks
  2. Security Headers
  3. Input Validation and Request Size Limits
  4. Rate Limiting Implementation
  5. Content Security Policy
  6. Exercise: Security Audit Checklist

Chapter 15: Testing, Benchmarking, and Logging

  1. Unit Testing Handlers and Routes
  2. Integration Tests with Test Clients
  3. Property-Based Testing for Parsers
  4. Structured Logging with tracing
  5. Benchmarking with criterion
  6. Exercise: Test Suite

Chapter 16: Performance Optimization and Deployment

  1. Profiling with flamegraphs
  2. Memory Allocation Strategies
  3. Connection Pooling
  4. Docker Containerization
  5. Reverse Proxy with Caddy or Nginx
  6. Exercise: Production Deployment

Conclusion: What Comes Next

  1. Recap of the Architecture
  2. Extending Your Server
  3. When Frameworks Make Sense
  4. Continuing Your Rust Journey

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub