Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file...

37
Web-Services in C++ Maximilian Haupt || MUC++ || 2016/06/30 Building fast, memory-efficient and maintainable web-services in C++

Transcript of Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file...

Page 1: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Web-Services in C++

Maximilian Haupt || MUC++ || 2016/06/30

Building fast, memory-efficient andmaintainable web-services in C++

Page 2: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Background

• Professional C/C++ developer since 2006

• VR, Real-Time Audio, Game-Engine, Big-Data, Machine-Learning

Page 3: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

When talking about web-services

• Plain REST-like HTTP APIs talking JSON

• a.k.a. Micro-Services

• No fully fledged web-stack à Ruby on Rails, Django, MEAN, …

• No static file serving à nginx/apache

• No reverse proxy / load balancing à nginx/haproxy

• No SSL termination à nginx/haproxy

• No template rendering à frontend (Angular, React, …)

Page 4: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

RTB detour

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

Page 5: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

mbr targeting 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 6: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

rtb stack before

nginx

DB/Cache

Logging

bidder

HTTP

HTTP

Graphite

Page 7: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

rtb stack after

nginx

broker

DB/Cache

Logging

bidder

HTTP

ZeroMQ (CBFC)

HTTP

Graphite

Page 8: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

More than HTTP

• Proxygen: https://github.com/facebook/proxygen

• lwan: https://github.com/lpereira/lwan

• Wt: https://www.webtoolkit.eu/wt

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

• httpp: https://github.com/daedric/httpp - Hi Thomas! :)

• served: https://github.com/datasift/served

• libevhtp: https://github.com/ellzey/libevhtp

Page 9: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

JSON

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

• RapidJSON https://github.com/miloyip/rapidjson

• Json11 https://github.com/dropbox/json11

• yajl https://lloyd.github.io/yajl/

• gason https://github.com/vivkin/gason

• ArduinoJson https://github.com/bblanchon/ArduinoJson

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

Page 10: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 11: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Btw: use latest JsonCpp and link it statically!

Page 12: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Broker evolution

• Augmentation with additional data

• Logging to Kafka

• QoS, load balancing, rate limiting

• Data scientists love data, i.e. traffic

Page 13: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

JSON love & hate

Page 14: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 15: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Protobuf to the rescue!!!11elf

Page 16: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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

Page 17: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Protobuf to the rescue!!!11elf – Reflection

Page 18: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Protobuf to the rescue!!!11elf – Serialization

Page 19: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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

Page 20: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Protobuf to the rescue!!!11elf – Documentation

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

Page 21: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Json to protobuf and back

• json-protobuf mapper using the generated reflection

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

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

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

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

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

• à Single place for schema validation and performance improvements

Page 22: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Json to protobuf and back

Page 23: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Drop the bass response times

Page 24: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Now ~3 billion requests/day

Page 25: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Let it sink

Page 26: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Use protobuf for all data structures!

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

Page 27: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 by also generating parser code

Page 28: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 29: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 30: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Disclaimer

• protog is not ready for production!

• Prototype which contains bugs

• Does not support self-referencing messages

• Many rough edges

Page 31: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 32: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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 33: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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

Page 34: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

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

Page 35: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Summary

• Measure! Measure! Measure!

• Web-services are more than HTTP+JSON

• Mastering JSON is hard

• Schemas and generated code helps

• C++ can be very fast when used correctly

• Which library/tool for which use-case

Page 36: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Thank you!

Page 37: Building fast, memory-efficient and maintainable web ... C++ Web-Services.pdf · • No static file serving à nginx/apache ... (node.js) • à Single place for schema validation

Questions?

Maximilian [email protected]

github.com/0x7f