PHP 应用程序何时使用 Redis 而不是 MySQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3966689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:35:50  来源:igfitidea点击:

When to use Redis instead of MySQL for PHP applications?

phpmysqlredis

提问by james.bcn

I've been looking at Redis. It looks very interesting. But from a practical perspective, in what cases would it be better to use Redis over MySQL?

我一直在看Redis。它看起来很有趣。但是从实际的角度来看,在什么情况下使用 Redis 比使用 MySQL 更好?

回答by Martin Wickman

Ignoring the whole NoSQL vs SQL debate, I think the best approach is to combine them. In other words, use MySQL for for some parts of the system (complex lookups, transactions) and redis for others (performance, counters etc).

忽略整个 NoSQL 与 SQL 的争论,我认为最好的方法是将它们结合起来。换句话说,将 MySQL 用于系统的某些部分(复杂查找、事务),将 redis 用于其他部分(性能、计数器等)。

In my experience, performance issues related to scalability (lots of users...) eventually forces you to add some kind of cache to remove load from the MySQL server and redis/memcached is very good at that.

根据我的经验,与可扩展性(大量用户...)相关的性能问题最终会迫使您添加某种缓存以从 MySQL 服务器中移除负载,而 redis/memcached 非常擅长于此。

回答by Thomas

I am no Redis expert, but from what I've gathered, both are pretty different. Redis :

我不是 Redis 专家,但从我收集到的信息来看,两者完全不同。Redis :

  • Is not a relational database (no fancy data organisation)
  • Stores everything in memory (faster, less space, probably less safe in case of a crash)
  • Is less widely deployed on various webhosts (if you're not hosting yourself)
  • 不是关系数据库(没有花哨的数据组织)
  • 将所有内容存储在内存中(更快,空间更少,发生崩溃时可能更不安全)
  • 在各种虚拟主机上部署较少(如果您不是自己托管)

I think you might want to use Redis for when you have a small-ish quantity of data that doesn't need the relational structure that MySQL offers, and requires fast access. This could for example be session data in a dynamic web interface that needs to be accessed often and fast.

我认为当您拥有少量不需要 MySQL 提供的关系结构并且需要快速访问的数据时,您可能希望使用 Redis。例如,这可能是需要经常快速访问的动态 Web 界面中的会话数据。

Redis could also be used as a cache for some MySQL data which is going to be accessed very often (ie: load it when a user logs in).

Redis 还可以用作某些 MySQL 数据的缓存,这些数据将经常被访问(即:在用户登录时加载它)。

I think you're asking the question the wrong way around, you should ask yourself which one is more suited to an application, rather than which application is suited to a system ;)

我认为您问这个问题的方式是错误的,您应该问自己哪个更适合应用程序,而不是哪个应用程序适合系统;)

回答by Will

MySQL is a relational data store. If configured (e.g. using innodbtables), MySQL is a reliabledata-store offering ACIDtransactions.

MySQL 是一种关系数据存储。如果配置(例如使用innodb表),MySQL 是一个提供ACID事务的可靠数据存储。

Redis is a NoSQL database. It is faster (if used correctly) because it trades speed with reliability (it is rare to run with fsync as this dramatically hurts performance) and transactions (which can be approximated - slowly - with SETNX).

Redis 是一个 NoSQL 数据库。它更快(如果使用得当),因为它以可靠性(很少使用 fsync 运行,因为这会极大地损害性能)和事务(可以用SETNX近似——缓慢地交换速度

Redis has some very neat featuressuch as sets, lists and sorted lists.

Redis 有一些非常简洁的特性,比如集合、列表和排序列表。

These slides on Redislist statistics gathering and session management as examples. There is also a twitter clonewritten with redis as an example, but that doesn't mean twitter use redis (twitter useMySQL with heavy memcache caching).

这些关于 Redis 的幻灯片列出了统计信息收集和会话管理作为示例。还有一个用 redis 编写的 twitter克隆作为示例,但这并不意味着 twitter 使用 redis(twitter 使用MySQL 和大量的 memcache 缓存)。

回答by Preetham R U

MySql -

MySql -

1) Structured data 2) ACID 3) Heavy transactions and lookups.

1) 结构化数据 2) ACID 3) 大量事务和查找。

Redis -

Redis -

1) Non structured data 2) Simple and quick lookups. for eg - token of a session 3) use it for caching layer.

1) 非结构化数据 2) 简单快速的查找。例如 - 会话的令牌 3) 将其用于缓存层。

回答by Punnerud

Redis, SQL (+NoSQL) have their benefits+drawbacks and often live side by side:

Redis、SQL(+NoSQL)各有优缺点,经常并存:

  • Redis - Local variables moved to a separate application
    • Easy to move from local variables/prototype
    • Persistant storrage
    • Multiple users/applications all see the same data
    • Scalability
    • Failover
    • (-) Hard to do more advanced queries/questions on the data
  • NoSQL
    • Dump raw data into the "database"
    • All/most of Redis features
    • (-) Harder to do advanced queries, compared to SQL
  • SQL
    • Advanced queries between data
    • All/most of Redis features
    • (-) Need to place data into "schema" (think sheet/Excel)
    • (-) Bit harder to get simple values in/out than Redis/NoSQL
  • Redis - 局部变量移动到单独的应用程序
    • 易于从局部变量/原型移动
    • 持久存储
    • 多个用户/应用程序都看到相同的数据
    • 可扩展性
    • 故障转移
    • (-) 很难对数据进行更高级的查询/问题
  • 无SQL
    • 将原始数据转储到“数据库”中
    • 所有/大部分 Redis 功能
    • (-) 与 SQL 相比,更难进行高级查询
  • SQL
    • 数据之间的高级查询
    • 所有/大部分 Redis 功能
    • (-) 需要将数据放入“模式”中(想想工作表/Excel)
    • (-) 获取简单值的输入/输出比 Redis/NoSQL 更难

(different SQL/NoSQL solutions can vary. You should read up on CAP theoremand ACIDon why one system can't simultaneously give you all)

(不同的 SQL/NoSQL 解决方案可能会有所不同。您应该阅读CAP 定理ACID,了解为什么一个系统不能同时为您提供所有解决方案)

回答by Ayush Jain

According to the official website, Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. Actually, Redis is an advanced key-value store. It is literally super fast with amazingly high throughput as it can perform approximately 110000 SETs per second, about 81000 GETs per second. It also supports a very rich set of data types to store. As a matter of fact, Redis keeps the data in-memory every time but also persistent on-disk database. So, it comes with a trade-off: Amazing speed with the size limit on datasets (as per memory). In this article, to have some benchmarks in comparison to MySQL, we would be using Redis as a caching engine only.

根据官网介绍,Redis 是一种开源(BSD 许可)、内存中的数据结构存储,用作数据库、缓存和消息代理。实际上,Redis 是一种高级的键值存储。它实际上是超快的,具有惊人的高吞吐量,因为它每秒可以执行大约 110000 个 SET,大约每秒 81000 个 GET。它还支持一组非常丰富的数据类型来存储。事实上,Redis 每次都将数据保存在内存中,但也会将数据保存在磁盘上的持久数据库中。因此,它带来了一个权衡:惊人的速度和数据集的大小限制(根据内存)。在本文中,为了与 MySQL 进行一些比较,我们将仅使用 Redis 作为缓存引擎。

Read Here: Redis vs MySQL Benchmarks

阅读此处:Redis 与 MySQL 基准测试