Microservices

+

Nameko

History Lesson

November 2014

Martin Fowler & James Lewis published "microservices"

http://martinfowler.com/articles/microservices.html

Definition

Microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms

Monolith vs Microservices

  • Single process application
  • e.g. a Django site
  • Application divided into "services"
  • Deployed as separate processes

Reasons to Adopt Microservices

"Maintainability at Scale"

Independently Deployable

Independently Scalable

Freedom of Technology

Not "Monolithic"

Conway's Law

organizations which design systems... are constrained to produce designs which are copies of the communication structures of these organizations

Implications of Microservices

"You must be this tall to ride"

DevOps overhead

"Post CD Architecture"

Domain Knowledge

Decentralisation

ACID → BASE

  • Basically Available
  • Soft-state
  • Eventually Consistent

Complexity

Are Microservices for Me?

  • Are you fighting a monolith?
  • Are you ready to build a distributed system?

What About A Multi-lith?

"Sliding Scale"

Nameko

A microservices framework that lets service developers concentrate on application logic and encourages testability.

Nameko Concepts

  • Entrypoints
    • To interact with a service
  • Dependencies
    • For the service to interact with external things

Example I

                        
class HelloWorld(object):
    name = “hello”

    @http("GET", "/greet/<string:friend>")
    def greet(self, friend):
        return “Hello {}!”.format(friend)
                        
                    

https://github.com/mattbennett/nameko-europython-demo

Example II

                        
class HelloWorld(object):
    name = “hello”

    cache = CacheClient()  # dependency declaration

    @rpc
    def greet(self, friend):
        greeting = self.cache.get(friend)

        if greeting is None:
            greeting = “Hello {}!”.format(friend)  # expensive
            self.cache.put(friend, greeting)

        return greeting
                        
                    

Dependency Injection

                        
class HelloWorld(object):
    name = “hello”

    cache = CacheClient()

    @rpc
    def greet(self, friend):
        print(HelloWorld.cache)  # DependencyProvider
        print(self.cache)        # injected dependency

        greeting = self.cache.get(friend)

        if greeting is None:
            greeting = “Hello {}!”.format(friend)  # expensive
            self.cache.set(friend, greeting)

        return greeting
                         
                    
                        
<CacheClient [unbound] at 0x10cbb2750>    # DependencyProvider
<memcache.Client object at 0x10cbd25d0>   # injected dependency
                         
                    

Extensible

"Built-in" Extensions

  • HTTP GET & POST
  • AMQP RPC
  • AMQP Pub-Sub
  • Timer
  • Websockets (experimental)

Test Helpers

                        
from mock import call
from nameko.testing.services import worker_factory

hello_svc = worker_factory(HelloWorld)

hello_svc.cache.get.return_value = None

assert hello_svc.greet("Matt") == "Hello Matt!"
assert hello_svc.cache.get.call_args_list == [call("Matt")]
                         
                    
                        
import mockcache
from nameko.testing.services import worker_factory

fake_cache = mockcache.Client()
hello_svc = worker_factory(HelloWorld, cache=fake_cache)

assert hello_svc.greet("Matt") == "Hello Matt!"
assert fake_cache.get("Matt") == "Hello Matt!"
                         
                    

Summary - Microservices

  • Application divided into services
  • Running in their own processes
  • Maintainability at scale
    • Independently deployable
    • Independently scalable
    • Freedom of technology
    • Team structure

Summary - Microservices

  • "Grown up" architecture
  • Complex, distributed system
    • DevOps automation
    • Monitoring
    • Analysis
    • Awareness

    Try it out in a multi-lith

Summary - Nameko

  • Made for microservices
  • Encourages you to write clean, highly testable code
  • Useful "built-in" extensions
  • Designed to be extended

Thank you

Nameko

https://nameko.readthedocs.org

Me

matt@bennett.name ~ @mattjam ~ https://github.com/mattbennett

Presentation

https://mattbennett.github.io/presentation-microservices-nameko-europython15

Demo Files

https://github.com/mattbennett/nameko-europython-demo