Hands-On Microservices with Rust

Book Description

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.

Downloading the example code for this ebook: You can download the example code files for this ebook on GitHub at the following link:https://github.com/PacktPublishing/Hands-On-Microservices-with-Rust-2018/. If you require support please email: customercare@packt.com

Publisher Resources

View/Submit Errata

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Microservices with Rust
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Introduction to Microservices
    1. Technical requirements
    2. What are microservices?
      1. Why we need microservices
      2. How to deploy a microservice
        1. Docker
        2. Continuous Integration
    3. How to split a traditional server into multiple microservices
      1. Reasons to avoid monoliths
        1. Impossible to scale vertically
        2. Impossible to update and deploy only one feature
        3. The failure of one server affects all features
      2. Breaking a monolithic service into pieces
        1. Definition of a REST API
          1. User registration microservice
          2. E-mail notifications microservice
          3. Product catalog  microservice
          4. Shopping cart microservice
          5. Payment Integration microservice
          6. Statistics collecting microservice
      3. Transformation to microservices
        1. Reusing existing microservices
    4. Why Rust is a great tool for creating microservices
      1. Explicit versus implicit
      2. Minimal amount of runtime errors
      3. Great performance
      4. Minimal dependencies burden
    5. Summary
    6. Further reading
  7. Developing a Microservice with the Hyper Crate
    1. Technical requirements
    2. Binding a Tiny Server
      1. Adding necessary dependencies
      2. The main function of the server
        1. Address of the server
        2. Server instances
        3. Setting the requests handler
        4. Adding the server instance to a runtime
      3. Building and running
        1. Rebuilding on changes
    3. Handling incoming requests
      1. Adding a service function
      2. Implementing a service function
      3. Index pages
    4. Implementing the REST principles
      1. Adding a shared state
      2. Accessing a shared state from a service function
      3. Parsing paths in a microservice
      4. Implementing REST methods
        1. Extracting the user's identifier
        2. Getting access to the shared data
        3. REST methods
          1. POST – Creating data
          2. GET – Reading data
          3. PUT – Updating data
          4. DELETE – Deleting data
    5. Routing advanced requests
      1. Defining paths with regular expressions
        1. Adding the necessary dependencies
        2. Writing regular expressions
          1. Path for index page
          2. Path for user management
          3.  Path for the users list
      2. Matching expressions
    6. Summary
  8. Logging and Configuring Microservice
    1. Technical requirements
    2. Adding logging to a microservice
      1. Random-value-generating microservices
      2. The log crate
        1. Loggers
        2. Log levels
        3. Logging messages
        4. Custom level of messages
        5. Checking logging is enabled
        6. Own target
      3. Using logging
        1. Configuring a logger with variables
          1. RUST_LOG
          2. RUST_LOG_STYLE
          3. Changing the RUST_LOG variable to your own
    3. Reading environment variables
      1. Standard library
      2. Using the .env file
        1. Using the dotenv crate
        2. Adding variables to the .env file
    4. Parsing command-line arguments
      1. Using the clap crate
        1. Adding dependencies
        2. Building a parser
        3. Reading arguments
        4. Usage
        5. How to add subcommands
    5. Reading the configuration from file
      1. Adding the TOML config
        1. Adding dependencies
        2. Declaring a struct for configuration
        3. Reading the configuration file
        4. Joining all values by a priority
        5. Creating and using the configuration file
    6. Summary
  9. Data Serialization and Deserialization with the Serde Crate
    1. Technical requirements
    2. Data formats for interaction with microservices
      1. The serde crate
        1. Serializing responses
        2. Deserializing requests
        3. Tweaking serialization
          1. Changing the case of names
          2. Removing a nesting
          3. Using specific names for a tag and content
        4. Any Value
      2. Using hyper
        1. Reading a body from a stream
      3. Custom types
        1. Custom serialization
        2. Custom deserialization
        3. Custom error types with the failure crate
        4. Binary data
      4. Compiling, running, and testing
    3. Microservices with multiple formats
      1. Different formats
        1. Parsing a query
        2. Checking different formats
      2. Transcoding
      3. XML support
    4. Summary
  10. Understanding Asynchronous Operations with Futures Crate
    1. Technical requirements
    2. Basic asynchronous types
      1. Basic types of future crate
        1. Using the Future trait
        2. Using the Stream trait
        3. Using Sink to send data back
      2. The channel module
        1. Channels for sending multiple messages
          1. Single-Producer Single-Consumer 
          2. Multi-Producer Single-Consumer
          3. Multi-Producer Multi-Consumer 
          4. Example of usage
        2. One-shot
        3. Using channels to use Sink in multiple places
      3. Executors
        1. Running futures and streams with blocking
        2. Using an executor
      4. The async/await syntax
    3. Creating an image service
      1. Uploading images
        1. The tokio crate
        2. Asynchronous input/output of files
        3. Multipart form requests
      2. Downloading images
        1. sendfile for sending files
      3. Testing the service
    4. Summary
  11. Reactive Microservices - Increasing Capacity and Performance
    1. Technical requirements
    2. What is a reactive microservice?
      1. Loose coupling
      2. Message-driven applications
      3. Asynchronous
        1. Should a reactive microservice be asynchronous?
        2. Reactive microservices with futures and streams
        3. Message brokers
        4. Remote procedure calls
          1. JSON-RPC
          2. gRPC
          3. Thrift
          4. Other RPC frameworks
        5. RPC and REST
      4. Reactive manifesto
    3. Understanding JSON-RPC
      1. How JSON-RPC works
      2. Creating a microservice
        1. Dependencies
        2. Client
        3. Worker
        4. Server
      3. Compiling and running
    4. Learning about gRPC
      1. How gRPC works
      2. Creating a microservice
        1. Dependencies
        2. Protocol
        3. Generating interfaces
        4. Shared client
        5. Client
        6. Server implementation
          1. Service implementation
          2. Handlers
          3. The main function
        7. Worker
      3. Compiling and running
    5. Summary
  12. Reliable Integration with Databases
    1. Technical requirements
    2. PostgreSQL
      1. Setting up a test database
      2. Simple interaction with a database
        1. Adding dependencies
        2. Creating a connection
        3. Wrapping with a tool
        4. Compiling and running
      3. Connection pools
        1. Creating a pool
        2. Parallel imports with the rayon crate
        3. Rayon
    3. MySQL
      1. Database for tests
      2. Connecting with the r2d2 adapter
        1. Adding dependencies
        2. Database interaction functions
        3. Creating a connection pool
    4. Redis
      1. Bootstrap database for tests
      2. Creating a connection pool
        1. Dependencies
        2. Adding commands and interaction functions
        3. Data manipulation functions
        4. Parsing arguments
        5. Connecting to Redis
        6. Executing subcommands
        7. Testing our Redis example
    5. MongoDB
      1. Bootstrapping a database for testing
      2. Connecting to a database using the r2d2 pool
        1. Interaction functions
        2. Parsing arguments
        3. Creating a connections pool
        4. Implementing subcommands
      3. Testing
    6. DynamoDB
      1. Bootstrapping a database for testing
      2. Connecting to DynamoDB
        1. Adding dependencies
        2. Interaction functions
        3. Parsing command-line arguments
      3. Testing
    7. Summary
  13. Interaction to Database with Object-Relational Mapping
    1. Technical requirements
    2. The diesel crate
      1. Adding the necessary dependencies
        1. diesel_cli
        2. Creating migrations
      2. Declaring the data structure
        1. Models
      3. Connecting to a database
        1. Parsing arguments
      4. Creating a connection
      5. Implementing subcommands using a DSL
        1. Adding a user subcommand implementation
        2. Listing users subcommand implementation
        3. Testing
    3. Complex database structure
      1. Business logic of the example application
        1. API methods
      2. Database structure and migrations
        1. Diesel initial setup
          1. up.sql
          2. down.sql
        2. Users table
          1. up.sql
          2. down.sql
        3. Channels table
          1. up.sql
          2. down.sql
        4. Memberships table
          1. up.sql
          2. down.sql
        5. Messages table
          1. up.sql
          2. down.sql
      3. Schema
      4. Models
        1. User
        2. Channel
        3. Membership
        4. Message
      5. Database interaction API crate
        1. Api
        2. Register user
        3. Create channel
        4. Publish channel
        5. Add member
        6. Add message
        7. Delete message
      6. Testing the crate
    4. Summary
  14. Simple REST Definition and Request Routing with Frameworks
    1. Technical requirements
    2. Rouille
      1. Creating a microservice
        1. Bootstrapping a server
        2. Handling requests
          1. Root handler
          2. Sign-up handler
          3. Sign-in handler
          4. Default handler
      2. Compiling and running
    3. Nickel
      1. Creating a microservice
        1. Bootstrapping a server
        2. Handling requests
        3. Worker for sending emails
      2. Compiling and running
    4. Rocket
      1. Creating a microservice
        1. Bootstrapping a server
        2. Handling requests
        3. Database schema and models
      2. Compiling and running
    5. Gotham
      1. Creating a microservice
        1. Types of framework
        2. The main function
        3. Handler implementation
      2. Running and testing
    6. Summary
  15. Background Tasks and Thread Pools in Microservices
    1. Technical requirements
    2. Interacting with threads
      1. Synchronous or asynchronous?
        1. IO-bound versus CPU-bound tasks
        2. Synchronous tasks inside asynchronous contexts
        3. The limitations of using IO operations on files
      2. Spawning a thread for image processing
      3. Interacting with a thread in an asynchronous way
    3. Using thread pools
      1. CpuPool
      2. The blocking section
    4. Actors
      1. Basics of the actix framework
      2. Implementing actors
        1. The count actor
          1. Types
          2. Actor
          3. Message
          4. Handler
        2. The log actor
          1. Types
          2. Actor
          3. Message
          4. Handler
        3. The resize actor
          1. Types
          2. Actor
          3. Message
          4. Handler
      3. Using the server with actors
        1. Requests handler
      4. Building and running
    5. Summary
  16. Involving Concurrency with Actors and the Actix Crate
    1. Technical requirements
    2. Actor concurrency in microservices
      1.  Understanding actors
        1. Actors in microservices
      2. The Actix framework
    3. Creating a microservice with actix-web
      1. Bootstrapping an actix-web server
        1. Starting a server
        2. App creation
        3. Scope and routes
        4. Static files handler
      2. HTTP client
        1. GET requests
        2. POST requests
      3. Handlers
        1. Signup
        2. Signin
        3. New comment
        4. Comments
        5. Counter
      4. Middleware
      5. Building and running
    4. Using databases
      1. The database interaction actor
        1. Actors
        2. Messages
          1. Setting a value message
          2. Get value message
        3. Link to actor
      2. Using database actors
    5. WebSocket
      1. Repeater actor
        1. Actors
        2. Messages
          1. Updating the message
          2. Control message
      2. The notification actor
        1. Actor
        2. Handlers
      3. Adding WebSocket support to a server
    6. Summary
  17. Scalable Microservices Architecture
    1. Technical requirements
    2. Scalable architecture
      1. Basic ideas
      2. Message brokers and queues
        1. RabbitMQ
        2. Kafka
      3. Application bottlenecks
    3. Scalable application with Rust and RabbitMQ
      1. Bootstrap message broker for testing
      2. Dependencies
      3. Abstract queue interaction actor
        1. Dependencies
        2. Abstract messages handler
        3. Actor
        4. Handling an incoming stream
        5. Sending a new message
        6. Utility methods
        7. Crate
        8. Request and response
      4. Worker
        1. Dependencies
        2. Handler
        3. main function
      5. Server
        1. Dependencies
        2. Shared state
        3. Server handler
        4. Requests handlers
          1. Index handler
          2. Tasks handler
          3. Upload handler
        5. main function
      6. Testing
      7. How to scale this application
    4. Summary
  18. Testing and Debugging Rust Microservices
    1. Technical requirements
    2. Testing microservices
      1. Unit testing
        1. Mocking
        2. Starting a server for testing
        3. Making requests
        4. Implementing tests
        5. Running our tests
      2. Integration testing
        1. Starting an application instance
        2. Dependencies
        3. Utils
        4. The integration testing client
        5. Types
        6. Users
        7. Content
        8. Mailer
        9. Router
    3. Debugging microservices
      1. curl
      2. Postman
      3. mitmproxy
      4. LLDB
      5. Visual Studio Code
    4. Structural logging
      1. Example
      2. Building and testing
    5. Distributed tracing
      1. Starting Jaeger
      2. Generating spans
      3. Compile and run
    6. Summary
  19. Optimization of Microservices
    1. Technical requirements
    2. Performance-measuring tools
      1. Welle
      2. Drill
    3. Measuring and optimizing performance
      1. Basic example
      2. Performance
      3. Optimizations
        1. State sharing without blocking
        2. Reusing values by references
        3. Caching
      4. Compilation with optimizations
    4. Optimization techniques 
      1. Link-time optimizations
      2. Abort instead of panicking
      3. Reducing the size of binaries
      4. Isolated benchmarks
      5. Profiling
    5. Summary
  20. Packing Servers to Containers
    1. Technical requirements
    2. Building a Docker image with a microservice
    3. Creating an image with the Rust compiler
      1. Users microservice image
        1. .dockerignore
        2. Dockerfile
    4. Building an image
      1. Starting a container
      2. Content microservice image
      3. Email microservice image
      4. Router microservice image
      5. DBSync worker image
        1. Dependencies
        2. The main function
      6. Hiding microservice source code 
    5. Composing a microservice set
      1. Application definition
      2. Database container
      3. A container with an email server
      4. DBSync worker container
      5. Mails microservice container
      6. Users microservice container
      7. Content microservice container
      8. Router microservice container
      9. Running the application
    6. Adding persistent state to the application
      1. Running the application in the background
    7. Summary
  21. DevOps of Rust Microservices - Continuous Integration and Delivery
    1. Technical requirements
    2. Continuous integration and continuous delivery
      1. Continuous integration 
      2. Continuous delivery 
      3. Container orchestration
    3. Rust tools
      1. Rustfmt
        1. Installation
        2. Usage
        3. Configuration
      2. Clippy
        1. Installation
        2. Usage
        3. Configuration
        4. Recommended code attributes
      3. Rustfix
        1. Installation
        2. Usage
      4. Cargo test
    4. CI and CD tools
      1. TravisCI
      2. AppVeyor
      3. Jenkins
    5. Demonstration of continuous integration
      1. Docker Compose
        1. The SCM server
        2. The CI server
        3. The CI agent
        4. The image
      2. Configuring Gogs
      3. Configuring TeamCity
      4. Authorizing agents
      5. Creating a project
      6. Building steps for Rust
      7. Building with CI
    6. Summary
  22. Bounded Microservices with AWS Lambda
    1. Technical requirements
    2. Serverless architecture
      1. AWS Lambda
      2. Azure Functions
      3. Cloudflare Workers
      4. IBM Cloud Functions
      5. Google Cloud Functions
    3. Minimal Rust microservices for AWS Lambda
      1. Dependencies
      2. Developing a microservice
      3. Building
      4. Deployment
    4. Serverless Framework
      1. Preparation
      2. Implementation
        1. Dependencies
        2. Handler
        3. Request and response types
        4. Functions
      3. Configuration
        1. Resources
      4. Deployment
        1. Permissions
        2. Script
        3. Running
      5. Testing
      6. Updating and removing
    5. Summary
  23. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Hands-On Microservices with Rust
  • Author(s): Denis Kolodin
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789342758