APIs are common if a not inevitable component in most of the application landscapes. APIs have their own benefits of being an idempotent unit of work independent of the rest of the world. But performance is one of the core expectations from any API being developed. Adding resources like CPU, Memory, and Server may not be the right approach to improve performance and might not help if a few basic principles are not followed while developing the APIs. And there are many commonly used practices for ensuring the APIs respond faster.
Lesser is better – It’s not only true in following a balanced diet, but applies to APIs as well. Always optimize the amount of data being returned by the API. Refactor the APIs if the served payloads are huge – removing unwanted information, providing maximum filter arguments, and building APIs to return smaller units of data should always be given a thought.
Having said that, there are practical scenarios where we cannot avoid big payloads. Using a faster serializer for getting data ready to be sent back is a very effective strategy – many times JSON serializers outperform XML serializers when compared for performance. A faster serializer along with data compressors would definitely optimize the data to be sent over the wire effectively. For larger data responses, it’s always a better idea to implement caching where commonly requested data is readily available and callbacks to data sources are avoided – making APIs return the data faster.
ORM to access database is very easy to use but might impact the performance of data retrieval or updates. Possibly using direct database connections to avoid ORM can also be thought of. In some cases, the performance improvement due to direct data connections might show significant promise performance.
And last but not least – wisely use asynchronous design to process data in parallel whenever possible to reduce the overall processing time. This not only helps in faster responses of the APIs but can also be utilized to server more requests in a given time.
Each of the areas mentioned has lots of resources available over the internet for research and study each area in detail to help you ensure you have covered most of the possible aspects while developing your APIs.
What more aspects do you think should also be accounted for?