Cover....1
Title Page....2
Copyright and Credits....3
Dedication....4
Contributors....5
Table of Contents....8
Preface....14
Part 1: Introducing Microservices and Getting Started....20
Chapter 1: What Is a Microservice?....22
Comparing monolithic web applications and microservices....23
A monolithic version of a Discount Claim app....23
A microservices version of the Discount Claim app....25
Characteristics of microservices....26
Exploring the microservices architecture and its components....27
An analogy to deepen our understanding of microservices....29
Listing the benefits of microservices....30
The drawbacks of microservices....31
Distinguishing types of microservice....31
Cloud-native microservices....31
Reactive microservices....33
Designing microservices....34
Analyzing the user story....34
Split the user story into use cases....35
Summary....38
Chapter 2: Introducing the Django Microservices Architecture....40
Technical requirements....40
Exploring Django’s native components for microservices web applications....41
DRF....41
Django’s Cache Framework....43
Traversing the external components for Django microservices web applications....47
Task and message queue brokers....47
Container software....49
The complete Django microservices architecture....50
Creating a sample microservice....50
Implementation 1: Offloading a task with a RabbitMQ microservice....52
Implementation 2: Offloading a task with a Celery microservice....58
Summary....62
Chapter 3: Setting Up the Development and Runtime Environment....64
Technical requirements....64
Setting up the development environment....65
Extra setup for Windows developers....65
Installing the required Python packages....68
Setting up the runtime environment....70
Installing Docker Desktop....71
Installing RabbitMQ as a Docker container....72
Installing Redis as a Docker container....73
Signing up for MongoDB and working from VS Code....73
Analyzing the sample microservices application....75
Matching an address....75
The app’s requirements as user stories....76
Splitting the requirements into use cases....78
Phasing the development of the sample application....80
Summary....80
Part 2: Building the Microservices Foundation....82
Chapter 4: Cloud-native Data Processing with MongoDB....84
Technical requirements....85
Introducing MongoDB and cloud-native databases....85
What are cloud-native databases?....85
MongoDB is a NoSQL database....86
Setting up MongoDB....88
Optional: creating a paid cluster for production databases....89
Creating a database user....89
Setting up our MongoDB cluster for Django....90
Creating a Database....91
Creating documents inside a collection....91
Updating documents....92
Deleting documents and collections....92
Mapping CRUD operations to HTTP methods....93
CRUD operations on MongoDB with Django ORM....94
CRUD operations on MongoDB with pymongo....100
Cleaning up....103
Summary....103
Chapter 5: Creating RESTful APIs for Microservices....104
Technical requirements....105
Introducing RESTful APIs....105
Benefits of RESTful APIs....107
The RESTful API architecture....107
Building RESTful APIs with DRF....109
Setting up DRF....111
Creating a model and a serializer....112
Creating a view and the URL endpoints....113
Browsing a DRF RESTful API....125
Error handling....127
Handling wrong-formatted requests....127
Handling validation errors....128
Summary....129
Chapter 6: Orchestrating Microservices with Celery and RabbitMQ....130
Technical requirements....131
Introducing task queues....131
Implementing the work queue scenario....132
Implementing the Publish-Subscribe scenario....135
Implementing the Request-Response scenario....138
Exploring Celery and RabbitMQ....142
Celery....143
RabbitMQ....144
Creating and running asynchronous tasks....146
Creating and running a Celery-based task....147
Creating and running a RabbitMQ-based task....160
Monitoring tasks and task queues....166
Monitoring Celery tasks with Flower....166
Monitoring RabbitMQ tasks....167
Summary....168
Chapter 7: Testing Microservices....170
Technical requirements....171
Introducing testing microservices....171
Unit testing microservices....173
Creating and running happy path tests....174
Creating and running boundary tests....179
End-to-end testing microservices....182
Automated testing with Selenium....185
Summary....188
Chapter 8: Deploying Microservices with Docker....190
Technical requirements....191
Introducing Docker....191
Benefits of Docker (containers)....192
Containerizing microservices....192
Applying multi-container deployment with Docker Compose....196
Deploying a Django microservices application....199
Showing a list of the images we created....200
Showing a list of created and running containers....200
Inspecting the console output of a container....201
Stopping a container....201
Starting a container....202
Removing a container....202
Removing an image....202
Deploying a new microservices version....203
Scaling microservices....204
Vertical and horizontal scaling....204
Docker Swarm....206
Kubernetes....206
Summary....207
Part 3: Taking Microservices to the Production Level....208
Chapter 9: Securing Microservices....210
Technical requirements....211
Introducing microservices security....211
North-south security for microservices....211
East-west security for microservices....213
Token-based security with JWT....214
User-based security with OAuth 2.0....215
Controlling access to microservices....215
Securing data communication between microservices....221
Summary....223
Chapter 10: Improving Microservices Performance with Caching....224
Technical requirements....225
Introducing caching....225
Applying Django’s cache framework....227
Caching a web page....229
Caching page data....231
Using Redis for caching....234
Redis as a backend cache for Django’s cache framework....234
Redis as a standalone cache....235
Summary....238
Chapter 11: Best Practices for Microservices....240
Technical requirements....240
Organizing code....241
Apply the singe task principle....241
Separate responsibilities....241
Standardize the communication protocols....242
Containerize microservices....242
Apply version control....243
Document the code....243
Conduct code reviews....243
Documenting microservices....244
Provide code comments....244
Create a README file for the microservices application....245
Document RESTful APIs....246
Logging and monitoring....246
Apply integrated logging....247
Implement log levels....248
Log context information....248
Alert anomalies....249
Error handling....249
Catch and log errors....249
Other error handling options....250
Versioning microservices....251
Apply semantic versioning....251
Utilize RESTful API versioning....252
Summary....252
Further reading....253
Chapter 12: Transforming a Monolithic Web Application into a Microservices Version....254
Introducing the transformation approach....255
Implementing the approach step by step....255
Determining requirements....256
Decomposing the monolith....257
Designing the microservices....260
Selecting the technology....261
Creating the data foundation....262
Developing the microservices....263
Testing and deploying....264
Summary....265
Index....266
Other Books You May Enjoy....275
Are you a Django developer looking to leverage microservices to create optimized and scalable web applications? If yes, then this book is for you. With microservices, you can split an application into self-contained services, each with a specific scope running asynchronously while collectively executing processes. Written by an experienced Python developer, Hands-On Microservices with Django teaches you how to develop and deploy microservices using Django and accompanying components such as Celery and Redis. You'll start by learning the principles of microservices and message/task queues and how to design them effectively. Next, you’ll focus on building your first microservices with Django RESTful APIs (DFR) and RabbitMQ, mastering the fundamentals along the way. As you progress, you’ll get to grips with dockerizing your microservices. Later, you’ll discover how to optimize and secure them for production environments. By the end of this book, you’ll have the skills you need to design and develop production-ready Django microservices applications with DFR, Celery/RabbitMQ, Redis, and Django's cache framework.