Data Layer

Most applications use relational databases for main data storage. Often it’s necessary to store temporary data, perform analytics or have a full-text search. Those tasks are easier accomplished by using key-value storage or data-analytics tools.

Relational databases

We prefer PostgreSQL database over its rivals, because:

  • It is robust and stable database solution existing for over 20 years.
  • It is faster than its rivals in most cases. With lots of indexes it usually writes slower than other engines.
  • Fast column adding.
  • It can be replacement for NoSQL databases by supporting JSONB, HSTORE, ARRAY.
  • PostGIS adds support for spatial database.

Database features

In Ruby on Rails world, there is a tendency to avoid database features and reimplement them using ruby. Database constraints are the simplest way to prevent data inconsistency.

That’s why we always:

  • Add foreign key constraints to ensure data consistency.
  • Add indexes on foreign keys to speed up queries.
  • Create views for querying data from multiple tables.
  • Use Rein for creating views, automatically adding indexes, adding constraints, or other database-specific features.

Database configuration

On development machines we don’t need data integrity, so extreme PostgreSQL configuration can be used:

fsync = 'off'
full_page_writes = 'off'
random_page_cost = 1.1

This ensures maximum database speed by minimizing writes to disk.

Key-value store

Web applications often require a key-value store. Such storage is useful to cache data, store translations, power background workers or store additional data (flags, temporary states, etc.)

One of the best solutions for this is redis because:

Data analytics

Performing data analytics usually is a complicated task. Having good tools simplifies this task. We prefer to use Elasticsearch for this because:

  • Provides great integration with various platforms.
  • Can be used together with ActiveRecord.
  • Provides powerful commands for data-analytics.
  • It’s easy to setup and maintain unlike bigger solutions, like Hadoop.
  • In case if it does not scale enough for some project, more complicated solutions such as Hadoop can be used.

Quite often web applications provide some kind of text search. Depending on requirements, different solutions can be used:

  • For simple cases PostgreSQL’s full-text search works just fine until:
    • You have large data sets.
    • You need specific ranking algorithm.
    • You need very specific and complicated fuzzy search.
  • When search gets complicated Elasticsearch providers powerful solution.
  • If simplicity is a requirement in specification, platform solutions such as Algolia can be used as well.