Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL is faster than Redis. Am I missing something here? [closed]

Tags:

php

mysql

redis

I've been considering using Redis in a project that uses a lot of writes.

So, after setting it up on my server, I created a simple PHP script using Predis with a simple loop to add records. I also created a second script that does a similar thing, only on a MySQL table (InnoDB) using PHP's MySQLi.

I ran a 10k loop, a 100k loop and a 500k loop and MySQL beat Redis every single time. In fact, the more records I added, the faster MySQL was compared to Redis.

There's so much buzz (hype?) around Redis, that I want to believe I'm missing something here.

Please educate me :)

Thanks!

Here's my Predis code:

for ($i=0; $i<100000; $i++) {
    $predis->set('key'.$i, $i);
}

Here's my MySQLi code:

for ($i=0; $i<100000; $i++) {
    mysqli_query($db, "INSERT INTO test (`key`, `value`) VALUES ('key$i', $i)");
}
like image 274
Ran Aroussi Avatar asked Oct 29 '12 17:10

Ran Aroussi


People also ask

Is Redis faster than MySQL?

In terms of the efficiency of updating databases, Redis is superior to MySQL while SQLite is slowest. However, in terms of the efficiency of querying from databases, SQLite seems to be about ten times faster than Redis and MySQL.

What is the difference between Redis and MySQL?

While MySQL supports the XML data format, Redis does not. When concerning indexes, both allow them. However, MySQL supports secondary indexes without any restrictions while Redis only supports secondary indexes with the RediSearch module.

Can Redis be used with MySQL?

Redis is an open-source and in-memory data structure store that can be used for caching, real-time analytics, searching, and machine learning. Integrate Redis with PHP and MySQL will improve your application performance because Redis stores data in RAM. You can use it with databases like MySQL or MariaDB.

Why Redis is faster?

Replication and persistenceRedis employs a primary-replica architecture and supports asynchronous replication where data can be replicated to multiple replica servers. This provides improved read performance (as requests can be split among the servers) and faster recovery when the primary server experiences an outage.


1 Answers

Comparing predis to mysqli is inappropriate

the mysqli extension - is an extension whereas predis is a php-client library. I.e. whereas mysqli is compiled code, predis is just plain php - extensions are faster.

A benchmark of the kind shown in the question primarily shows the performance loss of PHP code versus an extension.

Compare like with like

If you want to make a comparison of write performance - you'll need to compare to the php redis extension.

like image 114
AD7six Avatar answered Sep 26 '22 00:09

AD7six