» Python: Building Event-Driven Microservices with Kafka » 3. Consumer: Trend Service » 3.1 Service Design

Service Design

Event Queue

As shown in this diagram, the Trend Service consumes “search book“ events from the queue, analyzes them and lists the “trendy“ books to the users.

Our trend service should have at least 2 components:

Trend Service Arch

  • Consumer: It consumes and analyzes events from the queue.
  • API Server: It lists trends to the users.

We‘ll have 3 services in this tutorial, but we don‘t have to make 3 repositories since they're all written in Python. Let's try the monorepo.

Monorepo

A monorepo, short for "monolithic repository," is a software development approach where multiple projects or components of a system are stored within a single repository.

We need to structure the project like this:

.
├── LICENSE
├── README.md
├── requirements.txt
└── service
    ├── domain
    │   └── model
    │       ├── __init__.py
    │       ├── book.py
    │       └── trend.py
    ├── recommendation
    │   └── main.py
    ├── trend
    │   └── main.py
    └── web
        ├── adapter
        │   ├── router.py
        │   └── templates
        │       └── index.html
        ├── application
        │   ├── __init__.py
        │   ├── dto
        │   │   ├── __init__.py
        │   │   └── book.py
        │   ├── executor
        │   │   ├── __init__.py
        │   │   └── book_operator.py
        │   └── wire_helper.py
        ├── config.yml
        ├── domain
        │   └── gateway
        │       ├── __init__.py
        │       └── book_manager.py
        ├── infrastructure
        │   ├── config
        │   │   ├── __init__.py
        │   │   └── config.py
        │   ├── database
        │   │   ├── __init__.py
        │   │   └── mysql.py
        │   └── mq
        │       ├── __init__.py
        │       ├── helper.py
        │       └── kafka.py
        └── main.py

Restructure the folders and files

  1. Create service/ directory and move the whole web/ directory into service/.
  2. Create service/domain/model dir. We‘ll use it to put the public models for all 3 servcies.
  3. Move all files in service/web/domain/model to service/domain/model. Update its related import paths as well.

For example, in service/web/domain/gateway/book_manager.py:

...
from ....domain.model import Book
...
PrevNext