tRPC, gRPC, GraphQL or REST: when to use what?

Software Developer Diaries
19 Mar 202310:46

Summary

TLDRThis video provides a comprehensive overview of different approaches for client-server communication, including REST APIs, GraphQL, tRPC, and gRPC. The narrator delves into the strengths and weaknesses of each architecture, highlighting factors like scalability, bandwidth efficiency, and ease of implementation. They emphasize the importance of choosing the right approach based on specific project requirements, such as data complexity, bandwidth constraints, and the need for real-time streaming. The video aims to equip viewers with a solid understanding of the available options, enabling them to make informed decisions when building robust client-server communication systems.

Takeaways

  • πŸ‘‰ REST APIs are easy to build but don't scale well due to tight coupling between client and server, leading to overfetching/underfetching issues and potential conflicts between frontend and backend teams.
  • πŸ“‘ GraphQL eliminates overfetching/underfetching problems by allowing clients to request only the data they need, and creates a contract between frontend and backend through a predefined schema.
  • ⚠️ While powerful, implementing GraphQL correctly requires expertise, and its client libraries like Apollo can be complex and introduce caching issues.
  • πŸ”‘ REST is suitable for simple data structures, rapid iteration, and when bandwidth is not a major concern, while GraphQL is better for complex, nested data and bandwidth-constrained environments like mobile apps.
  • πŸŒ‰ tRPC (Typescript RPC) brings the frontend and backend closer by allowing the client to directly call server controllers as if they were part of the same application, with strong typing and a shared schema.
  • πŸš€ tRPC runs on HTTP/2, has optimizations out-of-the-box, is lightweight compared to GraphQL, and allows easy navigation between client and server code in the IDE.
  • πŸ“¦ tRPC has a smaller bundle size than GraphQL and doesn't require bulky client/server packages.
  • πŸ” GraphQL is preferred when requesting specific data from nested structures or when bandwidth is a concern, while tRPC is suitable when using JavaScript/TypeScript on both client and server.
  • 🌐 gRPC (Google RPC) is optimized for low bandwidth and real-time streaming, using a binary protocol (Protobuf) instead of JSON, and is suitable for communication between microservices written in different languages.
  • ⏳ While powerful, gRPC's maturity lags behind REST and GraphQL as of the video's recording time.

Q & A

  • What is the main difference between REST and GraphQL architectures?

    -The main difference is that REST is an architectural style, while GraphQL is a query language. REST APIs can suffer from over-fetching or under-fetching data, while GraphQL eliminates this issue by allowing clients to fetch only the data they need.

  • What are the advantages of using GraphQL over REST APIs?

    -The key advantages of GraphQL include: 1) Eliminating over-fetching and under-fetching of data, 2) Creating a contract between the backend and frontend teams, 3) Allowing clients to fetch data in a single query, saving bandwidth, and 4) Enabling mobile apps and websites to easily fetch the required data without separate endpoints.

  • What are the downsides of using GraphQL?

    -The main downsides of GraphQL mentioned are: 1) It's very hard to get GraphQL right without a GraphQL expert on the team, and 2) GraphQL client libraries like Apollo can be complicated and introduce their own caching systems, leading to potential bugs.

  • When should you choose REST APIs over GraphQL?

    -You should choose REST APIs if you are a startup that needs to iterate quickly and develop easily, without much time to spare. REST APIs are generally easier to build than GraphQL APIs.

  • What is tRPC and how does it differ from REST and GraphQL?

    -tRPC stands for Typed Remote Procedure Call and adds TypeScript support. It brings the backend and frontend much closer together, especially when using TypeScript on both ends. The client can directly call backend controllers as if it were one application, with TypeScript defining the schema for both ends.

  • What are the advantages of using tRPC?

    -The advantages of tRPC include: 1) Strong typing with TypeScript, 2) Running on HTTP/2 with optimizations, 3) Lightweight compared to GraphQL, 4) IDE support for navigating between client and server code, and 5) Lower bundle size without bulky packages.

  • When should you choose GraphQL over tRPC?

    -You should choose GraphQL over tRPC if you have nested and complex data representations, or if you care about bandwidth and need to request only specific data from the API.

  • What is gRPC and how does it differ from tRPC?

    -gRPC stands for Google Remote Procedure Call. Like tRPC, it glues the client and server together, allowing the client to invoke server functions as if it were one application. However, gRPC works with its own binary format (Protocol Buffers) instead of JSON and is designed for communication between microservices, potentially written in different languages.

  • When should you use gRPC?

    -You should use gRPC if: 1) You care about low bandwidth and optimized communication between microservices, 2) Your microservices are written in different languages, 3) You need real-time streaming capabilities, or 4) You require optimized communication between microservices.

  • What is the current maturity level of gRPC compared to other technologies mentioned?

    -According to the script, gRPC is not as mature as one would expect at the time of the video, but it may improve in the next two or three years.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now