Author: | Kenton Varda |
Programming Language: | C++ |
Genre: | Remote procedure call framework, serialization format and library, IDL compiler |
License: | MIT License |
Cap’n Proto is a data serialization format and Remote Procedure Call (RPC) framework for exchanging data between computer programs. The high-level design focuses on speed and security, making it suitable for network as well as inter-process communication. Cap'n Proto was created by the former maintainer of Google's popular Protocol Buffers framework (Kenton Varda) and was designed to avoid some of its perceived shortcomings.
Like most RPC frameworks dating as far back as Sun RPC and OSF DCE RPC (and their object-based descendants CORBA and DCOM), Cap'n Proto uses an Interface Description Language (IDL) to generate RPC libraries in a variety of programming languages - automating many low level details such as handling network requests, converting between data types, etc. The Cap'n Proto interface schema uses a C-like syntax and supports common primitives data types (booleans, integers, floats, etc.), compound types (structs, lists, enums), as well as generics and dynamic types.[1] Cap'n Proto also supports object-oriented features such as multiple inheritance, which has been criticized for its complexity.[2]
struct Date @0x5c5a558ef006c0c1
struct Contact @0xf032a54bcb3667e0 Values in Cap'n Proto messages are represented in binary, as opposed to text encoding used by "human-readable" formats such as JSON or XML. Cap'n Proto tries to make the storage/network protocol appropriate as an in-memory format, so that no translation step is needed when reading data into memory or writing data out of memory.[3] For example, the representation of numbers (endianness) was chosen to match the representation the most popular CPU architectures.[4] When the in-memory and wire-protocol representations match, Cap'n Proto can avoid copying and encoding data when creating or reading a message and instead point to the location of the value in memory. Cap'n Proto also supports random access to data, meaning that any field can be read without having to read the entire message.[5]
Unlike other binary serialization protocols such as XMI, Cap'n Proto considers fine-grained data validation at the RPC level an anti-feature that limits a protocol's ability to evolve. This was informed by experiences at Google where simply changing a field from mandatory to optional would cause complex operational failures.[6] [7] Cap'n Proto schemas are designed to be flexible as possible and pushes data validation to the application level, allowing arbitrary renaming of fields, adding new fields, and making concrete types generic.[8] Cap'n Proto does, however, validate pointer bounds and type check individual values when they are first accessed.
Enforcing complex schema constraints would also incur significant overhead,[9] negating the benefits of reusing in-memory data structures and preventing random access to data.[10] Cap'n Proto protocol is theoretically suitable[11] for very fast inter-process communication (IPC) via immutable shared memory, but as of October 2020 none of the implementations support data passing via shared memory.[12] However, Cap'n Proto is still generally considered faster than Protocol Buffers and similar RPC libraries.[13] [14]
Cap'n Proto RPC is network aware: supporting both handling of disconnects and promise pipelining, wherein a server pipes the output of one function into another function. This saves a client a round trip per successive call to the server without having to provide a dedicated API for every possible call graph. Cap'n Proto can be layered on top of TLS[15] and support for the Noise Protocol Framework is on the roadmap.[16] Cap'n Proto RPC is transport agnostic, with the mainline implementation supporting WebSockets, HTTP, TCP, and UDP.[17]
The Cap'n Proto RPC standard has a rich capability security model based on the CapTP protocol used by the E programming language.[18] As of October 2020, the reference implementation only supports level 2.
Cap'n Proto is often compared to other zero-copy serialization formats, such as Google's FlatBuffers and Simple Binary Encoding (SBE).[10] [19]
Cap'n Proto was originally created for Sandstorm.io, a startup offering a web application hosting platform with capability-based security. After Sandstorm.io failed commercially, the development team was acqui-hired by Cloudflare,[20] which uses Cap'n Proto internally.[21]