Skip to content

clients(php): cache host state only on change - #7202

Merged
Fluf22 merged 4 commits into
algolia:mainfrom
zigzagdev:perf/php-cache-host-state-only-on-change
Oct 28, 2026
Merged

Fluf22 merged 4 commits into
algolia:mainfrom
zigzagdev:perf/php-cache-host-state-only-on-change

Conversation

@zigzagdev

Copy link
Copy Markdown
Contributor

Summary

ClusterHosts::resetHost() is called after every successful request and always writes the host state to the cache, even when the host was already up and had no timeouts to clear. This PR only writes the cache when the reset actually changes a host.

🧭 What and Why

When a cache is set with Algolia::setCache(), every successful request runs updateCache(), which serializes the whole ClusterHosts and calls set() on the cache, although in most cases nothing changed.
Depending on the cache, that is one extra network round trip (f.e Redis) or one file write per request.
It also means processes sharing the same cache key keep overwriting each other's host state with writes that carry no change.

Changes included:

  • List changes
    • Host::reset() returns whether the host state changed: it was down, or its retry count was not 0 (a host that timed out but is still up).
    • HostCollection::resetHost() returns whether any matching host changed (array_map replaced by foreach to collect the result).
    • ClusterHosts::resetHost() resets both the read and write collections, then calls updateCache() only when one of them changed.
    • The other callers of Host::reset() ignore the return value, so their behavior does not change.

πŸ§ͺ Test

Plans

Add tests/output/php/src/manual/HostStateCacheTest.php, which swaps in a PSR-16 cache that counts set() like

  • testResetOfAHealthyHostDoesNotWriteTheCache: resetting a host that is up with no retries does not write the cache.
  • testResetOfAChangedHostWritesTheCache: resetting a host that was marked down, or that timed out, still writes the cache.
  • testSuccessfulRequestsOnAHealthyHostDoNotWriteTheCache: successful requests on a healthy host do not write the cache.
  • testSuccessAfterATimeoutWritesTheCacheOnce: after a timeout, the next success on that host writes the cache once, and later successes do not.

Results

$ vendor/bin/phpunit --no-configuration --bootstrap vendor/autoload.php --testdox ../../tests/output/php/src/manual/HostStateCacheTest.php

PHPUnit 10.5.62 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.5.9

....                                                                4 / 4 (100%)

Time: 00:00.037, Memory: 10.00 MB

Host State Cache (Algolia\AlgoliaSearch\Tests\HostStateCache)
 βœ” Reset of a healthy host does not write the cache
 βœ” Reset of a changed host writes the cache
 βœ” Successful requests on a healthy host do not write the cache
 βœ” Success after a timeout writes the cache once

OK (4 tests, 7 assertions)

Notes

ClusterHosts::createFromCache() still calls @unserialize() without allowed_classes, so whoever can write to the cache (for example a shared Redis) can make it create objects of any class.
However, this PR does not change that.
Because I think it will be handled in a separate PR by restricting it to ['allowed_classes' => [ClusterHosts::class, HostCollection::class, Host::class]].

@zigzagdev
zigzagdev requested a review from a team as a code owner October 28, 2026 01:18
@Fluf22 Fluf22 changed the title Perf/php cache host state only on change clients(php): cache host state only on change Oct 28, 2026
@Fluf22
Fluf22 merged commit 75e3099 into algolia:main Oct 28, 2026
14 of 17 checks passed
algolia-api-clients-automation-bot Bot added a commit that referenced this pull request Oct 28, 2026
…ip ci]

Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-csharp that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-dart that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-go that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-java that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-javascript that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-kotlin that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-php that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-python that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-ruby that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-scala that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
algolia-api-clients-automation-bot Bot added a commit to algolia/algoliasearch-client-swift that referenced this pull request Oct 28, 2026
algolia/api-clients-automation#7202

Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com>
Co-authored-by: Masatoshi Ogiwara <msts.oo0131@gmail.com>
@zigzagdev
zigzagdev deleted the perf/php-cache-host-state-only-on-change branch October 28, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants