Book Image

Hands-On Microservices with Rust

By : Denis Kolodin
Book Image

Hands-On Microservices with Rust

By: Denis Kolodin

Overview of this book

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.
Table of Contents (19 chapters)

Using thread pools

To use thread pools, you don't need a special library. Instead, you can implement a scheduler that sends requests to a bunch of threads. You can even check the responses of workers to decide which thread to choose for processing, but there are ready-to-use crates that help to solve this issue more elegantly. In this section, we're going to look at the futures-cpupool and tokio-threadpool crates. 

CpuPool

Here, we'll reuse an existing microservice and remove the start_worker function, and the WorkerRequest and WorkerResult types. Keep the convert function and add a new dependency to Cargo.toml:

futures-cpupool = "0.1"

Import the CpuPool type...