Handling communication between the server and the client can be one of the most frustrating tasks in web development. Ensuring harmony and security between requests and responses is not a walk in the park. Nonetheless, it all changed when REST came to the API requests world as a life savior. The developer community loves its simplicity and similarities with Object Oriented Programming (OOP).
REST has been the OG protocol to handle server-to-client communication for a while. Yet, a newer approach is killing it regarding API safety and communication between the front and backend team. I’m talking about tRPC. Some companies like Netflix, Invisible, Cal, Pleo, Newfront, and Dr. B already consider it a common choice. tRPC is probably the most efficient way to work with APIs (at least, for now). But does that mean you should migrate from REST? Is it the best option for every case? In this post, we cover all the necessary aspects of REST and tRPC so you can decide which one is best for you!
What is REST API Communication?
Everyone already knows and loves it. It's the most popular API protocol on the planet, and most APIs you hear about are surely RESTful. So, I thought it was the right thing to start with it. REST became a game-changer, quickly overtaking SOAP because of its ease of use, flexibility, and scalability. REST handles communication through HTTP status codes (GET, POST, PUT, and DELETE) using specific URIs represented by URLs.
The best part is that it allows JSON or XML to improve communication. This way, it became the standard way to develop Web Applications. There are, however, some issues with REST that you should be aware of. We'll look into them, but before that, let's review the perks of REST that continue to be relevant despite having more modern alternatives like tRPC procedure (and also GraphQL).
Pros of REST APIs
1. REST APIs Flexibility
Being the most popular API protocol, it has a vast pool of learning resources and a wide range of technologies and programming languages that support it. That includes JavaScript, Python, Java, Ruby, Kotlin, C#, and more. As mentioned, REST supports JSON and XML messaging formats, one of the most popular data formats. On top of that, it also supports HTML, YAML, and plain text format.
2. REST APIs Popularity
Everyone knows about REST, and most people love working with it. Since so many teams use it, you can be 100% sure you’ll come across it. Thus, REST is still necessary for anyone wanting to enter web and mobile development. You can use it with popular cross-platform frameworks like React Native and Flutter. They have useful built-in functions and third-party libraries that allow you to fetch data from the backend with ease.
3. REST APIs Scalability
Scalability is another big reason why REST became so popular. Its simple and lightweight architecture makes it relatively easy to scale larger applications. It involves several principles that make RESTful APIs highly scalable, such as catching, which allows for high performance, and statelessness, which enables horizontal scaling.
4. REST APIs Simplicity
REST principles are easy to understand and implement. It follows standard HTTP methods that promote discoverability, providing a uniform and consistent interface. That results in a familiar ecosystem for developers with previous experience in HTTP methods and makes it easy for clients to consume RESTful services.
Cons of REST APIs
1. REST APIs Over/Under-Fetching
The way REST structures and retrieves data often causes over-fetching or under-fetching issues. That can, of course, affect performance. Let me explain. RESTful APIs tend to retrieve a fixed amount of data for every request. Imagine you have a big request object like a user with multiple values (name, age, address, profession), and you want to fetch one or two values.
The request will still fetch all the other values causing an over-fetching that leads to inefficiency. It can also happen that the client needs more data than the fixed request allows. That translates to more requests to get the desired data leading to the load on the server and increased latency.
2. REST APIs Endpoint Duplication
Another common problem with RESTful APIs is endpoint duplication, especially if the endpoints’ purposes are similar or overlap. A classic example is an eCommerce app with a resource for products and another for categories. The API may require retrieving products by category and categories by product. Multiple clients (like mobile apps and browsers) may also require the same API endpoints that adapt to their needs. Endpoint duplication causes redundancies and the inefficient use of resources.
3. REST APIs Team Communication
If separate teams work on the front and back end using REST, communication issues can arise, leading to delays and inefficiencies. REST forces the front and back end to stay in constant and active communication to avoid unclear API contracts, inconsistent naming conventions, over-fetching, and under-fetching.
What is tRPC API Communication?
Now it’s time to talk about the big game-changer, often called the future of client-to-server communication. tRPC stands for TypeScript Remote Procedure Call. It inherits the principles of its old-school relative (RPC) but takes them to a new level. RPC is a protocol developed by Bruce Jay Nelson back in 1981, allowing programs to communicate with each other by implementing procedures or functions.
tRPC is a cutting-edge client-to-service communication protocol released in 2021. It provides a more modern and streamlined approach to RPC-style communication, including performance optimizations to minimize network overhead.
In simple terms, tRPC uses TypeScript functions or methods to handle client and server communication. This new approach allows you to focus solely on the logic while it takes care of communication using TypeScript. tRPC has strong IDE support, meaning it will detect changes on any side of the app (client or server) in compile-time.
Pros of tRPC APIs
1. tRPC APIs Safely-Typed
Type safety and autocomplete between the client and the server is h*ll of an accomplishment that makes a ton of frustrations disappear. tRPC uses TypeScript to check your routes, variables, and functions to ensure all their names match. This way, you can fix errors timely and easily so they don’t reach the end user. That results in a pleasant user and developer experience. You can also add seamless data validation to your project using ZOD, which is highly advisable if you work with React.
2. tRPC APIs No Under/Over-Fetching
As mentioned above, over-fetching and under-fetching are common issues with RESTful APIs. Like GraphQL, tRPC allows you to use TypeScript to define and get only the data you need avoiding bloated responses and duplicate requests. It enables you to do this without learning a different language or framework.
3. tRPC APIs Tooling
tRPC allows for easy integrations with some of the most popular developer tools in the JavaScript ecosystem, including React.js and Next.js. That reduces the learning curve of this new approach to APIs allowing developers to build a world-class product with a single language.
4. tRPC APIs Communication
Instead of requiring devs to construct and parse HTTP requests manually, tRPC provides a standardized way to define and call remote methods as local functions. It can improve communication between teams, boosting productivity and efficiency.
Cons of tRPC APIs
1. tRPC APIs Flexibility
You can only implement tRPC using TypeScript. While it’s true that TypeScript is among the most popular programming languages in the world, some developers prefer to work with other languages. Even some JavaScript developers may not want to dedicate time to learning it. On the other hand, REST is a much more mature API protocol that you can implement with almost any language (including TypeScript).
2. tRPC APIs Monorepo
To get the most out of tRPC, you must keep your project in a single file. In other words, a monorepo. That can be attractive for small projects, making it easy to maintain consistency on the client and server sides. However, managing and organizing the code in large projects where many developers collaborate can be complex. In this manner, ensuring the code is reusable for more clients can also be difficult.
3. tRPC APIs Adoption
As mentioned, REST is the standardized way for developers to handle client-to-server communication. Given its popularity and the fact that most of the APIs in the world are RESTful, REST will have a significant priority over tRPC for a while. Yet, tRPC is only two years old, and what it has achieved so far is breathtaking.
Conclusion
tRPC is an elegant and modern protocol to easily handle faster communication between the server and client. It has allowed us to build top-notch modern applications using a unique programming language, TypeScript. That said, the tRPC protocol blatantly surpasses REST for projects already using TypeScript both in the front and the backend.
Similarly, it may be a popular choice for projects involving JavaScript tools like React.js, Next.js, or Express. If the project has existing GraphQL and REST APIs, adding tRPC for new requirements could also simplify things for developers if they know TypeScript. However, migrating your existing APIs might not be practical for development time despite having performance benefits over REST.