Moving from Postgres to RethinkDB
yes or no?
by Guilherme Pereira
@guiferrpereira
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3242892/me.png)
The Problem
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230591/Screen_Shot_2016-11-14_at_23.29.25.png)
We need to scale between datacenters!!!
Postgres Replication
Master to Master
- Bucardo
- PgCluster
- ...
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230601/Screen_Shot_2016-11-14_at_23.34.11.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230615/Screen_Shot_2016-11-14_at_23.42.37.png)
Work in progress ...
Noop... You have to change boy!
But i love postgres... so what are my options?
- Redis
- Cassandra
- MongoDB
- ElasticSearch
- CouchDB
- HBase
- ...
Which one?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230623/Screen_Shot_2016-11-14_at_23.49.22.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230624/Screen_Shot_2016-11-14_at_23.50.07.png)
RethinkDB ? Isn't dead?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230635/Screen_Shot_2016-11-14_at_23.55.51.png)
Syntax?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3242819/Screen_Shot_2016-11-17_at_18.10.11.png)
* ReQL
Ruby / Rails and RethinkDB
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230637/Screen_Shot_2016-11-14_at_23.58.02.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230639/Screen_Shot_2016-11-14_at_23.58.50.png)
How easy it was moving?
# PostgresDB ActiveRecord
UR.where(user: user).includes(:user_transaction)
# RethinkDB NoBrainer
UR.where(user: user).eager_load(:transaction)
# PostgresDB ActiveRecord
UR.where(uid: uids)
# RethinkDB NoBrainer
UR.where(:uid.in => uids)
Text
# PostgresDB ActiveRecord
AM.
where(uid: [nil, d.uid]).
where(hd_model: [nil, d.hd.try(:fetch, 'model', nil)]).
order('CAST(uid IS NOT NULL AS INTEGER) + CAST(hd_model IS NOT NULL AS INTEGER) DESC').
pluck(:app_id).
first
# RethinkDB NoBrainer
AM.
where(:uid.in => [nil, d.uid]).
where(:hd_model.in => [nil, d.hd.try(:fetch, 'model', nil)]).
order_by(->(doc) { (doc[:uid].nil? ? 0 : 1) + (doc[:hd_model].nil? ? 0 : 1) } => :desc).
pluck(:app_id).raw.
first.try(:[], "app_id")
So easy?
Probably not!
# RethinkDB DIRECT ReQL (Slow!)
NoBrainer.run { |r|
r.table("transactions").merge { |transaction|
{
:usage_rights => r.table('usage_rights')
.get_all([transaction['identifier'], transaction['uid']], {
index: 'idx_identifier_uid_multi'
}).coerce_to('array')
}
}
}.count
# PostgresDB
usage_rights.
joins('JOIN transactions ON usage_rights.identifier = transactions.identifier
AND usage_rights.uid = transactions.uid')
Speed?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3242778/Screen_Shot_2016-11-17_at_17.52.14.png)
Pretty console
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3242814/Screen_Shot_2016-11-17_at_18.08.37.png)
!!! Attention this will work with javascript ReQL
You want ruby syntax go check on your irb :P
Dump data
* For this you have to install python driver...
* And it's a little bit slow...
sudo pip install rethinkdb
rethinkdb dump [options]
*** My advice do table by table :)
Just flowers? Why?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/274706/images/3230635/Screen_Shot_2016-11-14_at_23.55.51.png)
Well ...
Downsides
* You have to learn new language: ReQL
* Custom querys can be messy (simple joins)
* For Master-Master replication you will need 3 servers
* (LIKE / match) don't have indexes, it will be slow
* Refactor your code to get all stuff working
* Migrate your current data
Any Questions?
deck
By guiferrpereira
deck
- 1,181