REST best practices

Nadia Ahmadian
3 min readMar 20, 2023

Creating REST API is not a such hard thing to do but before making it you got to think about the architecture in order to make it reliable and perform well under loads so in this article I’m gonna go to through some practices which doesn’t hurt to know.

KISS

KISS doesn’t mean 😘 but it stands for “Keep it simple stupid”. As obvious as it is it means that you should keep things simple by assigning the API with one specific job and not too many unrelated tasks.

Pagination

one of the best practices is to provide large result sets with filters of different types. with pagination, you are able to deliver results in smaller chunks. For example in the apple store you just want to see macBooks or iPhones and not all of the products all at once so your API is in charge for doing so and you can accept such filters as query string parameters to retrieve only macBooks or only iPhones. When building When building an API, it is always a good idea to enable it to send results in smaller chunks. This way, if the client application wants to display 5 items per page, it will not need to make an API call for thousands of items all at once. By using pagination, you can deliver API results in chunks, and clients can decide the page number and how many records they want per page.

Versioning

You might break all of your client’s application if you make a single change to your API and we wouldn’t want that. There are so many things to keep in mind in order to decide whether an updated version is needed. BUT you should only support two versions of any given resource, because maintaining multiple versions can be complex, error prone, and costly.

Caching

API must be cacheable in order to reduce the load on your database-related API calls.You should always implement cacheing and send relevant HTTP headers in your response. This will minimize the number of calls your client makes to your API. For example, if the mobile app makes a call to the endpoint iphone-items, you can cache the results the first time it runs, and then serve the cached result every time after that. This way, you can avoid putting a heavy load on your database every time you need to fetch iPhone-items. You will only update the cache when a iPhone item is modified or added. And that saves a lot of computing power.

Rate limiting and Monitoring

If you want to prevent abuse of your APIs, try rate limiting. This limits the number of times someone can call your API in a period of time, like per minute, hour, or day.

You should monitor latency to make sure your clients are getting the best possible response time. If you are looking to get the most out of your API, it is essential to also monitor status codes. By keeping an eye on 400 to 499 and 500 to 599 codes, you can assure that your API runs smoothly and identify any potential problems early on.

Monitoring network bandwidth is also important to know if someone is abusing your API.

Conclusion

It’s important to keep your API healthy, performant, and sustainable. These included keeping things simple, filtering, ordering and pagination,versioning, as well as cacheing, and rate limiting and monitoring. By keeping to these best practices and standard rules for creating sustainable APIs, you’ll be designing great APIs in no time.

--

--

Nadia Ahmadian

a crazy gemini who believes in magic, aliens and impossible dreams and converts coffee into iOS apps and soon web apps