Rails has a very useful feature called schema cache.
When the Rails application is booted, it executes
SHOW FULL FIELDS
query to get information about the tables and the datatype of
all the columns.
Imagine, we have a large Rails application with many models. Rebooting
the servers will fire the above heavy query
SHOW FULL FIELDS for each model.
This expensive operation may also kill our database.
The motive behind schema cache is to serialize database details like tables, columns, types into a file. Load this file on the application servers. Read the data from this file to avoid hitting the database.
This avoids load on our database and speeds up the boot time of servers.
Schema cache is enabled by default for all Rails apps.
The above configuration is not useful unless we generate the dump file on our app server. Below is the command to generate the schema cache.
This will generate a file
db folder by default.
The other option is to use an
ENV variable -
SCHEMA_CACHE, to set the path.
The above command needs to be executed with every deployment of the Rails app.
Instead of directly querying the database, this file will be read, which will be faster than querying the database.
Rails, recently added a feature
to specify the schema cache path in
This is particularly helpful when we have multiple DB’s in our Rails app. We can set our own custom schema cache path for each database. Or during every release to production, we specify the path to which the schema file should be dumped.
Rails also added the flexibility
to let people choose between
YAML as a serialization strategy.
This can be done by specifying the extension
The default serialization strategy is
For a large Rails application, the schema cache size will be huge. This can be a problem when the application is deployed to Kubernetes because of
gzip support for both the
YAML and the
Marshal serialization strategies.
For making the
gzip file we just need to update the filename with
schema_cache_path is not specified, the filename will be read
ENV or a default will be derived.