RethinkDB 让实时交互更顺畅

原创
2017/02/07 19:13
阅读数 2.3K

 

Rethinkdb介绍

Rethinkdb官方网站点这里

RethinkDB is the first open-source, scalable JSON database built from the ground up for the realtime web. It inverts the traditional database architecture by exposing an exciting new access model – instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results to applications in realtime. RethinkDB’s realtime push architecture dramatically reduces the time and effort necessary to build scalable realtime apps.

In addition to being designed from the ground up for realtime apps, RethinkDB offers a flexible query language, intuitive operations and monitoring APIs, and is easy to setup and learn.

See the post Advancing the realtime web for more technical details on RethinkDB’s mission.

以下场景适合使用Rethinkdb

  • Collaborative web and mobile apps
  • Streaming analytics apps
  • Multiplayer games
  • Realtime marketplaces
  • Connected devices

For example, when a user changes the position of a button in a collaborative design app, the server has to notify other users that are simultaneously working on the same project. Web browsers support these use cases via WebSockets and long-lived HTTP connections, but adapting database systems to realtime needs still presents a huge engineering challenge.

举例,当用户在一款协作设计类app里移动一个按钮的位置时,服务器必须同步通知给所有在同一个项目上的其他用户。浏览器是通过websockets和长连接也实现这个功能的,但要数据库层实现仍是一个巨大的战。

RethinkDB is the first open-source, scalable database designed specifically to push data to applications in realtime. It dramatically reduces the time and effort necessary to build scalable realtime apps.

以下场景不适合使用

When is RethinkDB not a good choice?

  • RethinkDB is not a good choice if you need full ACID support or strong schema enforcement—in this case you are better off using a relational database such as MySQL or PostgreSQL.
  • If you are doing deep, computationally-intensive analytics you are better off using a system like Hadoop or a column-oriented store like Vertica.
  • In some cases RethinkDB trades off write availability in favor of data consistency. If high write availability is critical and you don’t mind dealing with conflicts you may be better off with a Dynamo-style system like Riak.

安装

各平台的安装文档。os x 可以通过下载package安装 或 brew安装

启动

直接运行命令rethinkdb,注意将会在当前目录下创建一个数据库:rethinkdb_data。

启动成功后可以通过浏览器访问管理后台 http://localhost:8080/ (注意8080端口不要占用)

使用

Rethinkdb还是比较成熟的,通过这么丰富的安装工具,使用教程就可以看出来。所以使用上也很方便,官司方默认提供了Javascript/python/java/ruby的sdk,其它语言也有第三方提供了(全部语言的sdk列表)。下面以javascript使用进行举例,要先安装node/npm,然后安装rethinkdb的npm包。

>npm install rethinkdb

>node

输入以下代码

r = require('rethinkdb')
r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
  if(err) throw err;
  r.db('test').tableCreate('tv_shows').run(conn, function(err, res) {
    if(err) throw err;
    console.log(res);
    r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn, function(err, res)
    {
      if(err) throw err;
      console.log(res);
    });
  });
});

将创建表tv_shows及一条记录。通过http://localhost:8080/的Data Explorer也可以操作库表,还自带语法提示,赞一个。

特性功能介绍

  • RethinkDB使用的是自定义的sql:ReQL,详细的使用方法参见官方教程
  • RethinkDB可以与多种流行平台进行整合,如RabbitMQ,可以订阅表数据发生的变化,主动推送消息给MQ(RethinkDB supports changefeeds, which allow you to subscribe to changes on a table. The database pushes these changes to you as they happen.)
  • 同时社区还贡献了大量的扩展组件,增强了RethinkDB的功能,如连接池/ORM。
  • 实时推送变化,使得RethinkDB既不同是MongoDB,也不同于实时推送API,如 Firebase, PubNub, or Pusher等。订阅一个变化只需要
  • r.table('users').get('coffeemug').changes().run()
  • 服务器启动时默认会检查新版本,并提交一些运行数据到RethinkDB,如果不期望提交数据,可以启动时加参数 no-update-check 

参考

  1. 增强实现一个实时网站
  2. RethinkDB vs MongoDB
展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部