Moving from Postgres to RethinkDB

yes or no?

 

by Guilherme Pereira

@guiferrpereira

 

The Problem

We need to scale between datacenters!!!

Postgres Replication

Master to Master

  • Bucardo
  • PgCluster
  • ...

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?

RethinkDB ? Isn't dead?

Syntax?

* ReQL

Ruby / Rails and RethinkDB

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?

Pretty console

!!! 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?

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,260