ruk·si

What Database Should I Use?

Updated at 2019-06-04 15:43

This note is about selecting a database. Mostly my humble opinions.

Main Database

Main database contains your business critical data.

Use PostgreSQL if possible. Switching database system is usually very expensive and PostgreSQL is the most mature. You will thank me later.

If you need sharding with PostgreSQL, consider using repmgr that is a set of tools to help manage PostgreSQL database clusters.

You may be forced to use MySQL/MariaDB if your framework or platform requires that. MySQL is not that bad, only inferior to PostgreSQL in every possible way.

Don't touch any new trendy databases like MongoDB was some years ago. They will kick you in the nuts when you realize how immature they are. But if you know what you are doing, go ahead.

Secondary Database

Secondary database contains stuff that is frequently accessed but less business critical e.g. counters, hits, votes, ads, friends, tags, a/b tests, csrf tokens and cache.

Use Redis as the secondary database. Memcached is a good alternative, but I think Redis is superior in every way.

Server memory amount is important when using Redis or Memcaced. They both can spend a lot of server memory, so monitor the usage after setup and upscale as needed.

You may also consider MongoDB for a secondary database, but it is not as fast as Redis or Memcached. Half-way between a main database and a secondary database. MongoDB has no place in my own development toolbox, just to let it out there.

Binary Database

Binary files like images, archives and downloadables don't belong in the databases mentioned above. Use a binary file hosting service for those.

Usually you use the one provided by your cloud-provider:

  • AWS S3 with optional AWS CloudFront (CDN)
  • Azure Storage Account (Blob Container)
  • Google Cloud Storage
  • OpenStack Swift