TIL, 2018-06-03, Sharding, Read/Write Databases
Visit Grade Tecko and its accompanying Heroku app!
Research on Sharding, Reading Octopus
- If you have
HashWithIndifferentAccess
, just use it? ActiveRecord::VERSION::MAJOR
andMINOR
is a thing to check versions.- Logger method:
def self.logger
if defined?(Rails.logger)
@logger ||= Rails.logger
else
@logger ||= Logger.new($stderr)
end
end
- Hooks into
ActiveRecord::Base.connection
, creates anOctopus::Proxy
- Inclusion method:
def self.included(base)
base.extend(ClassMethods)
end
How would I implement separate databases for reading and writing operations?
- Read/write master databases and read-only slaves are a common pattern, especially for big applications doing mostly read accesses/data warehouses.
- It allows you to scale and to tune the databases differently, for either efficient reads or efficient writes.
- It really depends on what you are trying to achieve by having two databases.
- If it is for performance reasons (which i suspect it may be) i would suggest you look into denormalizing the read-only database as needed for performance.
- If performance isn’t an issue then I wouldn’t mess with the read-only schema.
- I’ve worked on similar systems where there would be a read/write database that was only lightly used by administrative users. That database would then be replicated to the read only database during a nightly process.
Slow Rails Queries
- New Relic,
PgHero
to track down query bottlenecks in the applications. Seq Scans
: Means PG needs to scan a table row by row to find matching entries.- Seed local database to mimic production. Perform benchmarks on a local computer, with settings resembling a real production site.
- Siege for local benchmarks.
Musings/Links
- Bootstrap 4 File Input: I fixed this with JQuery.
- Capybara upload file: Use
File.absolute_path
. - You need
type: feature
to enable Capybara methods (RSpec). - When creating an index over multiple columns, put the column with least variance first.
- Getting a file’s MIME type using Rack.
Rack::Mime.mime_type(File.extname(filename))
- Apparently you shouldn’t offset when using an index?