DEV Community

Cover image for Build a REST API with Rust and MongoDB - Actix web Version

Build a REST API with Rust and MongoDB - Actix web Version

Demola Malomo on May 26, 2022

REST API has become the De Facto for connecting and transferring data from one source to another. It offers a set of guidelines and architectural p...
Collapse
 
kmahar profile image
kaitlin

Hi @malomz, I'm one of the maintainers of the mongodb crate. Thanks for writing up this tutorial!

I wanted to point out that since you are writing an asynchronous application (as Actix is an async framework), it would be most appropriate to use the mongodb async API here rather than the sync API. With the sync API, every time you interact with the database you will be running blocking code on the current thread and preventing the async runtime (Tokio) from scheduling any other async tasks on the thread while waiting for a response from the database. The end result is that your application is more resource-intensive and less performant, since threads cannot be shared as efficiently.
With the async API, the thread can be "given up" to another task while you await the response from the database.
This is a nice blog post on this subject.

The async API is identical to the sync one besides everything being async; to adopt your example here I think you'd just need to mark the methods on your MongoRepo type async and call the equivalent async mongodb API methods.

You can find a simple example of using the async mongodb API with actix here.

Feel free to get in touch with us via GitHub if you have any questions or run into any issues.

Collapse
 
malomz profile image
Demola Malomo

Hi @kmahar ,

Thanks for pointing this out ๐Ÿ™๐Ÿ™.

I have updated accordingly๐Ÿ‘

Collapse
 
kmahar profile image
kaitlin

Awesome! One more thing to note is that since Actix uses the tokio runtime, I'd suggest using the driver with tokio as well, rather than async-std, so that you do not need to have two separate runtimes going that cannot coordinate with one another.

By default the mongodb crate uses tokio, so to do this, you can just remove these lines from your Cargo.toml:

default-features = false
features = ["async-std-runtime"] 
Enter fullscreen mode Exit fullscreen mode

There's some documentation on all of the drivers' feature flags available here.

Collapse
 
codewander profile image
codewander

I expect to see more people start to use rust + postgres + an ORM, when they want something with a richer type system or more functional idioms than go, python, or node/ts. Do you think that emerge and possibly reach fourth place over ruby for web dev?

Collapse
 
malomz profile image
Demola Malomo

Well, rust is relatively new to the ecosystem as compared to others. Hopefully, it becomes mainstream! :)

Collapse
 
rustaceanx profile image
rustaceanX

hi there, just wanna collection ppl opinion but could I ask why you choose Postgres over Mongodb when stack with Rust or actix-web for more specific? TIA

Collapse
 
codewander profile image
codewander

As I understand, postgres will be easier to implement a wide variety of queries, while mongo will help keep inserts fast, so having more flexibility with queries may be a good default.

Collapse
 
triyanox profile image
Mohamed Achaq

Thanks for putting the effort to write this amazing post definitely will try Actix Web soon ๐Ÿ˜

Collapse
 
malomz profile image
Demola Malomo

Glad you enjoyed it.
Yea, you should give it a try!

Collapse
 
mayomi1 profile image
Mayomi Ayandiran

Thank you for this tutorial

Collapse
 
aaravrrrrrr profile image
Aarav Reddy

Thanks!

Collapse
 
snelson1 profile image
Sophia Nelson

Good article, thank you.

Collapse
 
lucasznk profile image
Lucas Zanek

AWESOME!! Thanks for this ammount of knowledge shared. I really appreciate it!

Collapse
 
malomz profile image
Demola Malomo

Glad you enjoyed it!

Collapse
 
rustaceanx profile image
rustaceanX

nice post thanks @malomz for the nice guide and thanks @kmahar for feedback.

Collapse
 
zmahmud255242 profile image
Z Mahmud

Thanks for your article. I have a question. Suppose after creating the client if you want to store it in global variables for further use how I can do that?

Collapse
 
lucagiorgino profile image
Luca Giorgino

How would you model the MongoRepo struct if you have multiple collections?

Collapse
 
malomz profile image
Demola Malomo

Check out this article

dev.to/hackmamba/create-a-graphql-...

I did something similar here