Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: reduce unnecessary API calls #23

Closed
ingria opened this issue Apr 2, 2022 · 1 comment
Closed

Suggestion: reduce unnecessary API calls #23

ingria opened this issue Apr 2, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@ingria
Copy link

ingria commented Apr 2, 2022

Typesense driver makes a lot of unnecessary requests to the server. Real life example: I have a collection with over 10000 documents in it. Each document is updated hourly. Each update changes one model attribute, which triggers 2 request to typesense server. That's 5.5 completely unnecessary requests per second.

20k documents will cause 11 requests per second, 100k documents - 55 requests per second.

I know that typesense server is fast and performant, but still, that requests congest the bandwidth, tcp connections and other resources.

1. Unnecessary updates

At the moment, typesence-driver updates the document on any model update, even if the changed field is not in the schema. This can cause performance problems if there are a lot of updates.

For example:

// MyModel.php

public function toSearchableArray(): array {
    return [
        'id' => (string) $this->id,
        'name' => $this->name,
    ];
}

public function getCollectionSchema(): array {
    return [
        'fields' => [[
            'name' => 'id',
            'type' => 'string',
        ],[
            'name' => 'name',
            'type' => 'string',
        ]],
    ];
}

Now, when I update any field in my model, typesense-driver will try to update the remote index:

MyModel::first()->update([
    'some_field' => 'test',
]);

// typesense-driver sends GET to /collections/my-model
// typesense-driver sends POST to /collections/my-model/documents/import

MyModel::first()->update([
    'some_date' => now(),
]);

// typesense-driver sends GET to /collections/my-model
// typesense-driver sends POST to /collections/my-model/documents/import

My suggestion is to check if the changed fields are in the schema before updating the document. For example, using the getChanges() model method.

The problem that I see here is how to handle computed attributes (ones that use appends array and getter function).

2. Unnecessary collection info retrieval

Upon every update, typesense driver makes a GET request to retrieve a collection info.

I suggest getting that info from schema (getCollectionSchema method).

Metadata

Typsense Version: 5.0

OS: Ubuntu 20.04

@ingria ingria changed the title Reduce unnecessary document updates Suggestion: reduce unnecessary API calls Apr 3, 2022
@AbdullahFaqeir AbdullahFaqeir self-assigned this Apr 19, 2022
@AbdullahFaqeir AbdullahFaqeir added the enhancement New feature or request label Apr 19, 2022
@sergey-laradev
Copy link

@ingria the bug fixed and merged here: #42

Thanks for reporting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants