Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's faster: Memcached or MySQL in memory table like HEAP?

If I have a pretty static set of data that I want to be able to access as quickly as possible. Should I cache the data into Memcached or should I store it in a HEAP table or something inside MySQL? Would one scale better than the other?

Is there some other option that's even faster?

like image 784
Brent Avatar asked Jun 09 '09 23:06

Brent


People also ask

How fast is Memcached?

memcached can process over 50 million keys per second on a 48 core machine using only RAM and heavy batching.

Is Memcached in-memory?

Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds. Memcached is also distributed, meaning that it is easy to scale out by adding new nodes.

How long does Memcached keep data?

The expiration time in Memcached is in seconds. For instance, the default value is 10800 seconds. But, it can have a maximum value of 2592000 seconds that is, 30 days.

Is Memcached relational database?

Key Difference – Memcached vs Redis A relational database is a common database type, but it is not suitable for storing a large quantity of data. Therefore, NoSQL was introduced. It stands for a non-relational or non-SQL. Memcached and Redis are categorized as NoSQL.


2 Answers

The fastest option would be in-memory caching on the local system. That won't scale well to many millions of relations, but will be very fast and work well for small data sets.

I haven't done performance testing between Memcached/MySQL HEAP, but I'd guess Memcached would be faster because it doesn't have the overhead of a full relational DB engine. Memcached would almost certainly scale better, because you could distribute it between servers and have a round-robin request dispatch between them.

If you need to perform any filtering on the data before retrieving it, you should use MySQL. The performance overhead of transmitting unwanted data will probably outweigh the benefits of faster lookups.

If I were you, I'd load the data set in question into MySQL and Memcached, then run performance tests to see which is better for your data set. If there's a core of data that's accessed particularly often, consider an additional machine-local cache.

like image 195
John Millikin Avatar answered Sep 21 '22 23:09

John Millikin


memcached will be faster for simple uses, hands down -- connection setup is so much cheaper on memcached, since there's no auth, buffer allocation, etc. Also, memcached is designed to easily distribute keys between multiple servers.

However, memcached is only a simple key/value store. If you need to do anything more complex to your data (even something like SELECT * WHERE x > 5), a HEAP table is much more powerful.

Robert Munteanu brings up a good point though. Your cache hierarchy should be:

  1. Globals (local to the request/process)
  2. APC (local to the server)
  3. memcache (global)

If you don't need to propagate global changes to this data, then storing it in APC makes sense. If you need to access it several times during script execution, you should also cache it in globals in your script.

like image 41
Frank Farmer Avatar answered Sep 17 '22 23:09

Frank Farmer