Category: Development

Database-efficient API pagination

When designing APIs, you will probably need to handle a way to paginate the results in a collection.

Depending on the database you are using, the first thing that could come to your mind could be to use your database limit and offset to paginate the results. This may be tempting, but sometimes it could be better to rely on something else.

Imagine you have a list of messages in a chat application, and for the first call, you show the first 15 messages. Now you want to get the next page of results, say other 15 messages past the ones you already have, so you do LIMIT 15 OFFSET 15 in your database. Cool right?

(more…)

Common database paradigms

From the simplest to the more advanced, here is a list of different database paradigms with an introduction and some use cases for each one.

Use this index if you want to navigate directly to a specific one.

  1. Key-value
  2. Wide column
  3. Document
  4. Relational
  5. Graph
  6. Search
(more…)

Making concurrent HTTP requests in PHP

This is part of a new series of articles aimed at giving instant responses to common questions. Find out more.


All the snippets make 10 requests to a fake API that takes 3 seconds to reply.

On my computer, multiple executions of these scripts ran in a little more than 3 seconds.

You can configure how many simultaneous connections to make in each client.

Using Symfony’s HTTPClient

Installation

$ composer require symfony/http-client

Example

$client = new \Symfony\Component\HttpClient\CurlHttpClient([], 10);
$responses = [];

for ($i = 0; $i < 10; ++$i) {
    $responses[] = $client->request('GET', 'http://slowwly.robertomurray.co.uk/delay/3000/url/https://jsonplaceholder.typicode.com/todos/1');
}

foreach ($responses as $response) {
    $content = $response->getContent();
}

Using Guzzle

Installation

$ composer require guzzlehttp/guzzle:^7.0

Example

$client = new \GuzzleHttp\Client();
$responses = [];

$generator = function ($total) use ($client) {
    for ($i = 0; $i < $total; $i++) {
        yield function() use ($client) {
            return $client->getAsync('http://slowwly.robertomurray.co.uk/delay/3000/url/https://jsonplaceholder.typicode.com/todos/1');
        };
    }
};

$pool = new \GuzzleHttp\Pool($client, $generator(10), [
    'concurrency' => 10,
    'fulfilled' => function (\GuzzleHttp\Psr7\Response $response, $index) use (&$responses) {
        $responses[$index] = $response->getBody()->getContents();
    },
]);

$promise = $pool->promise();

$promise->wait();

Any comments? Send me a tweet. 🐸