I’ve been asked recently, what’s the difference between Swagger and Thrift (or gRPC)? Although they look similar, they solve fundamentally different problems. Let’s look at the differences.

Swagger

At the most basic level, Swagger is a REST API specification language. The great part is that there is an entire ecosystem of tools built around this specification language to support API design, client and server code generation, and interactive documentation.

Key Features

  • REST + JSON API framework.
  • JSON requests and responses.
  • Code generation.
  • Documentation generation.

Thrift

Thrift is a software framework for supporting RPC. An interface definition language is used to describe your system in terms of data types and interfaces, the Thrift compiler generates client and server code that match your definition, and the Thrift library handles serialization and transport.

Key Features

  • RPC framework.
  • Code generation.
  • Documentation generation.
  • Modular serialization implementation.
  • Modular transport mechanism.

gRPC

gRPC shares a lot of similarities with Thrift. Instead of using Thrift’s interface definition language, gRPC uses protocol buffers. Instead of having a modular transport mechanism, gRPC uses HTTP/2.

Key Features

  • RPC framework.
  • Code generation.
  • Documentation generation.
  • Protobuf serialization.
  • HTTP/2 transport mechanism.

REST vs RPC

Each technology has a similar feature set, with the glaring exception of REST (Swagger) vs RPC (Thrift/gRPC). So what’s the difference?

With REST, your API is described using HTTP verbs and URIs. Each URI represents a resource in your system, and the HTTP verbs represent actions you take on those resources. By leveraging HTTP you take advantage of the interoperability and scale of the existing infrastructure of the Internet.

With RPC, you describe your API as a set of operations or procedures. RPC is agnostic to the transport mechanism used to implement your API, and can be implemented using HTTP, message queues, or files.

That is, comparing Swagger and Thrift/gRPC are like comparing apples and oranges. REST is a general architectural style for APIs leveraging HTTP and related Web technologies, while Thrift/gRPC are specific RPC systems. So, when to use REST, and when to use RPC?

The Comparison

REST is a widely adopted standard that any can be used without any additional support. If you have a reasonably defined API, teams can write their own client libraries to communicate with it. REST is widely interoperable, since the API definition is language and implementation agnostic.

RPC generally offers better performance, since it typically handles serialization using binary formats. With RPC, you can avoid parsing JSON responses or extracting query path parameters. RPC is also transport agnostic, and you may be able to leverage different behaviour by using an alternative transport mechanism like a message queue.

The Verdict

When should you use REST? If interoperability is your primary concern, nothing beats REST. There are no special technologies needed, and you can leverage all of the widely deployed infrastructure and tools that support the modern Web.

When should you use RPC? If performance is your primary concern, RPC can provide an edge by leveraging serialized data formats and alternative transport mechanisms.