Anshul Goyal I love to code and use new technologies.

11 database drivers and ORMs for Rust that are ready for production

5 min read 1434

11 Database Drivers and ORMs for Rust That Are Ready for Production

For more reasons than we have time to explore in this post, Rust is rapidly gaining popularity in the developer community. One of its selling points is that it supports all major databases and storage formats — from file-based storage platforms, to memory-based and hybrid databases.

In this guide, we’ll compare 11 of the most popular and stable database drivers and ORMs available for Rust. At the end, we’ll compile all the key takeaways in a convenient table to help you choose the right database for your next Rust project.

Database drivers

A database driver is a piece of code that performs communication between the application and the database. It implements protocols and formats for data exchange between application and database.

1. mysql

MySQL is a very popular database and the first choice for most applications that use the SQL database. The mysql crate provides a pure Rust implementation of the MySQL protocol. It supports both text-based and binary protocols.

mysql supports caching of statements as well as pools of connections. TLS is supported via the nativetls crate. The crate uses the params for creating rows. One downside is that MySQL does not have async support.

2. mysql_async

As you might’ve guessed, mysql_async is an async version of the mysql driver. It has a lot of APIs that are similar to those found in mysql, but it may not have some new features since the crates are maintained by different developers.

mysql_async has full support for MySQL protocol, including TLS-based encryption. mysql_async crate also allows you to maintain a connection pool. This crate is written in pure Rust.

3. tokio_postgres

Async Support: Yes

Postgres is a very popular database. Rust has an async implementation for the Postgres protocol that is built on top of tokio async runtime, but it can work with other runtimes as well.

The execution of queries is fully pipelined — i.e., the drivers can send multiple queries. That means tokio-postgres can process multiple data responses simultaneously. It also supports TLS using nativetls. The driver doesn’t have a connection pool support since multiple queries can be sent on the same connection.

We made a custom demo for .
No really. Click here to check it out.

tokio_postgres has multiple wrappers for various configs, such as a sync version of the driver, an OpenSSL version, native_tls version, and many more.

There are also several other crates that feature various additions, including:

  • postgres-types
  • postgres-native-tls
  • postgres-openssl
  • postgres

4. redis

Redis is an in-memory database that can be used for creating caches, worker queues, and microservices. Rust has excellent support for the Redis database. The redis crate provides both high- and low-level APIs. All the queries are pipelined, meaning multiple queries can be sent simultaneously.

redis has a full implementation of the Redis communication protocol. Support for pub/sub is still in the works. redis uses tokio as async runtime. This crate supports Lua scripts, auto-reconnect, and connection pool using the r2d2 crate.

5. rusqlite

Async Support: No

SQLite is an in-process database with SQL support that is used by multiple platforms for maintaining data stores. Android applications often use SQLite as a local database.

Rust has a wrapper around the SQLite C driver. The rusqlite crate uses bindgen to create the wrapper, which can result in long compile time. rusqlite is a safe wrapper around the SQLite wrapper. It also supports hooks such as commit, insert, etc.

rusqlite does not include async support.

6. leveldb

Created by Google, LeveDB is a key-value store and a key-value store only. It’s not an SQL database; it’s is more like NoSQL.

Many databases use LevelDB as a storage engine. Chrome uses LevelDB to implement IndexDB.

The leveldb crate provides a safe binding for LevelDB. Like rusqlite, it may increase compile time since it uses bindgen. Also like rusqlite, there is no async support.

7. mongodb

MongoDB is a NoSQL database. Rust has an official MongoDB driver with async support. It’s written in pure Rust using tokio runtime for async support.

mongodb has support for aggregation as well as various database operations. It uses the bson crate to create a BSON document. As of this writing, transactions are not supported.

8. memcache

Memcached is a free, open-source, high-performance, distributed memory object caching system. memcache is a Memcached client written in pure Rust. It supports multiple instances of Memcached. Some features, including auto JSON serialization and compression, aren’t available yet.

9. cdrs

Async Support: No

Cassandra is a distributed, scalable SQL database. crdrs is a database driver for Cassandra and ScyllaDB.

The crate provides a full implementation of Cassandra protocol and supports clusters. It also supports TLS using nativetls.

cdrs-async is an async version of the cdrs crate based on tokio async runtime.

ORMs

An object relational mapper (ORM) maps stable rows with Rust structs. ORMs provide support for multiple SQL databases. Many are complete database solutions that include multiple database drivers, connection pool support, and migration tooling. Below are two of the most popular.

1. diesel

Async Support: No

diesel is an ORM for Postgres, MySQL, and SQLite. It provides a migration utility called diesel_cli. diesel is type-safe, meaning table types are created using migration files. Its goal is to produce identical behavior on multiple databases.

diesel doesn’t have async support, and the group by query is not well-supported. On stable Rust, the error is a bit weird due to lack of support for procedural macros. It can include long nested types.

Drivers can be changed for a database. The connection pool is supported via r2d2.

2. quaint

quaint is an ORM that supports async operations. It provides AST for creating queries at runtime.

quaint supports multiple SQL databases, including MySQL, Postgres, and SQLite, as well as the connection pool. It’s not a database type-safe ORM.

How to choose the right database driver

The Rust community has built some awesome tools for managing database migrations, a few of which are ORM-independent. The best solution for you will depend on the goals and requirements of your project.

To help you make an informed selection, we’ve compiled the key takeaways, pros, cons, and features associated with each database driver and ORM described above (plus a few more) in a convenient table.

Name Type Prod.-ready? Async support? Pure Rust? Pros Cons Notes Maintenance
mysql Driver Yes No Yes
  • Full implementation of MySQL protocols
  • Connection pool
  • TLS Support using nativetls
  • Missing ORM
  • No async support
  • Used in thousands of projects
Active
mysql_async Driver Yes Yes Yes
  • Full implementation of MySQL protocols
  • Nearly same APIs as mysql
  • Async Support
  • TLS support using nativetls
  • Connection Pool
  • May have some missing feature from mysql crate
  • Thousands of downloads
Active
tokio_postgres Driver Yes Yes Yes
  • Fully pipelined execution
  • Send mutliple queries at the same time
  • Support for multiple runtimes
  • TLS support
  • Sync wrapper available
  • No pool
  • Multiple crates with various additons
  • postgres-types
  • postgres-native-tls
  • postgres-openssl
  • postgres
Active
redis Driver Yes Yes Yes
  • Support for cluster
  • r2d2 pool
  • Automatic reconnect
  • Lua script support
  • Pub/sub is not fullly supported
  • Full implementation of Redis
  • Provides both high- and low-level APIs
  • Supports pipelining
Active
rusqlite Driver Yes No No
  • Wrapper around SQLite library wriiten in C
  • Support for hooks
  • Safe Rust
  • Increased build time due to bindgen
  • SQLite is a in process database
Active
leveldb Driver Yes No No
  • Fast key-value pair storage
  • Safe wrapper
  • Almost complete wrapper
  • Increased build time due to bindgen
  • Fast key-value store
Active
mongodb Driver Yes Yes Yes
  • Official MongoDB driver
  • Support for aggregation
  • No support for MongoDB transactions
  • Rust binding for MongoDB client
Active
memcache Driver Yes No Yes
  • In-memory database
  • Support for multiple DB servers
  • No support for auto JSON serialization and compression
  • memcached is the distributed server
Active
cdrs Driver Yes No Yes
  • Supports ScyllaDB
  • Full implementation of specs
  • TSL/SSL support
  • Multiple serve event support
  • No async support
– Async version available (cdrs-async) Active
diesel ORM Yes No Yes
  • Strongly typed
  • Support for various SQL databases
  • Super fast serialization and deserialization
  • Identical behavior for most databases
  • Support for connection pool
  • No choice for different drivers
  • No support for group by query
  • Long compile time
Active
quaint ORM Yes Yes Yes
  • Syntax for building SQL queries
  • Support for pool
  • Support for MySQL, Postgres and Sqlite databases
  • Not type-safe abstraction
Active
rustorm ORM No No Yes
  • Easy serializing of table row
  • Support for mutliple databases
  • Pool support
  • No builder pattern
  • Raw SQL query is used
Active
tql ORM No No Yes
  • Compile-time SQL query creation
  • Poor error handling on stable Rust
  • Long compile time
Active
migrant Tool Yes N/A Yes
  • Support for MySQL migration
  • Self-update
  • Support for shell (REPL)
  • Verify database cred
Passive
refinery Tool Yes Yes Yes
  • Supports multiple drivers
Active

 

You come here a lot! We hope you enjoy the LogRocket blog. Could you fill out a survey about what you want us to write about?

    Which of these topics are you most interested in?
    ReactVueAngularNew frameworks
    Do you spend a lot of time reproducing errors in your apps?
    YesNo
    Which, if any, do you think would help you reproduce errors more effectively?
    A solution to see exactly what a user did to trigger an errorProactive monitoring which automatically surfaces issuesHaving a support team triage issues more efficiently
    Thanks! Interested to hear how LogRocket can improve your bug fixing processes? Leave your email:

    LogRocket: Full visibility into production Rust apps

    Debugging Rust applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking performance of your Rust apps, automatically surfacing errors, and tracking slow network requests and load time, try LogRocket.

    LogRocket is like a DVR for web apps, recording literally everything that happens on your Rust app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app’s performance, reporting metrics like client CPU load, client memory usage, and more.

    Modernize how you debug your Rust apps — .

    Anshul Goyal I love to code and use new technologies.

    Leave a Reply