Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Microservices with Django

You're reading from  Hands-On Microservices with Django

Product type Book
Published in May 2024
Publisher Packt
ISBN-13 9781835468524
Pages 278 pages
Edition 1st Edition
Languages
Author (1):
Tieme Woldman Tieme Woldman
Profile icon Tieme Woldman

Table of Contents (18) Chapters

Preface 1. Part 1:Introducing Microservices and Getting Started
2. Chapter 1: What Is a Microservice? 3. Chapter 2: Introducing the Django Microservices Architecture 4. Chapter 3: Setting Up the Development and Runtime Environment 5. Part 2:Building the Microservices Foundation
6. Chapter 4: Cloud-native Data Processing with MongoDB 7. Chapter 5: Creating RESTful APIs for Microservices 8. Chapter 6: Orchestrating Microservices with Celery and RabbitMQ 9. Chapter 7: Testing Microservices 10. Chapter 8: Deploying Microservices with Docker 11. Part 3:Taking Microservices to the Production Level
12. Chapter 9: Securing Microservices 13. Chapter 10: Improving Microservices Performance with Caching 14. Chapter 11: Best Practices for Microservices 15. Chapter 12: Transforming a Monolithic Web Application into a Microservices Version 16. Index 17. Other Books You May Enjoy

Best Practices for Microservices

Microservices are a relatively recent development, but fortunately, there is already enough experience to look at best practices. With this, we can profit from earlier hands-on experience and avoid pitfalls to optimize microservices development.

In this chapter, we’ll examine best practices for Django microservices. By the end, you’ll know how to organize your code, document it, and control its versioning.

To achieve this, this chapter covers the following topics:

  • Organizing code
  • Documenting microservices
  • Logging and monitoring
  • Error handling
  • Versioning microservices

Some best practices for error handling and monitoring have been addressed before in Chapter 5, Creating RESTful APIs for Microservices, and Chapter 6, Orchestrating Microservices With Celery and RabbitMQ, yet we mention them here to group them in a complete chapter for easy access.

Technical requirements

One of the best practices for microservices concerns version control, and Git is an excellent tool for managing updates to and releasing microservices. If you want to apply Git, you can install it from https://git-scm.com/downloads.

You can find the code files for this chapter on GitHub at https://github.com/PacktPublishing/Hands-on-Microservices-with-Django/tree/main/Chapter11.

Organizing code

Organizing our microservices code is important for maintainability and collaboration with other developers. Depending on your needs and situation, consider applying the following best practices for organizing microservices code:

  • Apply the singe task principle
  • Separate responsibilities
  • Standardize the communication protocols
  • Containerize microservices
  • Apply version control
  • Document the code
  • Conduct code reviews

Now, let’s look further into these best practices to organize our code.

Apply the singe task principle

Develop each microservice to execute a single task. This helps keep the codebase focused and makes it easier to understand, test, and maintain. Both for fellow developers and ourselves when we need to change a microservice after a while.

For example, our match address worker is only concerned with matching a new address to base addresses. And the email worker only sends a confirmation email.

Separate responsibilities...

Documenting microservices

Documenting microservices is essential because it helps:

  • Fellow developers understand our code when they need to modify it or provide third-line support.
  • Us when we later change our code.
  • System administrators install and configure our microservices application.
  • Users to work with our application.

Depending on the requirements, documentation can be extensive and include a complete set of how-to guides and a reference. But at least make sure that you cover the following documentation:

  • Code comments to help yourself and other developers understand your code.
  • A README file to help others to install and use your software.
  • A RESTful API reference to help others to work with the API.

Let’s examine these documentation types into more detail.

Provide code comments

Commenting code helps you remember why you developed your code like you did. And it helps other developers understand your code when they need...

Logging and monitoring

Logging microservices can help track errors and failures. Monitoring helps optimize microservices and can signal exceptional occurrences like security attacks.

In Chapter 6, Orchestrating Microservices With Celery and RabbitMQ, we already examined monitoring Celery with the Flower package and RabbitMQ with its built-in features. Here, we focus more on signaling exceptions and reacting to such exceptions.

Depending on your situation and requirements, you can apply the following best practices for logging and monitoring:

  • Apply integrated logging
  • Implement log levels
  • Alert anomalies

Next, we’ll look further into these best practices.

Apply integrated logging

You can build your own logging system, but applying the standard logging module for Python is more efficient because it provides a comprehensive set of logging features.

For example, we can log the send mail error in send_email_task to a text file instead of printing...

Error handling

Error handling is essential for developing robust and resilient microservices.

Depending on the robustness requirements for your microservice application, error handling can vary from simply catching and logging errors to extensive retry mechanisms or gentle degradation.

Catching and logging errors should be the minimum for our microservices, so we’ll look specifically at that.

Catch and log errors

Python has the try...except block structure to catch errors and unexpected events and it’s good practice to incorporate this into your workers to make them robust and resilient. Moreover, if we log errors from try...except blocks, we combine logging and error handling, which further optimizes microservice execution.

For example, we can extend send_email_task to handle errors like this:

1 import logging
2 from smtplib import SMTPException
   ...
9 logging.basicConfig(filename="workers_logs.txt",
    ...

Versioning microservices

Versioning microservices is vital to ensure the controlled evolution of our microservices, and these best practices help us with that:

  • Apply semantic versioning.
  • Utilize RESTful API versioning.

Let’s further examine these best practices.

Apply semantic versioning

Apply semantic versioning for your microservices to express the meaning of the changes in a specific version. Semantic versioning follows the major.minor.patch structure for version numbers. Because of this, the version number tells how extensive a new version is.

For example, a version number like 2.0.0 indicates a major release with significant changes and potential impact. While a version number like 4.1.21 refers to a small patch with most likely little impact.

We could setup complete versioning for our Django microservices application (project) as described at https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/.

But if...

Summary

In this chapter, we examined the best practices for developing and running django microservices applications. We started with how to organize our code optimally. Then, we looked into documenting microservices. Next, we explored logging, monitoring, and error handling for microservices. Finally, we addressed versioning microservices.

With this, you can build even more advanced, robust, resilient, and maintainable microservices.

In the next and final chapter, we’ll learn how to transform an existing monolithic Django application into a microservices version.

Further reading

The best practices for Django microservices will keep evolving, and the Internet is a great place to discover new best practices at sites like:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Hands-On Microservices with Django
Published in: May 2024 Publisher: Packt ISBN-13: 9781835468524
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}