{"id":440,"date":"2019-11-28T19:01:44","date_gmt":"2019-11-29T03:01:44","guid":{"rendered":"http:\/\/modulesafari.com\/?p=440"},"modified":"2019-12-06T18:17:38","modified_gmt":"2019-12-07T02:17:38","slug":"served","status":"publish","type":"post","link":"http:\/\/modulesafari.com\/served\/","title":{"rendered":"Served; Build RESTful C++ servers"},"content":{"rendered":"\n

Served<\/a> is a C++ library for easy creation of highly performant web servers. It presents a clean and elegant modern C++ interface, drastically reducing the amount of boiler-plate code that would normally be needed. Overall, it looks very promising for when you want everything to just work, without compromising on performance. Now, let’s dive right into it. <\/p>\n\n\n\n

Getting To Hello World<\/h2>\n\n\n\n

Getting started is fairly standard for a from-source installation, you can also opt to compile it into a deb or rpm package via the build flags. Running the following commands installs served on your system. This installation requires Boost 1.53 or newer, if you do not have Boost installed on your system, you can install it using your favorite package manager or by following these instructions<\/a>.<\/p>\n\n\n\n

git clone https:\/\/github.com\/meltwater\/served.git\nmkdir served\/served.build && cd served\/served.build\ncmake ..\/served && make\nsudo make install<\/pre>\n\n\n\n

Now, that you have it installed, it is time to build a simple webserver. This server will just give back “Hello world!” when you query against the endpoint GET \/hello.<\/code><\/p>\n\n\n\n

#include <served\/served.hpp>\n\nint main() {\n\tserved::multiplexer mux;\n\n\tmux.handle(\"\/hello\")\n\t\t.get([](served::response & res, const served::request & req) {\n\t\t\tres << \"Hello world!\\n\";\n\t\t});\n\n\tserved::net::server server(\"0.0.0.0\", \"8080\", mux);\n\tserver.run(10);\n\n\treturn 0;\n}<\/pre>\n\n\n\n

To compile your program, you will need to link against the pthread, boost_system, and served shared objects, and use at least C++-11. On Linux, this would look roughly like the following.<\/p>\n\n\n\n

g++ main.cpp -o demo -std=c++17 -pthread -lboost_system -lserved<\/pre>\n\n\n\n

Just run the binary and visit the URL http:\/\/localhost:8080\/hello<\/a> in your browser, and you have successfully reached the hello-world. All of the code for this demo is available on github<\/a>.<\/p>\n\n\n\n

Performance<\/h2>\n\n\n\n

Let’s take a quick look at the performance of C++ Served. At only 60k, the output binary from that demo is surprising small on my system. Of course, this is not the statically linked binary size. For this benchmark, we will be using Vegeta<\/a> for load testing. <\/p>\n\n\n\n

Requests      [total, rate, throughput]    99999, 20000.17, 19057.02\nDuration      [total, attack, wait]        5.000256833s, 4.999908146s, 348.687\u00b5s\nLatencies     [mean, 50, 90, 95, 99, max]  2.539834ms, 375.457\u00b5s, 7.240899ms, 8.184653ms, 38.421652ms, 1.691368283s\nBytes In      [total, mean]                1238770, 12.39\nBytes Out     [total, mean]                0, 0.00\nSuccess       [ratio]                      95.29%\nStatus Codes  [code:count]                 0:4709  200:95290  \n\n<\/pre>\n\n\n\n

On my machine (Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz 16 threads), the basic hello world server was able to handle around 19,000 requests per second! Additionally, it was able to maintain relatively low CPU usage during this attack, using only ~30% of the capacity available to it. <\/p>\n\n\n\n

Requests      [total, rate, throughput]    150000, 30000.10, 14094.09\nDuration      [total, attack, wait]        7.948649187s, 4.999982507s, 2.94866668s\nLatencies     [mean, 50, 90, 95, 99, max]  9.756488ms, 5.137985ms, 7.529834ms, 7.989994ms, 51.273487ms, 6.782636245s\nBytes In      [total, mean]                1456377, 9.71\nBytes Out     [total, mean]                0, 0.00\nSuccess       [ratio]                      74.69%\nStatus Codes  [code:count]                 0:37971  200:112029 <\/pre>\n\n\n\n

As the request rate increases above 20k, the performance started to degrade. Here, it seems like 20k requests per second is the sweet spot. Despite this, the performance of the library is very satisfactory under the given load. The majority of requests took under 1ms when it was under the 20k stress test. This should be more than enough to meet the needs of all but the most extreme demands. <\/p>\n","protected":false},"excerpt":{"rendered":"

Served is a C++ library for easy creation of highly performant web servers. It presents a clean and elegant modern C++ interface, drastically reducing the amount of boiler-plate code that would normally be needed. Overall, it looks very promising for when you want everything to just work, without compromising on performance. Now, let’s dive right […]<\/p>\n","protected":false},"author":1,"featured_media":450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":""},"categories":[7],"tags":[14,13,16,17,18,15,19],"yst_prominent_words":[188,198,195,194,138,192,136,48,191,196,189,218,193,132,187,184,197,190,186,185],"yoast_head":"\nServed; Build RESTful C++ servers - Module Safari<\/title>\n<meta name=\"description\" content=\"Served is a C++ library for easy creation of highly performant web servers. It presents a clean and elegant modern C++ interface...\" \/>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/modulesafari.com\/served\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Served; Build RESTful C++ servers - Module Safari\" \/>\n<meta property=\"og:description\" content=\"Served is a C++ library for easy creation of highly performant web servers. It presents a clean and elegant modern C++ interface...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/modulesafari.com\/served\/\" \/>\n<meta property=\"og:site_name\" content=\"Module Safari\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-29T03:01:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-07T02:17:38+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/modulesafari.com\/wp-content\/uploads\/2019\/11\/served-logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"http:\/\/modulesafari.com\/#organization\",\"name\":\"Module Safari\",\"url\":\"http:\/\/modulesafari.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/modulesafari.com\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/modulesafari.com\/wp-content\/uploads\/2019\/11\/PNG-Logo-02-not-transparent.png\",\"width\":2026,\"height\":530,\"caption\":\"Module Safari\"},\"image\":{\"@id\":\"http:\/\/modulesafari.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/modulesafari.com\/#website\",\"url\":\"http:\/\/modulesafari.com\/\",\"name\":\"Module Safari\",\"description\":\"development simplified\",\"publisher\":{\"@id\":\"http:\/\/modulesafari.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/modulesafari.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/modulesafari.com\/served\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/modulesafari.com\/wp-content\/uploads\/2019\/11\/served-logo.png\",\"width\":300,\"height\":300},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/modulesafari.com\/served\/#webpage\",\"url\":\"http:\/\/modulesafari.com\/served\/\",\"name\":\"Served; Build RESTful C++ servers - Module Safari\",\"isPartOf\":{\"@id\":\"http:\/\/modulesafari.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/modulesafari.com\/served\/#primaryimage\"},\"datePublished\":\"2019-11-29T03:01:44+00:00\",\"dateModified\":\"2019-12-07T02:17:38+00:00\",\"description\":\"Served is a C++ library for easy creation of highly performant web servers. It presents a clean and elegant modern C++ interface...\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/modulesafari.com\/served\/\"]}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/modulesafari.com\/served\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/modulesafari.com\/served\/#webpage\"},\"author\":{\"@id\":\"http:\/\/modulesafari.com\/#\/schema\/person\/a31876f85a9f031cf62afd4e79f746aa\"},\"headline\":\"Served; Build RESTful C++ servers\",\"datePublished\":\"2019-11-29T03:01:44+00:00\",\"dateModified\":\"2019-12-07T02:17:38+00:00\",\"commentCount\":0,\"mainEntityOfPage\":{\"@id\":\"http:\/\/modulesafari.com\/served\/#webpage\"},\"publisher\":{\"@id\":\"http:\/\/modulesafari.com\/#organization\"},\"image\":{\"@id\":\"http:\/\/modulesafari.com\/served\/#primaryimage\"},\"keywords\":\"c++,cpp,http,rest,restful,served,server\",\"articleSection\":\"C++\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/modulesafari.com\/served\/#respond\"]}]},{\"@type\":[\"Person\"],\"@id\":\"http:\/\/modulesafari.com\/#\/schema\/person\/a31876f85a9f031cf62afd4e79f746aa\",\"name\":\"Nathaniel Blakely\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/modulesafari.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1d85a4c5bf257b6a8972ab9bbaee0b51?s=96&d=mm&r=g\",\"caption\":\"Nathaniel Blakely\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","_links":{"self":[{"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/posts\/440"}],"collection":[{"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":30,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":696,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/posts\/440\/revisions\/696"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/media\/450"}],"wp:attachment":[{"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/tags?post=440"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"http:\/\/modulesafari.com\/wp-json\/wp\/v2\/yst_prominent_words?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}