📜 ⬆️ ⬇️

Let's play in CacheGraph?

image Suppose you have a website. Suppose you optimized it for the most "can not", but still wants more.
Personally, I wanted to, but for a long time did not work.
When I defeated the inhibition of the database by caching requests, the inhibition of blocks was done by caching blocks, and I could not cache entire pages ...
The site of course began to work much faster, but it began to slow down already ... memkeshed?


How many database queries did you have?


Probably after the start of using query caches was not enough.
And how many requests do you have to caches?
The following applies only to systems using multiple memcached servers. If you do not have memory, or one server - the effect will be minimal.

A bit of background


As philosophers say, everything develops in a spiral.
Three years ago I was an ordinary game developer in a small studio, I sat for optimization 70% of the time catching delays with VTun.
As you know - one of the main problems of video game development is DIP minimization. If in Russian, you are rendering something on a video card, while the processor is waiting for synchronization or a response, in general idle ...
It turned out that to render the monster on 10k triangles and on 1k triangles in time is the same.
There were many different ways and tricks to avoid synchronization, or to reduce delays ...
One of the assistants in this business was SceneGraph - the rendering tree.
The essence is simple - whenever possible we render similar objects together in order to change fewer different variables between rendering.
')
Three years have passed, I am sitting on a habr and reading how Facebook has been copying my memobook ...
Suddenly, the eyes catch on the expression ... and implement an application
Here I was hooked ...

How do template engines usually work?
They take a pattern and ON THE QUEUE they replace the necessary places with the values ​​that were previously set ON THE QUEUE.

Imagine the following

  <a href='{OBJECT_HREF}'> {OBJECT_LABEL} </a> 

and it is desirable to cycle 100 times ...
We will also assume that we take variables from the cache.
Will this be mmmm 200 requests?
If with different tricky tags (thanks again smira ) - then 400+
If you don’t like the task, and you would put the entire block in the cache - then let's assume that we have 5 such blocks on the page, and one suddenly decided to regenerate ...

Let's make a render of our page, as the game engines love to do - a lot of passing ...
Passage1 - we collect the template, we collect the used keys, we replace their “places” in the template with some anchors ...
Pass 2 - with one bundle (muti-get) we request all keys.
Passage 3 - collect key tags, one pack requested.
Pass 3 - we determine which cache is alive, which is not. For inanimate packs run run1

How it works


We have 5 blocks, 100 elements in each.
In pass 2 we request 5 keys - the values ​​of 5 blocks
In passage3 we request their tags (we will consider them more than five)
we assume that two blocks are expired
Pass 3 starts pass 1 for two blocks. Next will be pass 2 which will immediately request 200 keys from two blocks ...

The result is if all the blocks are “alive” instead of 10 (5 blocks + 5 tags) requests are made 2, 5 in a pack.
If the block cache is dead, we make two requests (100 values ​​+ 100 tags (if any)) instead of 200 ...

But how would this wrap up.
You can wrap it in a normal tree. In the Count. In CacheGraph.
We go around the tree, collect unknown keys, as long as they are there, request a pack and crawl on the tree further.
At the end of a large str_replace of anchors in the template for the values ​​obtained (or another traversal of the tree with the substitution of values, like who you like)

The result is minimization of calls to asynchronous sources, minimization of timeout for synchronization and response. Acceleration of work.
VERY serious acceleration of work in some tasks

Bonus: if the cached items are made standalone, then the tree can be serialized and bypassed for example C ++ ...
In the case of the expiration of the cache lifetime, again with a bundle, you can run php with a request to generate standalone blocks ...

What can I personally say about this?


Went somehow Kashchei Immortal to drown. He rushed into the turbulent river from the highest bridge ... Then under the train ... Then he jumped off the skyscraper ... Then he leaped into the pool with hydrochloric acid ... Then he swallowed cyanide ... He played Kashchei , in general, as he could!

Source: https://habr.com/ru/post/47756/


All Articles