Microservices With Python and Flask
Microservices With Python and Flask
Miguel Grinberg
@miguelgrinberg
Agenda
● First hour
○ Introduction & demo
○ Microservices concepts
○ Class project design discussion
● Remaining time
○ Step-by-step development of the class project
○ (Focus is on techniques more than on a particular implementation)
About the MicroFlack Project
● MicroFlack is a microservices version of Flack
○ Flack is the chat server I used in the “Flask at Scale” class I gave at PyCon 2016
● The application lives on 7 (yes, seven!) GitHub repositories
● Runs on Python 3.4+ and Docker
● Not tied to any specific cloud or microservice vendor technology
Deploying MicroFlack to your Laptop
● Requirements
○ 4GB RAM (8GB recommended)
○ Vagrant
○ VirtualBox
○ Everything is installed in an Ubuntu 16.04 VM (Windows, Mac, Linux laptops are all OK!)
● Deployment commands:
git clone https://github.com/miguelgrinberg/microflack_admin
cd microflack_admin
vagrant up # to create the VM or restart it after shutdown
vagrant ssh # to open a shell session on the VM
vagrant halt # to shutdown the VM (without destroying it)
vagrant snapshot save clean # to save a snapshot with name “clean”
vagrant snapshot restore clean --no-provision # to restore the snapshot
vagrant destroy # to delete the VM
Monolithic Flack
MicroFlack
MicroFlack Features
● Five microservices, four HTTP/REST, one WebSocket
● Each service is a standalone Flask app
● Two of the services use MySQL databases
● Services run in Docker containers
● Services communicate over HTTP, message queue or service registry
● All services are load balanced
● Services scale independently of each other
● Upgrades can be done without downtime
Microservice Benefits
● Less complexity (maybe)
○ Awesome for teams with varying degrees of experience
● Scaling flexibility
● More reliability
● Less coupling
● More choice
● Deploy/upgrade while running
Microservice URL(s)
User interface /
/static/...
Users /api/users
/api/users/:id
Tokens /api/tokens
Messages /api/messages
/api/messages/:id
Socket.IO /socket.io
From Flack to MicroFlack
MicroFlack v0.1: Just the UI
UI Service Summary
● Endpoints