Is Your Query Cache Really Disabled?

Is Your Query Cache Really Disabled?

PREVIOUS POST
NEXT POST

This blog post was motivated by an internal discussion about how to fully disable query cache in MySQL.

According to the manual, we should be able to disable “Query Cache” on the fly by changing query_cache_type to 0, but as we will show this is not fully true. This blog will show you how to properly disable “query cache,” and how common practices might not be as good as we think.

Can we just disable it by changing variables, or does it requires a restart to avoid the global mutex? Let’s see how it works.

Some Query Cache context

The query cache stores the text of a “Select” statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.

But cacheable queries take out an “exclusive lock” on MySQL’s query cache. In addition, any insert, update, delete or other modifications to a table causes any relevant entries in the query cache to be flushed. If you see many “Waiting for query cache lock” in the processlist, you might be suffering from this exclusive lock. In this blog post, you can see how this global mutex in high concurrency can cause performance degradation.

If we are facing with this situation, how can we disable it?

Disabling Query Cache

There are two options that you can change: query_cache_type and query_cache_size.

So if we change query_cache_size to “0”, does it means the cache is disabled? Or we also have to change query_cache_type? Or both? And does MySQL require a restart to avoid the global mutex?

The source code shows us this:

MySQL is going to check if the query cache is enabled before it locks it. It is checking four conditions, and one of them has to be true. The last three could be obvious, but what is the “is_disabled()” function? Following the source code, we can find the next: sql_cache.h

sql_cache.cc

If the global_system_variables.query_cache_type == 0 condition is true it is going to call the  disable_query_cache  function, which sets m_query_cache_is_disabled = True, so is_disabled going to be “True”. That means if we are setting query_cache_type to 0 in runtime, that should eliminate the global mutex. Let’s run some tests to confirm this and see if the global mutex disappears after changing query_cache_type to 0.

Running tests

Context on the tests:

  1. We ran simple OLTP tests using sysbench as follows:

  1. Important portion of my.cnf file:

Disable the Query Cache

So basically the tests were run for two minutes each while playing with query_cache_type and query_cache_size.

  1. Started MySQL with query_cache_type = 1 and query_cache_size=1G.
  2. Change query_cache_type to 0. As we can see nothing changed, MySQL is still using the query cache.
  3. But when we stopped sysbench and started again (closing and opening new connections), we can see there are no more inserts going into query cache. But we still can see the queries like “Not Cached” that means changing the query_cache_type applies only for the new connections, and we still can see some mutex.
  4. Restarted MySQL with query_cache_type = 0 and query_cache_size=0. Finally we disabled the query cache and all the mutex is disappeared.
  5. Restarted MySQL with query cache enabled.
  6. We changed query_cache_size=0 and it almost worked, we could disable query cache on the fly, but as we can see there is still some mutex activity.
  7. Changing query_cache_type=0 and restarting sysbench does not have any effect on the mutex.

So the only way to stop any activity around query cache requires restarting MySQL with query_cache_type = 0  and query_cache_size=0. Disabling it or even set it to “0” on runtime is not completely stopping mutex activity.

But why do we still need query_cache_size while in theory query_cache_type should be enough?

As referenced above, the manual says if query_cache_type = 0:

Do not cache results in or retrieve results from the query cache. Note that this does not deallocate the query cache buffer. To do that, you should set query_cache_size to 0.

Based on our test, if we change query_cache_type to 0, it still hits the cache.

So you might think “well, I don’t enable the query cache and use defaults to keep it disabled.” Keep reading, because you might be wrong. According to manual, starting from 5.6.8 query_cache_type=0 is set by default, but query_cache_size= 1048576  (1MB). This means that if we keep default configuration, we will still see activity in the query cache as follows:

But if we just add query_cache_size=0  to my.cnf and check again (of course after restarting server):

We finally get no query cache related activity at all. How much overhead is caused by this? We’re not fully sure because we didn’t perform benchmarks, but we like to see no activity when we don’t want to.
Now we’re wondering if this case requires a bug report. Stay tuned, we will publish results in the post soon.

Digging more code

Let’s have a look on store_query function. MySQL uses this function to store queries in the query cache. If we read the code we can find this:

It only checks the query_cache_size, it does not check the type. Store_query is called in handle_query, which also does not check the query_chache_type.

Conclusion

There is some contradiction between checking the query cache and storing the data in the query cache, which needs further investigation. But as we can see it is not possible to fully disable the query cache on the fly by changing query_cache_type  or/and query_cache_size to 0. Based on the code and the tests, if you want to make sure the query cache is fully disabled, change query_cache_size and query_cache_type to 0 and restart MySQL.

Is a known fact that query cache can be a big point of contention, and we are not trying to benchmark the performance overhead since this mostly depends on the workload type. However, we still can see some overhead if the query cache is not fully disabled when MySQL is started.

PREVIOUS POST
NEXT POST

Share this post

Comments (9)

  • Mike Reply

    Great post! It would be great to see a similar post for AHI

    November 11, 2016 at 6:06 pm
  • Daniel Reply

    Which version of MySQL is the source code you guys reviewed?

    November 14, 2016 at 3:20 pm
    • Tibor Korocz Reply

      Hi Daniel,

      I checked 5.6,5.7 but the links and copies are from 5.7.

      November 14, 2016 at 3:27 pm
  • Csaba Balázs Reply

    Great post! Thx!

    November 14, 2016 at 3:24 pm
  • Derek Downey Reply

    You show the qcache activity if MySQL started with the defaults, but the graph of mutex contention doesn’t have a section for starting with defaults (query_cache_type=0, query_cache_size=1M).

    If the defaults result in mutex contention, it definitely warrants a bug, since the documentation states: “By default, the query cache is disabled. This is achieved using a default size of 1M, with a default for query_cache_type of 0.” (src: http://dev.mysql.com/doc/refman/5.7/en/query-cache-configuration.html)

    Also some discussion from 2013 in this bug: https://bugs.mysql.com/bug.php?id=65336 Last comment: “If the server is started with query_cache_type set to 0, it does not acquire the query cache mutex at all,…*snip*”

    November 14, 2016 at 4:18 pm
    • Francisco Bordenave Reply

      Derek, you’re right I didn’t add that test to graphic so I ran it a quick test and I can’t see any activity reported in performance_schema (where Grafana reads data from) but I still see status counters increasing. As mentioned we didn’t check the overhead and performance impact but IMO manual is not fully clear about how defaults works because the only way we stopped any activity (even performance counters) was by setting both variables to 0.

      November 15, 2016 at 7:45 am
  • raja Reply

    HI Tibor,

    How can I draw graphs using metrics.sh output scripts ?

    November 24, 2016 at 5:55 am
  • Arjen Lentz Reply

    Hi –

    In your profiling test, you don’t have a step where you set both the size and type to 0 while not restarting. According to the source code flow, that should be sufficient, however you don’t test this.
    Your step 4 should have been split, first setting both to 0 and checking the mutex, THEN restarting (if the mutex was still occurring).
    As it is now, you don’t have that data. Therefore, claim that the server has to be restarted for the mutex to disappear is not warranted by the evidence provided. It’s one step away.

    We know the query cache is being deprecated, but currently people are still referring to resources regarding the query cache so it’s possibly worth updating the post.

    January 31, 2018 at 7:52 pm

Leave a Reply