Rust for Big Data: Processing Millions of Rows Faster Than Python

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 the New King of Big Data Processing?

\n\n

Have you ever waited 20 minutes for a Python script to process a 10-gigabyte CSV file? And then another 10 minutes because memory ran out and the system went into swap? Sound familiar?

\n\n

The world of big data has long belonged to Python and its ecosystem: Pandas, NumPy, Dask. But there is one problem — Python is slow. Very slow. And when data is measured in terabytes, every extra millisecond turns into hours of downtime.

\n\n

This is where Rust enters the stage. A systems language that delivers C++ performance and memory safety, but with syntax that (spoiler) is quite lovable. And yes, it is perfect for big data processing.

\n\n

In this article, we will break down how Rust allows you to process huge volumes of data faster, safer, and with less memory consumption. No magic — just code and benchmarks.

\n\n

Rust vs Python for Big Data: What's Inside?

\n\n

Let's get straight to the numbers. Here is a simple test: read a 500 MB CSV file, filter rows by a condition, and calculate the sum.

\n\n\n\n\n\n\n
ParameterPython (Pandas)Rust (Polars)
Execution time12.4 seconds0.8 seconds
Peak RAM usage2.1 GB380 MB
Number of rows5 million5 million
\n\n

15 times faster and 5 times more memory efficient. And this is not the limit. Rust gives you control over every byte, every allocation. It does not forgive mistakes, but it delivers speed that Python will never catch up to.

\n\n

Rust Toolset for Big Data

\n\n

Before writing code, let's look at what's available. Rust is not standing still — the data processing ecosystem is actively growing.

\n\n

Key Libraries

\n
    \n
  • Polars — an analog of Pandas, but in Rust. Supports lazy evaluation, parallel processing, and works with data that doesn't fit in memory.
  • \n
  • Apache Arrow — a columnar data format that allows data exchange between different languages without copying.
  • \n
  • DataFusion — a SQL engine in Rust that can execute queries directly over Arrow data.
  • \n
  • rayon — a library for parallel iterations. Makes it easy to parallelize any processing.
  • \n
  • csv — a fast CSV parser with minimal allocations.
  • \n
  • serde — serialization/deserialization. Essential for working with JSON, YAML, TOML.
  • \n
\n\n

We will use Polars and rayon, as they are the de facto standard for big data processing in Rust.

\n\n

Practical Example: Processing Web Server Logs

\n\n

Imagine you have 100 million Nginx log entries. You need to:

\n
    \n
  1. Filter only 5xx errors
  2. \n
  3. Group by IP address
  4. \n
  5. Count the number of errors per IP
  6. \n
  7. Output the top 10 offenders
  8. \n
\n\n

In Python with Pandas, this would take about 2-3 minutes and consume all your RAM. In Rust — 15-20 seconds.

\n\n

Data Structure

\n

Let's assume each log looks like this:

\n
192.168.1.1 - - [10/Oct/2023:13:55:36 +0000] "GET /api/data HTTP/1.1" 500 1234 "-" "Mozilla/5.0"
\n\n

We need to parse the string, extract the IP and status code. In Rust, this is done using a custom struct type:

\n\n
use serde::Deserialize;\n\n#[derive(Debug, Deserialize)]\nstruct LogEntry {\n    ip: String,\n    status_code: u16,\n    method: String,\n    path: String,\n    user_agent: String,\n}
\n\n

Parsing CSV with Polars

\n

Polars is what makes it worth trying

Blogs

Book Recommendations