How to build microservices with Nameko

Nameko is a microservices framework for Python that lets service developers concentrate on application logic and encourages testability. In this blog, we build a simple application using microservices architecture with the help of Nameko.

GraphQL has a role beyond API Query Language- being the backbone of application Integration
background Coditation

How to build microservices with Nameko

Microservices have become a popular way to build modern, scalable applications. They allow developers to break down large monolithic applications into smaller and more manageable components that can be developed, deployed and managed independently. Nameko is a Python framework that makes building and deploying microservices easy.

What is Nameko?

Nameko is a microservice framework for Python that provides tools and libraries to help you build and deploy microservices quickly and easily. It takes care of many of the common challenges which come while building the microservices, such as service discovery, service communication, and service management. So a developer can focus on writing your application logic. 

Key Features of Nameko:

  1. Service Discovery: Nameko provides built-in service discovery so that services can find and communicate with each other without having to hardcode the addresses of their dependencies.
  2. Service Communication: Nameko provides a simple and efficient communication mechanism for services to communicate with each other using Remote Procedure Calls (RPC).
  3. Service Management: Nameko provides tools to manage your services, such as a command line interface for starting and stopping services and a simple and flexible configuration system.

Building a Microservice with Nameko:

Building a microservice with Nameko is straightforward and can be done in just a few steps. Let's build a simple example project to understand how to build a microservice using nameko.

About the sample Project:

  1. This contains three nameko services

- Auth service: Helps to authenticate the user 
- Adder service: which takes 2 inputs and gives the addition of it 
- Multiplier service: which takes 2 inputs and returns the multiplication of it.

  1. One non-nameko service 

- API service: which will act as a gateway between the frontend and other nameko services 

This approach has one disadvantage which is the project will be dependent on the API service (but just for the illustration of the communication between the non-nameko and nameko services I’m using this approach)

When we build the microservice architecture we have to choose between code redundancy and inter-service dependency

  • Communication Between the services:

This requires RabbitMQ to communicate between the services because Nameko is using the built-in AMQP RPC features.
There are multiple ways to set up the message queue using RabbitMQ. A quicker way is to use the Cloud AMQP. Here’s the doc - cloudAMQP.
We would need the AMQP URL for the configuration of our services.

Let’s Code the services:  

Folder structure: create the following folder structure

Nameko sample project
  • API Gateway service

This will be the non-nameko service. So we are using a simple flask application as the api_gateway service. 

This api_gateway service acts as a middleware between the request and the nameko services. This receives the request and routes it to the respective nameko service and returns the response returned by the nameko service.

Since this is a non-nameko service, for this service to communicate with other nameko services we use the ClusterRpcProxy block. We have to configure it with the AMPQ URL 

API Gateway service

Build the API gateway service:

  • Create the virtual env, and activate it.
  • Install flask and nameko
  • Run the command “pip install Flask nameko”
API gateway service - Nameko

- To run the service - “python api_gateway.py run”

Authentication service:

The authentication service is the nameko service. It has 2 functions.

  • the login function is called from the api_gateway service - (non-nameko service to nameko service)
  • Verify_user function is called from the other nameko services - (nameko to nameko)

We Are using @rpc decorator to define functions as the RPC functions so that we could be able to call these functions from other services.

Build the Authentication service:

  • Create the virtual env, and activate it. 
  •  Install nameko
  • Run the command “pip install nameko” 
Authentication service - Nameko
  • Create the config.yaml file 
config.yaml file - Nameko
  • Run the Authentication service:

- Run - “nameko run --config ./config.yaml auth_service”

Note: Having the config.yaml file is not mandatory but it is recommended use. Since you’re building the microservices you may have a lot of services, so having the config.yaml file will help you keep the deployment of services in sync. - If you don't want to create the YAML file you can mention the broker URL in the command line and the service will still run, you can use the following command - “nameko run AuthService --broker amqps://***.cloudamqp.com/***”

Note - It's just for the demonstration of the authentication. This is not how an authentication service should look like  

  • Adder service:

Adder service is another nameko service.

  • RpcProxy is used to communicate with other nameko services.

In the adder service, we are calling the auth_service to verify the user.

Build the Adder service:

  • Create the virtual env, and activate it. 
  • Install nameko
  • Create the config.yaml file(same as above)
  • Run the service:

- Run -nameko run --config ./config.yaml adder_service

- if you don't want to create the YAML file you can still run the service using the command - “nameko run adder_service --broker amqps://***.cloudamqp.com/***”

adder service - Nameko

Multiplier service: 

Build the Multiplier service:

  • Create the virtual env, and activate it. 
  • Install nameko
  • Create the config.yaml file(same as above)
  • Run the service:

- Run -nameko run --config ./config.yaml multiplier_service

- if you don't want to create the YAML file you can still run the service using the command - “nameko run multiplier_service --broker amqps://***.cloudamqp.com/***”

Note: Click here to see the code - git_hub_link

Pros:

  1. Easy to use: Nameko provides a simple API that makes it easy to build and deploy microservices, even for developers who are new to microservices.
  2. Scalable: Nameko is designed to be scalable, so you can build and deploy microservices that can handle large amounts of traffic and data. You just need to run the nameko services on multiple machines and it’ll automatically redirect the traffic onto multiple machines.
  3. Flexible: Nameko provides a flexible architecture that allows you to build microservices with a variety of communication patterns, such as RPC, pub-sub, and HTTP.
  4. Well-documented: Nameko provides detailed documentation and it also provides examples to help you get started quickly.

Cons:

  1. Limited to Python: Nameko is a Python framework, so if you're building a microservice in another language, you'll need to use a different framework.
  2. Performance overhead: The use of remote procedure calls (RPC) in Nameko can result in performance overhead compared to other communication patterns.

Conclusion:

Nameko is a powerful and flexible microservice framework that makes it easy to build and deploy microservices in Python. It takes care of many of the common challenges which come while building microservices. So you can focus on writing your application logic. Whether you're building a new application from scratch or migrating an existing monolithic application to a microservices architecture, Nameko is a great choice for quickly building scalable and resilient microservices. However, it may not be the best choice for projects which need to be built in another programming language than python.

Hey there, I am Vishal. I am a full-stack developer with a passion to solve complex problems. I love to read, travel and click pictures in my free time.

Want to receive update about our upcoming podcast?

Thanks for joining our newsletter.
Oops! Something went wrong.