How to improve API performance.

Ian CarsonIan Carson
2 min read

API performance is critical for any infrastructure or any other system. Think of slow API performance like when you try to check the items in your cart and it is taking like forever to update or for the page to display. This is a horrible situation and the chances of the users abandoning the website or the application altogether are pretty high.

The below image shows some optimization tricks.

How to Improve API Performance

Today, I will focus on Connection Pooling.

Connection pooling overview.

When completing the API requests, often a database query is needed. Opening a new database connection every time will result in huge overhead costs. Connection pooling solves this by reusing previously available connections.

Below is how Connection pooling works.

  1. For each API server, establish a pool of database connections at startup.

  2. Workers share these connections, requesting one when needed and returning it after.

Challenges for some Languages.

However, connection pooling can be tricky for languages such as PHP, Python, and Node.js which scale by spawning multiple processes.

  • In these languages, database connections are tied to each process.

  • Connections can't be efficiently shared across processes. Each process needs its pool, wasting resources.

In contrast, languages such as Java and Go use threading to handle requests within a single process.

  • Connections are bound to the application level, allowing easy sharing of a centralized pool.

Connection Pooling Solution.

There are existing tools such as PgBouncer that provide a walk-around in these challenges. PgBouncer uses Proxying connections at the application level.

PgBouncer creates a centralized pool that all processes can access. No matter which process makes the request, PgBouncer efficiently handles the pooling. At a high scale, all languages can benefit from running PgBouncer on a dedicated server. Now the connection pool is shared over the network for all API servers. This conserves finite database connections.

Database connection pooling enhances efficiency, but its implementation complexity varies across languages.

Major Takeaways.

  • Connection pooling improves efficiency by reusing database connections.

  • However, implementation complexity varies by language architecture.

  • Tools such as PgBouncer enable central pooling for languages with separate processes.

Thank you for your time and see you in the next.

0
Subscribe to my newsletter

Read articles from Ian Carson directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ian Carson
Ian Carson

A Disciplined, keen on details, and curious problem solver. I read and code a lot. I believe in Teamwork, Accountability, Transparency, and Competency.