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!
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).
What are REST Advantages?
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.
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.
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.
What are REST Disadvantages?
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. 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. 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.
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.
What are tRPC Advantages?
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. 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.
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.
What are tRPC Disadvantages?
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.
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.
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.