Unleash / unleash-client-php

Unleash client SDK for PHP
MIT License
55 stars 14 forks source link

Feat: Compact metrics #198

Closed RikudouSage closed 9 months ago

RikudouSage commented 10 months ago

Description

Instead of serializing the whole object to cache, a simple (and small) string which can be reconstructed to the metrics bucket object is serialized instead. Should massively lower the size of the metrics in the cache storage.

Fixes #197

Type of change

How Has This Been Tested?

Checklist:

RikudouSage commented 10 months ago

@vbartusevicius Does this work for you regarding #197?

RikudouSage commented 10 months ago

As another alternative, consider using a different cache adapter for metrics, for example APCu - metrics are non-critical, so if something gets lost because of a restarting process, nothing really important should be lost (or you can implement a procedure where the metrics bucket gets flushed to server in case of shutdown).

By using APCu you would cut the network overhead of Redis and it's not important to have a shared cache for metrics in particular. Alternatively take a look at all the Symfony adapters.

You can use something like this (if you use symfony/cache):

<?php

use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Psr16Cache;
use Unleash\Client\UnleashBuilder;

$unleash = (new UnleashBuilder())
    ->withMetricsCacheHandler(new Psr16Cache(new ApcuAdapter()))
    // your other builder options
    ->build()
;
RikudouSage commented 9 months ago

Some quick tests I did today:

=== Toggle amount: 1 === Old serialization size: 1,538 bytes New serialization size: 31 bytes Saving: 97.98%

=== Toggle amount: 10 === Old serialization size: 11,016 bytes New serialization size: 121 bytes Saving: 98.90%

=== Toggle amount: 100 === Old serialization size: 105,967 bytes New serialization size: 1,111 bytes Saving: 98.95%

=== Toggle amount: 1,000 === Old serialization size: 1,057,268 bytes New serialization size: 11,911 bytes Saving: 98.87%

=== Toggle amount: 10,000 === Old serialization size: 10,588,269 bytes New serialization size: 128,911 bytes Saving: 98.78%

=== Toggle amount: 100,000 === Old serialization size: 106,078,270 bytes New serialization size: 1,388,911 bytes Saving: 98.69%

vbartusevicius commented 8 months ago

Thanks, @RikudouSage - these size savings are amazing. We are patiently waiting for the release, including this improvement!

RikudouSage commented 8 months ago

@vbartusevicius It was already released a while ago, I just forgot to do the release on GitHub, but composer should have picked up the new version already.