Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

High Performance Python Microservice Communication

143 views

Published on

Python Microservices are becoming increasingly popular for large scale web applications. We’ll start by talking about some of the trade-offs of different microservice communication designs. Afterwards we’ll dive into code regarding specific configurations including RPC and traditional REST.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

High Performance Python Microservice Communication

  1. 1. High Performance Python Microservice Communication Joe Cabrera @greedoshotlast
  2. 2. Joe Cabrera ● Software engineer at Handshake ● Python programmer since 2009 ● Author on various open source Python projects Hi, I’m
  3. 3. Things to consider ● Speed ● Reliability ● Security ● Learning curve ○ Easy to learn ○ Easy to debug ○ Easy to fix
  4. 4. Traditional REST ● Serialization and Deserialization are expensive ● Decently reliable (HTTP) ● Several Authentication schemes ● Predefined Status Codes ● Easy learning curve ○ Lots of resources on it ○ Great frameworks built around it ○ Human-readable
  5. 5. GRPC ● 6x Faster ● Same reliable as REST ● More secure ○ Several Authentication schemes ○ Difficult to decode without schema ● Harder learning curve ○ Not as much documentation ○ Still very early days so you may have to roll your own ○ Not in plaintext so harder to debug ● Supports Duplex Streaming
  6. 6. HTTP/2 ● Fully binary protocol ● Multiplexed Streams ● Priorities and Dependencies ● Header Compression ● Reset stream ● Server Push
  7. 7. GRPC Syntax syntax = "proto2"; package example; message Person { required string name = 1; required int32 id = 2; optional string email = 3; }
  8. 8. Using GRPC # GPRC import person_pb2 person = person_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "jdoe@example.com" print(person.SerializeToString())
  9. 9. GRPC over HTTP/1.1 # GPRC import person_pb2 from django.http import HttpResponse from jwt_utils import encode_auth_token, decode_auth_token def send_response(request): if 'HTTP_AUTHORIZATION' in request.META and isinstance(decode_auth_token(request.META['HTTP_AUTHORIZATION']), int): person = person_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "jdoe@example.com" response = HttpResponse(person.SerializeToString()) return response return HttpResponse('Unauthorized', status=401)
  10. 10. GRPC over HTTP/2 # GPRC import person_pb2 # Twisted from twisted.web import server from twisted.web.resource import Resource from twisted.web.static import File from twisted.internet import reactor from twisted.internet import endpoints class Person(Resource): isLeaf = True def render_GET(self, request): person = person_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "jdoe@example.com" return person.SerializeToString() if __name__ == "__main__": site = server.Site(Simple()) server = endpoints.serverFromString( reactor, "ssl:port=5000:privateKey=key.pem:certKey=cert.pem", ) server.listen(site) reactor.run()
  11. 11. GRPC over HTTP/2 #GRPC import person_pb2 # Hyper from hyper import HTTPConnection conn = HTTPConnection(host='localhost', port=5000) conn.request('GET', '/') resp = conn.get_response() person = person_pb2.Person() data = person.ParseFromString(resp.read()) print "Person ID:", person.id print "Name:", person.name if person.HasField('email'): print "E-mail address:", person.email
  12. 12. Authentication with JSON Web Tokens # JWT import jwt SECRET_KEY = "webuildthiscity" def encode_auth_token(user_id): """ Generates the Auth Token """ try: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=5), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode( payload, SECRET_KEY, algorithm='HS256' ) except Exception as e: return e request.setHeader('Authorization', encode_auth_token('my_user'))
  13. 13. Authentication with JSON Web Tokens # JWT import jwt SECRET_KEY = "webuildthiscity" def decode_auth_token(auth_token): """ Decodes the auth token """ try: payload = jwt.decode(auth_token, SECRET_KEY) return payload['sub'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.' resp = conn.get_response() if decode_auth_token(response.headers['Authorization']):
  14. 14. Resources ● JSON Web Token - https://jwt.io/ ● PyJWT - https://pyjwt.readthedocs.io/en/latest/ ● GRPC - https://grpc.io/ ● Twisted - https://twistedmatrix.com/trac/ ● Hyper - http://hyper.readthedocs.io/en/latest/ ● HTTP/2 Spec - https://tools.ietf.org/html/rfc7540 ● HTTP/2 Explained - https://http2-explained.haxx.se/content/en/
  15. 15. Thank you! Watch @greedoshotlast for these slides

×