Home php教程 php手册 [轉]redis;mongodb;memcache三者的性能比較

[轉]redis;mongodb;memcache三者的性能比較

Jun 13, 2016 am 09:33 AM
memcache mongodb redis programming

先说我自己用的情况:

最先用的memcache ,用于键值对关系的服务器端缓存,用于存储一些常用的不是很大,但需要快速反应的数据


然后,在另一个地方,要用到redis,然后就去研究了下redis. 一看,显示自己安装了php扩展,因为有服务器上的redis服务端,自己本地就没有安装,其实用法和memcache基本一样,可能就是几个参数有所不 同。当然 它们缓存的效果也不一样,具体的哪里不一样,一下就是一些资料,和自己的总结



1、 Redis和Memcache都是将数据存放在内存中,都是内存数据库。不过memcache还可用于缓存其他东西,例如图片、视频等等。
 

从以下几个维度,对redismemcachemongoDB 做了对比,欢迎拍砖

1、性能

都比较高,性能对我们来说应该都不是瓶颈

总体来讲,TPS方面redismemcache差不多,要大于mongodb

2、操作的便利性

      memcache数据结构单一

      redis丰富一些,数据操作方面,redis更好一些,较少的网络IO次数

       mongodb支持丰富的数据表达,索引,最类似关系型数据库,支持的查询语言非常丰富

3、内存空间的大小和数据量的大小

       redis2.0版本后增加了自己的VM特性,突破物理内存的限制;可以对key value设置过期时间(类似memcache

       memcache可以修改最大可用内存,采用LRU算法

       mongoDB适合大数据量的存储,依赖操作系统VM做内存管理,吃内存也比较厉害,服务不要和别的服务在一起

4、可用性(单点问题)

对于单点问题,

             redis,依赖客户端来实现分布式读写;主从复制时,每次从节点重新连接主节点都要依赖整个快照,无增量复制,因性能和效率问题,

所以单点问题比较复杂;不支持自动sharding,需要依赖程序设定一致hash 机制。

一种替代方案是,不用redis本身的复制机制,采用自己做主动复制(多份存储),或者改成增量复制的方式(需要自己实现),一致性问题和性能的权衡

             Memcache本身没有数据冗余机制,也没必要;对于故障预防,采用依赖成熟的hash或者环状的算法,解决单点故障引起的抖动问题。

             mongoDB支持master-slave,replicaset(内部采用paxos选举算法,自动故障恢复),auto sharding机制,对客户端屏蔽了故障转移和切分机制。

5可靠性(持久化)

对于数据持久化和数据恢复,

         redis支持(快照、AOF):依赖快照进行持久化,aof增强了可靠性的同时,对性能有所影响

          memcache不支持,通常用在做缓存,提升性能;

          MongoDB1.8版本开始采用binlog方式支持持久化的可靠性

6、数据一致性(事务支持)

         Memcache 在并发场景下,用cas保证一致性

        redis事务支持比较弱,只能保证事务中的每个操作连续执行

        mongoDB不支持事务

7、数据分析

         mongoDB内置了数据分析的功能(mapreduce),其他不支持

8、应用场景

        redis:数据量较小的更性能操作和运算上

        memcache:用于在动态系统中减少数据库负载,提升性能;做缓存,提高性能(适合读多写少,对于数据量比较大,可以采用sharding

        MongoDB:主要解决海量数据的访问效率问题

 

 

 

最近一直在研究key-value的存储,简单记一下感受。。一些memcache和redis的安装和使用就不赘述啦。只简单说说两种方案的差别。一些 感想和测试结果未必足够能说明问题,有什么不妥请大家指正。因为这两天在学习的过程发现一直在更正自己认识的缺陷,每天都会否定前一天的想法。。好了,费 话少说。

  经过对50万个数据存储的研究发现:

  每秒单条指令执行量    

    memcache  约3万次

  redis     约1万次

    而且,memcache的一大优点是可以通过一个函数直接设置过期时间,而redis需要两个函数才可以既设置了键值对又设置过期时间,也就是redis在这点上效率变成了原来的一半,即5千次,这对于大部分需求来说,有点太慢了。

  memcache的测试代码如下:

$mem = new Memcache;

$mem->connect("127.0.0.1", 11211);

$time_start = microtime_float();

//保存数据

for($i = 0; $i

    $mem->set("key$i",$i,0,3);

}

$time_end = microtime_float();

$run_time = $time_end - $time_start;

echo "用时 $run_time 秒\n";

function microtime_float()

{

    list($usec, $sec) = explode(" ", microtime());

    return ((float)$usec + (float)$sec);

}

?>

  redis的测试代码如下:redis1.php 此代码大概需要10秒左右

//连接

$redis = new Redis();

$redis->connect('127.0.0.1', 6379);

$time_start = microtime_float();

//保存数据

for($i = 0; $i

    $redis->sadd("key$i",$i);

}

$time_end = microtime_float();

$run_time = $time_end - $time_start;

echo "用时 $run_time 秒\n";

//关闭连接

$redis->close();

 

function microtime_float()

{

    list($usec, $sec) = explode(" ", microtime());

    return ((float)$usec + (float)$sec);

}

?>

  如果需要在设置键值的同时设置过期时间,大概执行需要20秒左右,测试代码如下:redis2.php

//连接

$redis = new Redis();

$redis->connect('127.0.0.1', 6379);

$time_start = microtime_float();

//保存数据

for($i = 0; $i

    $redis->sadd("key$i",$i);

    $redis->expire("key$i",3);

}

$time_end = microtime_float();

$run_time = $time_end - $time_start;

echo "用时 $run_time 秒\n";

//关闭连接

$redis->close();

 

function microtime_float()

{

    list($usec, $sec) = explode(" ", microtime());

    return ((float)$usec + (float)$sec);

}

?>

  后来在网上发现redis有一个神奇的功能叫事务,通过multi原子性的将一段代码块依次执行,从而达到一个完整功能模块的执行。不幸的是,通过测 试发现,采用multi方式执行代码时并没有减少请求次数,相反在执行multi指令和exec指令时都要发送请求,从而将运行时间变成了原来的四倍,即 四条指令的运行时间。测试代码如下:redis3.php

//连接

$redis = new Redis();

$redis->connect('127.0.0.1', 6379);

$time_start = microtime_float();

//保存数据

for($i = 0; $i

    $redis->multi();

    $redis->sadd("key$i",$i);

    $redis->expire("key$i",3);

    $redis->exec();

}

$time_end = microtime_float();

$run_time = $time_end - $time_start;

echo "用时 $run_time 秒\n";

//关闭连接

$redis->close();

 

function microtime_float()

{

    list($usec, $sec) = explode(" ", microtime());

    return ((float)$usec + (float)$sec);

}

?>

    问题出现了瓶颈,有好多公司需要海量数据处理,每秒5000次远不能满足需求,然后由于redis主从服务器上比memcache有更大的优势, 为了将来数据的着想,不得不使用redis,这时候出现了一种新的方式,即phpredis提供的pipline功能,该功能能够真正的将几条代码封装成 一次请求,从而大大提高了运行速度,50万次的数据执行只有了58秒。测试代码如下:redis4.php

//连接

$redis = new Redis();

$redis->connect('127.0.0.1', 6379);

$time_start = microtime_float();

//保存数据

for($i = 0; $i

  $pipe=$redis->pipeline();

    $pipe->sadd("key$i",$i);

    $pipe->expire("key$i",3);

    $replies=$pipe->execute();

}

$time_end = microtime_float();

$run_time = $time_end - $time_start;

echo "用时 $run_time 秒\n";

//关闭连接

$redis->close();

 

function microtime_float()

{

    list($usec, $sec) = explode(" ", microtime());

    return ((float)$usec + (float)$sec);

}

?>

  运用这个操作可以非常完美的将赋值操作和设置过期时间操作打包到一个请求去执行,大大提高了运行效率。

 

redis安装:http://mwt198668.blog.163.com/blog/static/48803692201132141755962/

memcache安装:http://blog.csdn.net/barrydiu/article/details/3936270

redis设置主从服务器:http://www.jzxue.com/fuwuqi/fuwuqijiqunyuanquan/201104/15-7117.html

memcache设置主从服务器:http://www.cnblogs.com/yuanermen/archive/2011/05/19/2051153.html

本文來自:http://blog.csdn.net/a923544197/article/details/7594814

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

How to configure slow query log in centos redis How to configure slow query log in centos redis Apr 14, 2025 pm 04:54 PM

Enable Redis slow query logs on CentOS system to improve performance diagnostic efficiency. The following steps will guide you through the configuration: Step 1: Locate and edit the Redis configuration file First, find the Redis configuration file, usually located in /etc/redis/redis.conf. Open the configuration file with the following command: sudovi/etc/redis/redis.conf Step 2: Adjust the slow query log parameters in the configuration file, find and modify the following parameters: #slow query threshold (ms)slowlog-log-slower-than10000#Maximum number of entries for slow query log slowlog-max-len

PostgreSQL performance optimization under Debian PostgreSQL performance optimization under Debian Apr 12, 2025 pm 08:18 PM

To improve the performance of PostgreSQL database in Debian systems, it is necessary to comprehensively consider hardware, configuration, indexing, query and other aspects. The following strategies can effectively optimize database performance: 1. Hardware resource optimization memory expansion: Adequate memory is crucial to cache data and indexes. High-speed storage: Using SSD SSD drives can significantly improve I/O performance. Multi-core processor: Make full use of multi-core processors to implement parallel query processing. 2. Database parameter tuning shared_buffers: According to the system memory size setting, it is recommended to set it to 25%-40% of system memory. work_mem: Controls the memory of sorting and hashing operations, usually set to 64MB to 256M

How to install redis in centos7 How to install redis in centos7 Apr 14, 2025 pm 08:21 PM

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

See all articles