review log
- 20191114
- 实现pg->es同步数据的方式有几种
- 基于插件
- 常见的几个插件都需要在db上装插件,在我们的使用场景上不太实用,因为db和es全部都交给aws托管
- 一般的插件使用时针对db中单表对es中单index的同步,我们需要的场景更加复杂,需要支持自定义数据聚合后倒入到index
- 基于框架/库
- laravel有EloquentOrm+Scout的库可以同步model到es上,可以参考migrate的场景,在node中似乎没找到比较成熟的库,打通orm和es
- 如果有库可以用,需要支持多表数据聚合的场景
- 自行设计实现(通用的源+目标数据同步策略)
- 基于queue
- 把同步任务抛给queue,由queue调度执行同步任务
- 基于cronjob
- 定期检查DB中有没有新数据,如果有就写入es
- 基于queue
- 基于插件
- 由于考虑到第一第二种方式对于我们目前项目的底层设施和技术栈不合适,因此还是考虑自行设计实现的方式。具体的策略可以参考下面es官方关于sync的文章
- 实现pg->es同步数据的方式有几种
keyword extend
- database sync/replication/transform
- replicated to Elasticsearch
- replication strategy
- data sync strategy
- change data capture
- ETL(extract transform load)
reference
201911月之前宏斌、米莱的一些方案设计
- https://docs.google.com/document/d/19XnxKGw7KMBW_o7IWM1pCR179HdqWLilxYgk7L43c-A/edit
- 需求描述
- https://docs.google.com/document/d/1YYG8ptvuzuvShioIE168fuLj06N_ptCjL0_4R1Qln7M/edit
- 基于lambda+sqs的同步方案
- https://drive.google.com/drive/u/1/folders/1X-1URYrUy_tyx7nNfzmuW86Yg8M_zTHP
- 基于lambda+sqs同步方案图示
- https://docs.google.com/document/d/1m3D8aDxb7UYBGczY1_KgvDy_HHvpeELvfAZDBtGMxnU/edit#
- laravel的EloquentOrm+Scout同步插件,https://laravel.com/docs/5.8/scout#introduction
- https://docs.google.com/document/d/19XnxKGw7KMBW_o7IWM1pCR179HdqWLilxYgk7L43c-A/edit
https://www.elastic.co/blog/found-keeping-elasticsearch-in-sync(es官方的introduction)
- 性能的来源
- http请求过多会影响客户端性能
- update对于es是性能耗费比较高的,尽量使用bulk api进行操作
- 队列可以限制系统瓶颈,防止打爆es
- 基于queue的使用
- 定时任务+批量更新
- 一般处理只需要插入的资源
- 错误处理、重试等,都需要自己来维护
- 队列的问题
- 全量index
- 性能的来源
-
- 基于logstash的定期轮训的方案,需要装插件
- 最有价值的点,讨论了获取需要同步的数据的查询语句
- (UNIX_TIMESTAMP(modification_time) > :sql_last_value AND modification_time < NOW()) ORDER BY modification_time ASC
-
- 讨论是否有aws原生同步pg-es的方案,比较麻烦,Here's a good article explaining how to connect it all together, namely:
- how to stream RDS data into a Kinesis Stream
- configuring a Lambda function to handle the stream
- push the data to your AWS ES instance
- 讨论是否有aws原生同步pg-es的方案,比较麻烦,Here's a good article explaining how to connect it all together, namely:
https://www.quora.com/What-is-the-best-way-to-sync-Postgres-and-ElasticSearch
https://www.quora.com/How-do-I-sync-data-between-PostgreSQL-and-Elasticsearch
- Batch Sync: Write an application/script to pull the data from PosgreSQL to index into Elasticsearch. You can schedule this job to be executed at a specific time you wish.
- Real-time Sync: Right after you insert/update a record into PostgreSQL successfully, you send a request to index/update this record to Elasticsearch. This is often done with a task queue system, like Celery.
http://hood.ie/blog/beyond-progressive-web-apps-part-2.html
- 使用一张独立的表,来记录更新的id,由client那边进行拉取;和轮训拉取是另一种模式
https://stackoverflow.com/questions/39667292/postgres-with-elasticsearch-keep-in-sync-nodejs
- 用redis的pub/sub模式实现
- 自行用cron-job实现
https://www.jianshu.com/p/629f698a7c58
- 几种需要安装插件的同步数据方案