Data Science: Serving and Processing Data with Actix.

applied.math.coding
5 min readAug 12, 2023

This story is part of my Data Science series.

Tasks in Machine Learning not only involves building of models or data analysis but in addition to deploy the results on production systems. Often such systems are built around a web server — and at exactly this I want to focus in this story.

At the time of writing one of the best web frameworks in the Rust community is Actix. It offers full support of asynchronous request handling which is very important for scalability and performance of I/O tasks.

In order to not repeat ourselves, I simply re-use the project that we setup together in the previous story about accessing data.

First of all, we add Actix to our dependencies in Cargo.toml:

[dependencies]
...
actix-web = { version = "4" }
serde = { version = "1.0", features = ["derive"] }

Actix is a very easy to use web framework with high performance. Actually it resembles features that you find in frameworks like Express.js, Nestjs or even Spring MVC.

Let us aim to create a simple GET-endpoint that lets us retrieve the data of a ticker by name. The related method at the service layer will look like this:

use sqlx::{PgExecutor, Row};

pub async fn find_all_ticker_data(
executor: impl PgExecutor<'_>,
ticker: &str,
) -> sqlx::Result<Vec<f32>> {
let rows = sqlx::query(
r#"
SELECT
sp.close
FROM stock_prices sp
WHERE sp.ticker = $1
ORDER BY sp."date" ASC
"#,
)
.bind(ticker)
.fetch_all(executor)
.await?;
Ok(rows.iter().map(|row| row.get::<f32, usize>(0)).collect())
}

This is nothing new if you have read my previous article (see here). Remember, the executor typically is of the concrete type Pool<Postgres>, i.e. a pool of DB connections managed by sqlx.

In order to declare the endpoint we add this function:

use actix_web::{error, get, web…

--

--

applied.math.coding

I am a Software Developer - Rust, Java, Python, TypeScript, SQL - with strong interest doing research in pure and applied Mathematics.