christova

APIs

First let me explain what is anย APIย in simple terms

Imagine you're a customer (an application or program) sitting at a table (a device), and you want to order food (request data or services). Instead of going into the kitchen (the system or server) yourself, you interact with the waiter (API), who takes your order and communicates it to the kitchen on your behalf. The kitchen then prepares the food (performs the requested task), and the waiter brings it back to you.

Similarly, an API allows different software applications to communicate and share information or perform specific tasks without knowing the intricate details of each other's internal workings. It acts as a bridge, enabling applications to request and exchange data or functionalities in a standardized way.


Ok now lets explore 6 common ways to build APIs.

  1. REST: Architecture using standard HTTP methods for CRUD operations on resources.

  2. GraphQL: Query language enabling clients to request specific data structures.

  3. WebSocket: Full-duplex communication protocol for real-time applications.

  4. gRPC: High-performance RPC framework for efficient communication between services.

  5. MQTT: Lightweight messaging protocol for low-bandwidth, high-latency networks, common in IoT.

  6. Serverless: Cloud computing model where developers focus on code, and the provider manages infrastructure.

#APIs

Improving API Performance with Database Connection Pooling

The diagram below shows 5 common API optimization techniques. Today, Iโ€™ll focus on number 5, connection pooling. It is not as trivial to implement as it sounds for some languages.

When fulfilling API requests, we often need to query the database. Opening a new connection for every API call adds overhead. Connection pooling helps avoid this penalty by reusing connections.

๐—›๐—ผ๐˜„ ๐—–๐—ผ๐—ป๐—ป๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ฃ๐—ผ๐—ผ๐—น๐—ถ๐—ป๐—ด ๐—ช๐—ผ๐—ฟ๐—ธ๐˜€

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.

๐—–๐—ต๐—ฎ๐—น๐—น๐—ฒ๐—ป๐—ด๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐—ฆ๐—ผ๐—บ๐—ฒ ๐—Ÿ๐—ฎ๐—ป๐—ด๐˜‚๐—ฎ๐—ด๐—ฒ๐˜€

However, setting up connection pooling can be more complex for languages like PHP, Python and Node.js. These languages handle scale by having multiple processes, each serving a subset of requests.

- In these languages, database connections get tied to each ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€. - Connections can't be efficiently shared across processes. Each process needs its own pool, wasting resources.

In contrast, languages like Java and Go use threads within a single process to handle requests. Connections are bound at the application level, allowing easy sharing of a centralized pool.

๐—–๐—ผ๐—ป๐—ป๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ฃ๐—ผ๐—ผ๐—น๐—ถ๐—ป๐—ด ๐—ฆ๐—ผ๐—น๐˜‚๐˜๐—ถ๐—ผ๐—ป

Tools like PgBouncer work around these challenges by ๐—ฝ๐—ฟ๐—ผ๐˜…๐˜†๐—ถ๐—ป๐—ด ๐—ฐ๐—ผ๐—ป๐—ป๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€ 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 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.

Connection pooling improves efficiency, but its implementation complexity varies across languages.

#APIs