high-performance microservices with c/c++ c++ microservices.pdf · • why c/c++ for microservices?...

39
High-Performance Microservices with C/C++ Microservices Meetup Berlin Maximilian Haupt 2016/07/20

Upload: ngocong

Post on 26-Mar-2018

239 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

High-Performance Microservices with C/C++

Microservices Meetup BerlinMaximilian Haupt

2016/07/20

Page 2: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Agenda

• Why C/C++ for microservices?

• Evolution of a real-time bidding service

• Plot twist: JSON love & hate

• Getting nerdy

• Some benchmarks

Page 3: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Why C/C++?

Page 4: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Why C/C++?

• Response time of single request

• Flexibility, e.g. shared memory

• Scarce resources (embedded, IoT, ...)

• Many great libraries (TensorFlow, Vowpal Wabbit, OpenCV, ...)

• Existing code-base not always “bundle-able”

• Extensive and mature tools

• C11 and C++11 are beautiful languages ;-)

Page 5: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Why not C/C++?

Bjarne Stroustrup:

“C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.“

Page 6: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

http + json + /bid

Page 7: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Real-time bidding

https://github.com/openrtb/OpenRTB/raw/master/OpenRTB-API-Specification-Version-2-3-1-FINAL.pdf

Page 8: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Real-time bidding

https://github.com/openrtb/OpenRTB/raw/master/OpenRTB-API-Specification-Version-2-3-1-FINAL.pdf

Page 9: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

bidder wrap-up

• Started as a research project written in python

• Small RTBkit Intermezzo

• Later, rewritten in node.js

• Performance issues due to increasing traffic

Page 10: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

rtb stack

nginx

broker

DB/Cache

Logging

bidder

HTTP

ZeroMQ (CBFC)

HTTP

graphite

Page 11: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

HTTP + X

• CivetWeb: github.com/civetweb/civetweb

• Proxygen: github.com/facebook/proxygen

• Klone: github.com/koanlogic/klone

• lwan: github.com/lpereira/lwan

• libevhtp: github.com/ellzey/libevhtp

• httpp: github.com/daedric/httpp

• http-parser: github.com/nodejs/http-parser

• and many more: Mongoose, Mongrel2, libwebsockets, QHttp, QtWebApp, Wt, ...

Page 12: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

libevhtp hello world

Page 13: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

JSON

• JsonCpp github.com/open-source-parsers/jsoncpp

• RapidJSON github.com/miloyip/rapidjson

• Json11 github.com/dropbox/json11

• yajl lloyd.github.io/yajl/

• gason github.com/vivkin/gason

• ArduinoJson github.com/bblanchon/ArduinoJson

For a more complete list, e.g.: github.com/miloyip/nativejson-benchmark

Page 14: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

There is no silver bullet

• Serialization vs meat

• Large vs small JSON

• Multi-purpose backend vs single task

• Green field vs existing code

• Standalone vs embedded http server

• Normal vs high throughput

• Hard memory constraints

https://lisavandore.com/2015/04/17/8-popular-countertop-materials-the-pros-and-the-cons/

Page 15: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

TODO: put some nice broker graphs here

Adx/Adscale/…

Augmentation

Routing

Timeout

JSON

Dispatch

ZeroMQ + JSON

Sanity Checks

Adx/Adscale/…

(expensive stuff)

Linux, C++14 (g++ 4.8), boost::asio, httpp, JsonCpp, ZeroMQ, tcmalloc, Intel TBB

Req

uest

Res

pons

e

Page 16: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Btw: use latest JsonCpp and link it statically!

Page 17: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Broker evolution

• Augmentation with additional data

• Logging to Kafka

• QoS, load balancing, rate limiting

• Data scientists love data, i.e. traffic

Page 18: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

JSON love & hate

Page 19: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Schemas to the rescue

• JsonSchema helps. a bit. maybe

• Typos are still an issue

• Knowledge about types still spread throughout the code

• Still accessing json members through strings

• Start writing you own serialization

• Structs plus hand-crafted de-/serialization code

Page 20: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Protobuf to the rescue!!!11elf

Page 21: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Protobuf to the rescue!!!11elf – generated Classes

Page 22: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Protobuf to the rescue!!!11elf – Reflection

Page 23: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Protobuf to the rescue!!!11elf – Serialization

Page 24: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Protobuf to the rescue!!!11elf – Message::Clear()

Page 25: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Protobuf to the rescue!!!11elf – Documentation

• Thanks Google: openrtb.proto (github.com/google/openrtb)

Page 26: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Json to protobuf and back

• json-protobuf mapper using the generated reflection

• github.com/yinqiwen/pbjson (RapidJSON based)

• github.com/shramov/json2pb (jansson based)

• github.com/shafreeck/pb2json (jansson based)

• github.com/bivas/protobuf-java-format (Java)

• github.com/mafintosh/protocol-buffers (node.js)

• à Single place for schema validation and performance improvements

Page 27: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Json to protobuf and back

Page 28: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Drop the bass response times

Page 29: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Now ~3 billion requests/day

Page 30: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Let it sink

Page 31: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Use protobuf for all data structures!

http://knowyourmeme.com/memes/x-all-the-y

Page 32: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Sky is the limit

• Still more memory used than needed (raw buffer + json + protobuf)

• Use event-based json parser (e.g. yajl or RapidJson) for memory-efficiency

• Get rid of reflection (e.g. by also generating parser code)

Page 33: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Generate json state machine from proto file

0 1

2

3

5 6

4{

}

key:"a"

key:"b"key:"c"{

}

<number>

null

7 8

<string>

Page 34: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

protog

A protobuf json-parser generator!

https://github.com/0x7f/protog

$ protoc --cpp_out=. openrtb.proto$ protog -p openrtb.proto -i openrtb.pb.h \

-m com.google.openrtb.BidRequest

Will generate openrtb.pb.{cc,h} and bidrequest_parser.pb.{cc,h}

Page 35: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Disclaimer

• protog is not ready for production!

• Prototype which contains bugs

• Does not support self-referencing messages

• Many rough edges

Page 36: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Benchmark preface

• Never trust benchmarks other people present to you

• Highly recommend Brendan Gregg’s talks and blog: brendangregg.com

• All code is online: github.com/0x7f/cpp-meetup

Page 37: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

0 0,5 1 1,5 2 2,5 3

httpp + rapidjson + validation

node.js + ajv

node.js + JSON

nodejs (raw)

httpp + pbjson

httpp + rapidjson

httpp + jsoncpp

httpp + protog

httpp + ganson

proxygen (raw)

httpp (raw)

EC2 c4.4xlarge (14.04 LTS), 4 threads, wrk (c=200,t=12,d=60s), POST bidrequest.2.json (~1.5kb)

Page 38: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Summary

• Measure! Measure! Measure!

• Web-services are more than HTTP+JSON

• Mastering JSON is hard

• C++ is not much faster by default

• But, C++ can be very fast when used correctly

• No silver bullet – heavily depends on the use-case

Page 39: High-Performance Microservices with C/C++ C++ Microservices.pdf · • Why C/C++ for microservices? ... github.com/miloyip/nativejson-benchmark. There is no silver bullet ... •

Questions?

Maximilian [email protected]

github.com/0x7f