Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RESTful Microservices with Python

RESTful Microservices with Python

Is it a good idea to write RESTful microservices in Python? We will see that is the case.

Antonio Ognio

July 24, 2014
Tweet

More Decks by Antonio Ognio

Other Decks in Technology

Transcript

  1. RESTful Microservices
    with Python
    Antonio Ognio
    Systems Architect at Red Científica Peruana
    L I G H T N I N G :. T A L K S:.

    View full-size slide

  2. What is REST
    anyway?

    View full-size slide

  3. What is a microservice
    architecture?

    View full-size slide

  4. Why is it a good idea to
    build this with Python?

    View full-size slide

  5. REST
    (REpresentational State Transfer)

    View full-size slide

  6. REST
    A way of taking full advantage of
    the existing web-related protocols and
    software in order to query and manipulate
    objects remotely.

    View full-size slide

  7. REST
    A way of taking full advantage of
    the existing web-related protocols and
    software in order to query and manipulate
    objects remotely.

    View full-size slide

  8. REST
    ✓ Was reverse-engineered by Roy Fielding for this
    PhD thesis from the existing web technologies after
    they were created (2000).
    Requires you to use provide links to your resources

    so your APIs are browseable.
    Requires you to respect to uniform interface of

    HTTP and use each of the verbs without violating
    their own semantics.

    View full-size slide

  9. REST
    HTTP Verb CRUD Action
    GET Read
    POST Update
    PUT Create
    DELETE Delete
    Isn't REST just a mapping of verbs to.. ?

    View full-size slide

  10. REST
    HTTP Verb CRUD Action
    GET Read
    POST Update
    PUT Create
    DELETE Delete
    Isn't REST just a mapping of verbs to.. ?
    WRONG!!!

    View full-size slide

  11. Some things you should be doing...
    ✓ Returning many different status codes not just 200 or 404:
    Accepted (201)
    Created (202)
    No Content (204)
    Moved Permanently (301)
    See Other (303)
    Bad Request (400)
    Unauthorized (401)
    Forbidden (403)
    Method Not Allowed (405)
    Not Acceptable (406)
    Conflict (409)
    Gone (410)
    Internal Server Error (500)
    etc.

    View full-size slide

  12. Some things you should be doing...
    ✓ Making sure one “thing” or “entity” in your domain problem have one
    and JUST ONE URI associated with it.
    Providing a “home document” at the root of your service linking to

    the main resources available.
    Using URI templates with named parameters so client code does not

    have to hard code URIs.
    Providing paginated results including hyperlinks to the next, prev,

    first and last pages.
    Returning 200 and not 404 when a search yields no results.

    Returning 400 if an input validation fails but a 409 if the data is OK

    but there is a business model conflict in the requested action.

    View full-size slide

  13. Master your HTTP codes
    httpstatus.es
    Point your browser to
    for a friendly refresher on available status codes
    and their actual meanings...
    Point your browser to

    View full-size slide

  14. Learn more about REST (1)
    Roy Fielding's PhD dissertation
    Grab your own free copy of
    http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
    ...it is surprisingly easy to read and very informative

    View full-size slide

  15. Learn more about REST (2)
    REST for the REST of us
    Grab your own free copy of
    http://vimeo.com/82725804
    ...a talk at gave at a conference in Chile last year
    https://speakerdeck.com/gnrfan/rest-for-the-rest-of-us

    View full-size slide

  16. Books on REST I recommend...

    View full-size slide

  17. Microservices

    View full-size slide

  18. Microservice Architecture
    Building a larger system or service by
    piecing together smaller services that are
    usually highly reusable, self-contained,
    conceptually simple and easy to maintain.

    View full-size slide

  19. Advantages of Microservices
    ✓ Once you define a public API and others start using it you can change
    the implementation as you which without breaking things.
    Each service is so small that one engineer can fully understand it and

    make a much better job of maintaining it.
    You are no longer limited to using an specific language, version or

    database for your whole system: use what is best for the job!
    Upgrading the system one micro-service at a time is much easier

    than upgrading a monolithic system.
    Prototype a microservice in a language you're comfortable with and

    re-implement it in any other as needed if that helps for speed or
    reliability reasons.

    View full-size slide

  20. Learn more about Microservices (1)
    Martin Fowler's article on Microservices
    Grab your own free copy of
    http://martinfowler.com/articles/microservices.html
    ..it is short but comprehensive and useful in general

    View full-size slide

  21. Learn more about Microservices (2)
    µCon: The Microservices Conference
    Attend
    https://skillsmatter.com/conferences/6312-mucon
    ..27th and 28th of November 2014, London, UK.

    View full-size slide

  22. Why is it a good idea
    to build microservices
    with ?

    View full-size slide

  23. Why Python? (for Microservices)
    ✓ The most important thing is to get the API right so prototyping
    quickly and easily is key and Python is great for that.
    There are good microframeworks available for Python, most notably

    Flask, you might not need a full-fledged Django installation.
    This is a great opportunity to build production code in Python 3. No

    more excuses!!!
    Chance to build asynchronous services using Tornado or Twisted.

    Re-implement using PyPy, Cython, Golang, Java, C++, Clojure, etc as

    needed if you require more speed or efficiency.
    You can build a Python web service frontend to many microservices

    in legacy languages like PHP, ASP, etc.

    View full-size slide

  24. Useful Python Packages
    ✓ requests
    uritemplate

    rfc3339

    Tornado

    hal-json

    halogen

    collection-json

    negotiator

    Flask-Restless

    tastypie

    djangorestframework

    https://github.com/gnrfan/django-restful

    View full-size slide

  25. RESTful Microservices
    with Python
    Antonio Ognio
    Systems Architect at Red Científica Peruana
    L I G H T N I N G :. T A L K S:.

    View full-size slide