How did we reduce Memcached memory usage in PHP at Wingie — Enuygun

Doğukan Akkaya
Wingie / Enuygun Tech
3 min readJan 31, 2022

Firstly, for those of you who don’t know what Memcached is, Memcached is a high-performance, in-memory, key-value data store.

Here at Enuygun Flight besides many other data, we were basically storing user’s Search Result in Memcached, so each search request (if it has the same parameters) didn’t have to re-fetch the results from apis, just bring them from Memcached for fast response to our frontend.

Why did we tried to reduce Memcached usage?

Since Enuygun and Wingie attracts more and more visitors every day and creating new features that use memory, our memory usage is increased. Of course you can solve this problem by adding RAMs to your physical machine or buy more usage from cloud machines. Since RAMs in physical machines are not infinite and cloud has other costs. We thought that we can achieve this within our application.

What did we do?

We started to do some research that how we can achieve this or if we can compress the data. We have found that PHP’s Memcached extension was serializing the data. That means it serializes the all PHP Class with properties, class name etc. Even if you change your Class name it increases or decreases (depends on your class name) the bytes written in Memcached.

The value can be any valid PHP type except for resources, because those cannot be represented in a serialized form.

As PHP’s Memcached offers us some options like changing the serializer which was important for us as i mentioned above. We could not use these options because our server structure was not appropriate for this kinda usage since other teams and their application’s code were using the same Memcached service. If we would have changed it, they probably would get effected from this change. Memcached also allows you to set this serializer option in the runtime.

We decided to set data to Memcached as JSON encoded to reduce the size. For this we used a library called JMS serializer, we were already using that library but not for that purpose. JMS serializer allows you to (de-)serialize data of any complexity. (https://github.com/schmittjoh/serializer) We defined our Type Annotations to our Schemas that we set to Memcached like:

After this once you call JMS Serializer’s serialize function with json format, you get a json representation of this class and once you deserialize, it returns you Result Schema. If we would not use such a library we would have to call setters to our Schema’s properties and set each of them with correct data types.

But that was not enough for us, we wanted to make our data smaller. So we also used gzcompress and gzuncompress functions of PHP to compress the json representation of our data like:

Object PHP serialization vs JSON converted object PHP serialization

PHP's serialize"O:6:"Result":2:{s:17:"�Result�requestId";s:8:"1234qwer";s:15:"�Result�flights";a:2:{i:0;O:6:"Flight":2:{s:21:"�Flight�departureDate";s:10:"2022-07-24";s:18:"�Flight�returnDate";s:10:"2022-08-24";}i:1;O:6:"Flight":2:{s:21:"�Flight�departureDate";s:10:"2022-07-24";s:18:"�Flight�returnDate";s:10:"2022-08-24";}}}"
JSON{"requestId":"1234qwer","flights":[{"departureDate":"2022-07-24","returnDate":"2022-08-24"},{"departureDate":"2022-07-24","returnDate":"2022-08-24"}]}
PHP's serialize with JSONs:150:"{"requestId":"1234qwer","flights":[{"departureDate":"2022-07-24","returnDate":"2022-08-24"},{"departureDate":"2022-07-24","returnDate":"2022-08-24"}]}";
PHP's serialize with gzcompressed JSONs:88:"x��V*J-,M-.�LQ�R2426),O-R�QJ��L�()V���VJI-H,*)-JuI,I*3202�50�52*,J���X�dju��[ ���,O";

Results

Remember the chart that i showed you at the beginning? Let’s take a look at that chart again to see if we have achieved to decrease the memory usage?

If you want to join our team, share your CV with us: kariyer@enuygun.com

--

--