Otar Chekurishvili

July 26, 2022

Strategies for Reducing Big Redis Traffic in Laravel

Most of the project generally start using Redis in a simple way, let's say as a user session storage, then delegate many important things to it along the way: caching, pub/sub, queues, etc... This way Redis becomes an important piece of your project ecosystem.

As much as you might love and admire Redis, it may become a bottleneck at some point: Redis network traffic might hit GiB/s and might slow down your entire system.

Screenshot 2022-07-26 at 15.08.03.png


Thankfully, Redis has batteries included that can dramatically reduce the network traffic: data serialisation and compression. Redis by default does not serialise or compress the values that are stored in it, so it is up to you to enable these features. Since Laravel 8 these options are provided in the database.php configuration file as well.


Serialisation

igbinary is an algorithm that replaces default PHP serialiser and is much, much faster... You can see the benchmarks here: https://github.com/igbinary/igbinary/blob/master/benchmark/comparisons.php.

If you switch to this method, serialisation and deserialisation of the data before it is stored in Redis or after it is being retrieved from it becomes much faster.

Caveat: if you intend to use the same Redis store from the technologies other than PHP, I would use msgpack or JSON serialiser for data consistency between the different technologies. 


Compression

Compressing the serialised data means that the payload sent over the network will be reduced in size, hence reduce the network traffic.

I prefer to use LZ4 compression because it's a perfect balance between the compression efficiency and the compression speed. There are other supported compression tools too: LZF, Zstandard, etc... 


Laravel

For Laravel projects, if you use PHP Redis extension instead of predis library (which you should for large scale projects), framework supports additional configuration options... Below is an example how you can enable igbinary serialisation and LZ4 compression for Laravel projects.
database.php (1).png

P.S.

Certain Redis features need to be enabled during the extension installation/building, check out the official PHP Redis extension documentation for the instructions: https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown

About Otar Chekurishvili

Internet Citizen. Software & Wine Craftsman. Digital Entrepreneur. https://otar.me