Hands-On Microservices with Rust
eBook - ePub

Hands-On Microservices with Rust

Build, test, and deploy scalable and reactive microservices with Rust 2018

Denis Kolodin

Share book
  1. 520 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Microservices with Rust

Build, test, and deploy scalable and reactive microservices with Rust 2018

Denis Kolodin

Book details
Book preview
Table of contents
Citations

About This Book

A comprehensive guide in developing and deploying high performance microservices with Rust

Key Features

  • Start your microservices journey and get a broader perspective on microservices development using RUST 2018,
  • Build, deploy, and test microservices using AWS
  • Explore advanced techniques for developing microservices such as actor model, Requests Routing, and threads

Book Description

Microservice architecture is sweeping the world as the de facto pattern for building web-based applications. Rust is a language particularly well-suited for building microservices. It is a new system programming language that offers a practical and safe alternative to C.

This book describes web development using the Rust programming language and will get you up and running with modern web frameworks and crates with examples of RESTful microservices creation. You will deep dive into Reactive programming, and asynchronous programming, and split your web application into a set of concurrent actors. The book provides several HTTP-handling examples with manageable memory allocations. You will walk through stateless high-performance microservices, which are ideally suitable for computation or caching tasks, and look at stateful microservices, which are filled with persistent data and database interactions. As we move along, you will learn how to use Rust macros to describe business or protocol entities of our application and compile them into native structs, which will be performed at full speed with the help of the server's CPU.

Finally, you will be taken through examples of how to test and debug microservices and pack them into a tiny monolithic binary or put them into a container and deploy them to modern cloud platforms such as AWS.

What you will learn

  • Get acquainted with leveraging Rust web programming
  • Get to grips with various Rust crates, such as hyper, Tokio, and Actix
  • Explore RESTful microservices with Rust
  • Understand how to pack Rust code to a container using Docker
  • Familiarize yourself with Reactive microservices
  • Deploy your microservices to modern cloud platforms such as AWS

Who this book is for

This book is for developers who have basic knowledge of RUST, and want to learn how to build, test, scale, and manage RUST microservices. No prior experience of writing microservices in RUST is assumed.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Hands-On Microservices with Rust an online PDF/ePUB?
Yes, you can access Hands-On Microservices with Rust by Denis Kolodin in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in C++. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789341980
Edition
1

Reliable Integration with Databases

Persistent microservices have to store and load data. If you want to keep this data organized and reliable, you should use a database. Rust has third-party crates that support popular databases, and in this chapter, you'll learn about how to use different databases with Rust, including the following:
  • PostgreSQL
  • MySQL
  • Redis
  • MongoDB
  • DynamoDB
We will create utilities that will allow you to insert or remove data to and from the database, and to query the data held in the database.

Technical requirements

In this chapter, you'll need database instances to run our examples. The most effective way to run and work with a database for testing purposes is to use Docker. You can install databases locally, but seeing as we'll also need Docker for the remaining chapters, it's best to install and use it from this chapter.
We will use the following official images from Docker Hub:
  • postgres:11
  • mysql:8
  • redis:5
  • mongo:4
  • amazon/dynamodb-local:latest
You can get to know more about these images on the Docker Hub repository pages: https://hub.docker.com/.
We will also use the DynamoDB database, which is provided as part of Amazon Web Services: https://aws.amazon.com/dynamodb/.
If you want to interact with databases to check whether our examples work successfully, you'll also have to install the corresponding clients for each database.
You can find all of the examples for this chapter in the Chapter07 folder on GitHub: https://github.com/PacktPublishing/Hands-On-Microservices-with-Rust-2018/.

PostgreSQL

PostgreSQL is a reliable and mature database. In this section, we will explore how to start an instance of this database in a container, and how to connect to it from Rust using third-party crates. We will look at simple interactions with this database, and at the use of connection pools to get extra performance. We will start an instance of the database with Docker and create a tool to add records to a table and to query the list of added records before printing them to a console.

Setting up a test database

To create our database, you can use Docker, which automatically pulls all the necessary layers of the images containing the preinstalled PostgreSQL database. It's important to note that PostgreSQL has official images on Docker Hub, and you should opt to use these instead of unofficial ones, because the latter have a greater risk of malicious updates.
We need to start a container with a PostgreSQL database instance. You can do this using the following command:
docker run -it --rm --name test-pg -p 5432:5432 postgres
What does this command do? It starts a container from the postgres image (the latest version) and uses port 5432 on the localhost to forward it to the inner port, 5432, of the container (that is, the port exposed by the image). We also set a name with the --name argument. We give the container the name test-pg. You can use this name later to stop the container. The --rm flag will remove the anonymous volumes associated with the container when it's stopped. So that we can interact with the database from a Terminal, we've added -it flags.
The database instance will start and print something like the following to the Terminal:
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
........
The database is now ready for use. You can check it with the psql client, if you have it locally. The default parameters of the image are as follows:
psql --host localhost --port 5432 --username postgres
If you don't need the database anymore, you can use the following command to shut it down:
docker stop test-pg
But don't shut it down yet—let's connect to it with Rust.

Simple interaction with a database

The easiest way to interact with a database is to create a single connection directly to the database. Simple interaction is a straightforward database connection that doesn't use connection pools or other abstractions to maximize performance.
To connect to a PostgreSQL database, we can use two crates: postgres or r2d2_postgres. The first is a generic connection driver. The second, r2d2_postgres, is a crate for the r2d2 connection pools crate. We will start by using the postgres crate directly, without a pool from the r2d2 crate, and work on a simple utility to create a table, before adding commands to manipulate the data in that table.

Adding dependencies

Let's create a new project with all the necessary dependencies. We will create a binary utility for managing users in a database. Create a new binary crate:
cargo new --bin users
Next, add the dependencies:
cargo add clap postgres
But wait! Cargo doesn't contain an add command. I've installed cargo-edit tool for managing dependencies. You can do this with the following command:
cargo install cargo-edit
The preceding command installs the cargo-edit tool. If you don't install it, your local cargo won't have an add command. Install the cargo-edit tool and add the postgres dependency. You can also add dependencies manually by editing the Cargo.toml file, but as we are going to create more complex projects, the cargo-edit tool can be used to save us time.
The Cargo tool can be found here: https://github.com/killercup/cargo-edit. This tool contains three useful commands to manage dependencies: add to add a dependency, rm to remove an unnecessary dependency, and upgrade to upgrade versions of dependencies to their latest versions. Furthermore, with the awesome Edition 2018 of Rust, you don't need to use an extern crate ... declaration. You can simply add or remove any crates and all of them will be available immediately in every module. But what about if you add a crate that you don't need, and end up forgetting about it? Since the Rust compiler allows unused crates, you can add the following crate-wide attribute, #![deny(unused_extern_crates)], to your crate. This is necessary in case you accidentally add a crate that you won't use.
Also, add the clap crate. We need it for parsing arguments for our tool. Add the usages of all the necessary types, as follows:
extern crate clap;
extern crate postgres;

use clap::{
crate_authors, crate_description, crate_name, crate_version,
App, AppSettings, Arg, SubCommand,
};
use postgres::{Connection, Error, TlsMode};
All necessary dependencies have been installed, and our types have been imported, so we can create the first connection to a database.

Creating a connection

Before you can execute any query on a database, you have to establish a connection with the database you started in a container. Create a new Connection instance with the following command:
let conn = Connection::connect("postgres://postgres@localhost:5432", TlsMode::None).unwrap();
The created Connection instance has execute and query methods. The first method is used to execute SQL statements; the second is used to query data wi...

Table of contents