Introducing Nameko (2.0)

This is an article from our archives. It was originally published in March 2015 by Matt Bennett.

Nameko is a microservices framework which is the fabric of the onefinestay backend. It’s been open-source since its inception as a point of principle, but its scope and utility has grown to the point where we think it could now really be useful to other teams.

To facilitate this we asked Armin Ronacher, author of some of our favourite Python libraries, for his perspective on the framework. In addition to contributing code for new HTTP and websocket support, he gave us a ton of other wisdom about how to successfully present an open-source project and helped us find some of the “weirdness” that you become blind to when you see it every day.

Over the last few months we’ve taken Armin’s advice and have just made a big headline release (2.0) that adds HTTP support, lots of documentation and a good deal more approachability. We actively worked towards reducing the “time to hello world”.

Features

Nameko is a framework that helps you write microservices in Python. It encourages you to structure your services in a such a way that promotes testable code with sensible separation of concerns.

Clean Service Structure

A Nameko service is just a class:

# helloworld.py
from nameko.rpc import rpc
class GreetingService(object):
name = "greeting_service"
    @rpc
def hello(self, name):
return "Hello, {}!".format(name)

A service’s “published interface” is specified by “entrypoints” that decorate the service methods (e.g. @rpc above).

Easy To Get Up And Running

Nameko includes command line tools for easily hosting and interacting with running services:

$ nameko run helloworld
starting services: greeting_service
...
$ nameko shell
>>> n.rpc.greeting_service.hello(name="Matt")
u'Hello, Matt!'

Useful Out Of The Box

Nameko ships with support for some common interfaces and protocols, including:

  • AMQP RPC
  • Asynchronous AMQP Events (pub-sub)
  • Simple HTTP
  • Timer for periodic actions
  • Websockets (experimental)

And the CLI for rapid development and utilities for unit and integration testing.

Dependency Injection

Nameko strongly encourages the use of the “dependency injection” pattern. Anything that a service depends on, such as a database or another service, is declared as a “dependency”.

Dependency providers are class attributes that encapsulate and hide the logic of the dependency. They are dynamically replaced at runtime with an object that provides the service with a clean interface to the underlying dependency.

For example:

from nameko.rpc import rpc, RpcProxy
class ServiceA(object):
name = "service_a"
    # service_a depends on the RPC interface of service_b
service_b_rpc = RpcProxy('service_b')
    def method(self):
# simple interface get service_b to ``do_something``
return self.service_b_rpc.do_something()

In the example above, self.service_b_rpc lets our “service_a” talk to “service_b” over RPC. The setup and teardown of AMQP connections are encapsulated into the RpcProxy dependency provider. This keeps the service code clean and lets the developer concentrate on application logic instead of dealing with the AMQP transport.

The separation of dependency code from service code makes it easier to test and helps us split responsibilities between developers.

Extensible

All entrypoints and dependencies are implemented as “extensions” to Nameko. If the “built-in extensions” aren’t sufficient it’s simple to add support for your own databases, protocols and transports.

Future Plans

Nameko is in constant use at onefinestay. These features are a natural progression that’ll help us grow, in particular by normalising our tech stacks around Nameko as a common toolkit.

Outside onefinestay we’re exited to see what the community makes of our framework. We think it has some neat design decisions that make it a clean and powerful choice for writing microservices in Python.

We have a long list of features that we’d like to add, and of course pull requests are welcome :)

Learn more about Nameko from the official documentation, get involved onGitHub or find us in #nameko on freenode.