Page 70 of 170

Using multiple MongoDB databases instead of one – performance check

I'm starting to develop a new application. Can't say what it is, but it perfectly fits MongoDB Document Oriented Database approach. Everything is great. except of small detail - I don't want to store everything in one database. Of course I could use collections and embedded documents to organize whole nested structures and keep users stuff separated, although it would make source code much more complicated than it should be. Instead I've decided to use one MongoDB database per user. That way I can separate users data and I don't need to worry about scoping it out. There will be a gateway, that will authorize incoming requests to a proper database.

Gateway

Schema is pretty straightforward and the only thing that was bothering me was the multi-db switching performance. I've decided to make a simple benchmark that would test if there's a difference when using one or many databases. Results are promising. It seems, that there's only around 5% (5.3% exactly) performance loss when using many databases instead of one. 5% is a difference level that I can easily accept. To be honest I think, I will gain much more especially when I will have a lot of data. Lets say I have 100 customers with 100 000 000 records. With one database I would have to query all of it. With separate databases, I will have to query only 1% of it.

Below you can see performance difference when querying one vs many MongoDB databases.

Dbs I will definitely go with that approach and I will try to keep you posted.

Note: This is not a full-pro-extremely accurate long-time test - more like a proof of concept. Keep that in mind ;)

Rails + Devise and remember_me (rememberable) by default

I've wanted to add an automatic remember_me for Devise by default (without a checkbox). First I've tried to do something like this:

def create
  params[:user].merge!(remember_me: true)
  super
end

Unfortunately it doesn't seem to work. I have a rememberable strategy included in my User model, but still, remember_user_token cookie was not created. I don't know why, even with remember_me option provided, it sets it to false. The easiest (and working) way to fix this, is to overwrite remember_me method, so it will always return true value.

# User of our portal
class User < ActiveRecord::Base

  devise :database_authenticatable, 
    :trackable, :encryptable, :confirmable, :recoverable,
    :registerable, :validatable, :lockable, :rememberable

  # @return [Boolean] user should be remembered when he logs in (with cookie)
  #   so he won't be asked to login again
  def remember_me
    true
  end
end

To be honest I didn't have time to investigate it more deeply, so maybe there's a better solution but this one works. After that, remember_user_token cookie is created and the session isn't lost after user closes his browser.

Update

As Christoph mentioned in one of the comments, this is the "official" way to do this: , so probably there's no better way ;)

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑