简体   繁体   English

如何在Node.js应用程序中将数据从Redis保存到MySQL

[英]How to persist data from Redis to MySQL in a Node.js app

Working on a new prototype for an app that should be able to handle 20.000+ incoming requests per minute for a short period of time ( like 20-30 minutes ) and give realtime results back to the user. 为应用程序开发新原型,该应用程序应该能够在短时间内(例如20-30分钟)每分钟处理20,000个传入请求,并将实时结果返回给用户。

Example: I show a yes/no question and you should be able to post a vote with YES / NO to our API, and our API should process that and give realtime information back on how many have voted yes and how many have voted no. 示例:我显示是/否问题,您应该能够对我们的API发布YES / NO投票,我们的API应该处理这个并提供实时信息,说明有多少投票赞成,有多少投票否。

All of those requests need to be saved to our MySQL database. 所有这些请求都需要保存到我们的MySQL数据库中。

I don't think that it's a wise idea to directly save those 20K requests realtime to the database, as that will create a huge load to the database, especially considering we need realtime data. 我认为直接将这些20K请求实时保存到数据库是一个明智的想法,因为这会给数据库带来巨大负担,特别是考虑到我们需要实时数据。

So my idea was to put Redis as middle layer. 所以我的想法是将Redis作为中间层。 The API writes to Redis, Redis returns the realtime count. API写入Redis,Redis返回实时计数。

But I still need to be able to persist the data. 但我仍然需要能够持久保存数据。 Is it possible to tell Redis to write all rows to MySQL when there are resources free? 当资源空闲时,是否可以告诉Redis将所有行写入MySQL?

Or would you suggest a completely different approach? 或者你会建议一种完全不同的方法吗?

I have looked into RabbitMQ as well, to queue all inserts and process them when possible, but as far as I know this can't return realtime data 我也研究过RabbitMQ,在可能的情况下排队所有插入并处理它们,但据我所知这不能返回实时数据

I have no code yet, as I am first considering the tools needed to implement this. 我还没有代码,因为我首先考虑实现这个所需的工具。

You can use both ways 你可以两种方式使用

1.Add Redis as the middle layer with a persistence option. 1.使用持久性选项将Redis为中间层。

Redis provide different range of persistence options. Redis提供不同范围的持久性选项。 If you don't use persistence option at all. 如果您根本不使用持久性选项。 When Redis server restarts all data will be lost. Redis服务器重新启动时,所有数据都将丢失。

You can configure Redis to save data at various events like 您可以配置Redis以在各种事件中保存数据

  • automatically from time to time 自动不时
  • when you manually call BGSAVE command 当您手动调用BGSAVE命令时

  • when redis is shutting down 当redis关闭时

Read more about Redis Persistence here . 在此处阅读有关Redis Persistence的更多信息。

  1. You can also use Redis with RabbitMQ . 您也可以将RedisRabbitMQ一起使用。

Redis to show real-time results back to the user. Redis将实时结果显示给用户。

RabbitMQ to add data in queue and save data to any Database. RabbitMQ在队列中添加数据并将数据保存到任何数据库。

RabbitMQ can handle load during peak times. RabbitMQ可以在高峰时段处理负载。 Thus all save call will be in Queue. 因此,所有保存呼叫都将在队列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM