SlideShare a Scribd company logo
Cap’n Proto or: How I
Learned to Stop Worrying
and Love RPC
Razvan Rotari
What it is?
● Serialization protocol
● Like JSON but binary
● Similar to Protocol Buffers (same author)
Features
● Fast
● Platform independent format - always in little-endian
● Backwards compatible
● Cross language support
● Strong Typed
● Reference counted
● Time Travel RPC
Serialization
Uses a schema file to define the message structure.
The schema file is compiled to the target language using capnp.
capnp compile -oc++ schema.capnp
Will generate a schema.capnp.h and schema.capnp.c++ that need to be
included in your application
Serialization
Supported types:
● Void: Void
● Boolean: Bool
● Integers: Int8, Int16, Int32, Int64
● Unsigned Integers: UInt8, UInt16, UInt32, UInt64
● Floating-point: Float32, Float64
● Blobs: Text, Data
● Lists: List(T)
● Structs
● Generic types - Similar to Java Generics
● Unions
● Interfaces
● Methods
No support for dictionaries!
.capnp file example
#id generated by capnp
@0xed1e03e015818faa;
struct Person {
id @0 :UInt32;
name @1 :Text;
email @2 :Text;
phones @3 :List(PhoneNumber);
struct PhoneNumber {
number @0 :Text;
type @1 :Type;
enum Type {
work @0;
mobil @1;
home @2;
}
}
}
struct AddressBook {
contacts @0 :List(Person);
}
How to use it? Write
#include <AddressBook.capnp.h>
void writeAddressBook(int fd) {
::capnp::MallocMessageBuilder message;
AddressBook::Builder addressBook = message.initRoot<AddressBook>(); //Create the
root node
::capnp::List<Person>::Builder people = addressBook.initContacts(1);
Person::Builder ion = people[0];
ion.setName("Ion");
...
// Lists are fixed sized
::capnp::List<Person::PhoneNumber>::Builder ionPhones = ion.initPhones(1);
ionPhones[0].setNumber("0755-555-321");
ionPhones[0].setType(Person::PhoneNumber::Type::MOBILE);
writePackedMessageToFd(fd, message);
}
How to use it? Read
void printAddressBook(int fd) {
::capnp::PackedFdMessageReader message(fd);
AddressBook::Reader addressBook = message.getRoot<AddressBook>();
for (Person::Reader person : addressBook.getContacts()) {
std::cout << person.getName().cStr() << ": " << person.getEmail().cStr()
<< std::endl;
for (Person::PhoneNumber::Reader phone : person.getPhones()) {
std::cout << " " << " phone: " << phone.getNumber().cStr()
<< std::endl;
}
}
}
RPC
● Uses KJ concurrency framework
● Event driven
● Based on event loop, promises and callbacks
● Similar to node.js
● Can be used over TCP or UNIX sockets
RPC .capnp example
@0x952fc0868f401293;
interface User {
//Methods need to include a index number
login @0 (username :Text, password :Text) -> (token :AuthToken);
getAge @1 (token :AuthToken) -> (age :UInt32);
struct AuthToken {
owner @0 :Text;
token @1 :UInt64;
}
}
Server example
class UserImpl final: public User::Server {
public:
kj::Promise<void> login(LoginContext context) override {
if (context.getParams().hasUsername()) // All fields are optional by default
auto userName = context.getParams().getUsername();
auto token = context.getResults().getToken();
token.setToken(40);
return kj::READY_NOW;
}
};
…
capnp::EzRpcServer server(kj::heap<UserImpl>(), “127.0.0.1”, 5923);
auto& waitScope = server.getWaitScope(); //Register an event loop for this thread
kj::NEVER_DONE.wait(waitScope);
Client example
capnp::EzRpcClient client(“127.0.0.1”, 5923);
auto& waitScope = client.getWaitScope();
// Request the bootstrap capability from the server.
User::Client cap = client.getMain<User>();
// Create a request
auto request = cap.loginRequest();
request.setUsername("admin");
request.setPassword("123456");
auto promise = request.send(); // Make a call to the server.
// Wait for the result. This is the only line that blocks.
auto response = promise.wait(waitScope);
Time Travel!
The result of a RPC call can be used
immediately, even before the server receives it.
The only catch is that it can only be used in
another RPC request.
For example:
foo(bar(f())
Will do a single network round trip.
Limitations
● C++11 only
● Poor MSVC support (only serialization)
● Not 1.0 yet, but used in production
QUESTIONS?

More Related Content

PPTX
Add an interactive command line to your C++ application
PPTX
User defined function in c
PDF
Java 8-streams-collectors-patterns
PDF
SEH overwrite and its exploitability
PPTX
Unix signals
PPTX
SwtBot: Unit Testing Made Easy
PPT
Repetition Structure
PPTX
Unit 4. Operators and Expression
Add an interactive command line to your C++ application
User defined function in c
Java 8-streams-collectors-patterns
SEH overwrite and its exploitability
Unix signals
SwtBot: Unit Testing Made Easy
Repetition Structure
Unit 4. Operators and Expression

What's hot (20)

PPTX
Beneath the Linux Interrupt handling
PPTX
async/await のしくみ
PDF
Device Tree for Dummies (ELC 2014)
PPTX
User defined data types.pptx
PDF
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
PPT
Ipc in linux
PPTX
Linuxのsemaphoreとmutexを見る 
DOCX
linux file sysytem& input and output
PDF
oracle-osb
PPTX
While loop,Do While loop ,for loop
PDF
クリーンアーキテクチャの考え方にもとづく Laravel との付き合い方 #phpconokinawa
PDF
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
PDF
C programming session10
PDF
Postgres Vision 2018: WAL: Everything You Want to Know
 
PPT
Input And Output
PDF
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
PDF
Sass - css bomba[do]
PDF
Introduction to Perf
PDF
系統程式 -- 第 1 章
Beneath the Linux Interrupt handling
async/await のしくみ
Device Tree for Dummies (ELC 2014)
User defined data types.pptx
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Ipc in linux
Linuxのsemaphoreとmutexを見る 
linux file sysytem& input and output
oracle-osb
While loop,Do While loop ,for loop
クリーンアーキテクチャの考え方にもとづく Laravel との付き合い方 #phpconokinawa
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
C programming session10
Postgres Vision 2018: WAL: Everything You Want to Know
 
Input And Output
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Sass - css bomba[do]
Introduction to Perf
系統程式 -- 第 1 章
Ad

Similar to Cap'n Proto (C++ Developer Meetup Iasi) (20)

PDF
Claire protorpc
PDF
gRPC Design and Implementation
PDF
Serialization in Go
PPTX
GRPC.pptx
PPT
Introduction to Thrift
PPTX
Building your First gRPC Service
PDF
Apache Thrift
PDF
[grcpp] Refactoring for testability c++
PDF
Cloud Native API Design and Management
DOCX
project_docs
PDF
Real-time Streaming Pipelines with FLaNK
PDF
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
PPTX
CocoaConf: The Language of Mobile Software is APIs
PDF
Networking and Go: An Engineer's Journey (Strangeloop 2019)
PDF
Thrfit从入门到精通
PPTX
Rpc framework
DOC
Use perl creating web services with xml rpc
PDF
Microservices in Scala: Spray
PDF
Enforcing API Design Rules for High Quality Code Generation
PDF
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Claire protorpc
gRPC Design and Implementation
Serialization in Go
GRPC.pptx
Introduction to Thrift
Building your First gRPC Service
Apache Thrift
[grcpp] Refactoring for testability c++
Cloud Native API Design and Management
project_docs
Real-time Streaming Pipelines with FLaNK
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
CocoaConf: The Language of Mobile Software is APIs
Networking and Go: An Engineer's Journey (Strangeloop 2019)
Thrfit从入门到精通
Rpc framework
Use perl creating web services with xml rpc
Microservices in Scala: Spray
Enforcing API Design Rules for High Quality Code Generation
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Ad

More from Ovidiu Farauanu (13)

PDF
SIMD with C++ 26 by Alexandru Pentilescu
PDF
Introduction to C++20 Coroutines by Alex P
PDF
Interfacing C++ with Python to boost your legacy apps with Python interfaces
PDF
Back in Business with C++
PDF
Interface Oxidation
PDF
Optimization of the build times using Conan
PDF
Bind me if you can
PDF
Distributed Cache, bridging C++ to new technologies (Hazelcast)
PPTX
Monadic Computations in C++14
PDF
Domain Specific Languages and C++ Code Generation
PDF
Florentin Picioroaga - C++ by choice
PDF
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
PDF
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
SIMD with C++ 26 by Alexandru Pentilescu
Introduction to C++20 Coroutines by Alex P
Interfacing C++ with Python to boost your legacy apps with Python interfaces
Back in Business with C++
Interface Oxidation
Optimization of the build times using Conan
Bind me if you can
Distributed Cache, bridging C++ to new technologies (Hazelcast)
Monadic Computations in C++14
Domain Specific Languages and C++ Code Generation
Florentin Picioroaga - C++ by choice
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)

Recently uploaded (20)

PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
top salesforce developer skills in 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
medical staffing services at VALiNTRY
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
VVF-Customer-Presentation2025-Ver1.9.pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
top salesforce developer skills in 2025.pdf
System and Network Administration Chapter 2
wealthsignaloriginal-com-DS-text-... (1).pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
medical staffing services at VALiNTRY
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Odoo POS Development Services by CandidRoot Solutions
Upgrade and Innovation Strategies for SAP ERP Customers
CHAPTER 2 - PM Management and IT Context
Navsoft: AI-Powered Business Solutions & Custom Software Development

Cap'n Proto (C++ Developer Meetup Iasi)

  • 1. Cap’n Proto or: How I Learned to Stop Worrying and Love RPC Razvan Rotari
  • 2. What it is? ● Serialization protocol ● Like JSON but binary ● Similar to Protocol Buffers (same author)
  • 3. Features ● Fast ● Platform independent format - always in little-endian ● Backwards compatible ● Cross language support ● Strong Typed ● Reference counted ● Time Travel RPC
  • 4. Serialization Uses a schema file to define the message structure. The schema file is compiled to the target language using capnp. capnp compile -oc++ schema.capnp Will generate a schema.capnp.h and schema.capnp.c++ that need to be included in your application
  • 5. Serialization Supported types: ● Void: Void ● Boolean: Bool ● Integers: Int8, Int16, Int32, Int64 ● Unsigned Integers: UInt8, UInt16, UInt32, UInt64 ● Floating-point: Float32, Float64 ● Blobs: Text, Data ● Lists: List(T) ● Structs ● Generic types - Similar to Java Generics ● Unions ● Interfaces ● Methods No support for dictionaries!
  • 6. .capnp file example #id generated by capnp @0xed1e03e015818faa; struct Person { id @0 :UInt32; name @1 :Text; email @2 :Text; phones @3 :List(PhoneNumber); struct PhoneNumber { number @0 :Text; type @1 :Type; enum Type { work @0; mobil @1; home @2; } } } struct AddressBook { contacts @0 :List(Person); }
  • 7. How to use it? Write #include <AddressBook.capnp.h> void writeAddressBook(int fd) { ::capnp::MallocMessageBuilder message; AddressBook::Builder addressBook = message.initRoot<AddressBook>(); //Create the root node ::capnp::List<Person>::Builder people = addressBook.initContacts(1); Person::Builder ion = people[0]; ion.setName("Ion"); ... // Lists are fixed sized ::capnp::List<Person::PhoneNumber>::Builder ionPhones = ion.initPhones(1); ionPhones[0].setNumber("0755-555-321"); ionPhones[0].setType(Person::PhoneNumber::Type::MOBILE); writePackedMessageToFd(fd, message); }
  • 8. How to use it? Read void printAddressBook(int fd) { ::capnp::PackedFdMessageReader message(fd); AddressBook::Reader addressBook = message.getRoot<AddressBook>(); for (Person::Reader person : addressBook.getContacts()) { std::cout << person.getName().cStr() << ": " << person.getEmail().cStr() << std::endl; for (Person::PhoneNumber::Reader phone : person.getPhones()) { std::cout << " " << " phone: " << phone.getNumber().cStr() << std::endl; } } }
  • 9. RPC ● Uses KJ concurrency framework ● Event driven ● Based on event loop, promises and callbacks ● Similar to node.js ● Can be used over TCP or UNIX sockets
  • 10. RPC .capnp example @0x952fc0868f401293; interface User { //Methods need to include a index number login @0 (username :Text, password :Text) -> (token :AuthToken); getAge @1 (token :AuthToken) -> (age :UInt32); struct AuthToken { owner @0 :Text; token @1 :UInt64; } }
  • 11. Server example class UserImpl final: public User::Server { public: kj::Promise<void> login(LoginContext context) override { if (context.getParams().hasUsername()) // All fields are optional by default auto userName = context.getParams().getUsername(); auto token = context.getResults().getToken(); token.setToken(40); return kj::READY_NOW; } }; … capnp::EzRpcServer server(kj::heap<UserImpl>(), “127.0.0.1”, 5923); auto& waitScope = server.getWaitScope(); //Register an event loop for this thread kj::NEVER_DONE.wait(waitScope);
  • 12. Client example capnp::EzRpcClient client(“127.0.0.1”, 5923); auto& waitScope = client.getWaitScope(); // Request the bootstrap capability from the server. User::Client cap = client.getMain<User>(); // Create a request auto request = cap.loginRequest(); request.setUsername("admin"); request.setPassword("123456"); auto promise = request.send(); // Make a call to the server. // Wait for the result. This is the only line that blocks. auto response = promise.wait(waitScope);
  • 13. Time Travel! The result of a RPC call can be used immediately, even before the server receives it. The only catch is that it can only be used in another RPC request. For example: foo(bar(f()) Will do a single network round trip.
  • 14. Limitations ● C++11 only ● Poor MSVC support (only serialization) ● Not 1.0 yet, but used in production