In ancient Roman religion and myth, Janus (/ˈdʒeɪnəs/ JAY-nəs; Latin: Ianvs [ˈi̯aːnʊs]) is the god of beginnings, gates, transitions, time, duality, doorways,[2] passages, frames, and endings. (wikipedia)
Janus ActiveRecord is generic primary/replica proxy for ActiveRecord 7.1+ and MySQL (via mysql2
and trilogy
). It handles the switching of connections between primary and replica database servers. It comes with an ActiveRecord database adapter implementation.
Note: Trilogy support is experimental at this stage.
Janus is heavily inspired by Makara from TaskRabbit and then Instacart. Unfortunately this project is unmaintained and broke for us with Rails 7.1. This is an attempt to start afresh on the project. It is definitely not as fully featured as Makara at this stage.
Learn more about its origins: https://tech.olioex.com/ruby/2024/04/16/introducing-janus.html.
Notes: GEM is currently tested with MySQL 8, Ruby 3.2, ActiveRecord 7.1+
Use the current version of the gem from rubygems in your Gemfile
.
gem 'janus-ar'
This project assumes that your read/write endpoints are handled by a separate system (e.g. DNS).
After a write request during a thread the adapter will continue using the primary
server, unless the context is specifically released.
For Rails 7.2 you'll need to manually register the database adaptor in config/application.rb
after requiring rails but before entering the application configuration, e.g.
require 'rails/all'
ActiveRecord::ConnectionAdapters.register("janus_trilogy", "ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter", 'janus-ar/active_record/connection_adapters/janus_trilogy_adapter')
# ...or...
ActiveRecord::ConnectionAdapters.register("janus_mysql2", "ActiveRecord::ConnectionAdapters::JanusMysql2Adapter", 'janus-ar/active_record/connection_adapters/janus_mysql2_adapter')
ActiveRecord 7.1 was tested up to releases v0.15.*. After this release we only tested Rails 7.2+. This does not mean it is not compatible, just not tested.
Update your database.yml as follows:
development:
adapter: janus_mysql2
database: database_name
janus:
primary:
<<: *default
host: primary-host.local
replica:
<<: *default
password: ithappenstobedifferent
host: replica-host.local
Note: For trilogy
please use adapter "janus_trilogy". You'll probably need to add the following to your configuration to have it connect:
ssl: true
ssl_mode: 'REQUIRED'
tls_min_version: 3
tls_min_version
here refers to TLS1.2.
Otherwise you will get an error like the following (see https://github.com/trilogy-libraries/trilogy/issues/26):
trilogy_auth_recv: caching_sha2_password requires either TCP with TLS or a unix socket: TRILOGY_UNSUPPORTED"
A context is local to the curent thread of execution. This will allow you to stick to the primary safely in a single thread in systems such as sidekiq, for instance.
If you need to clear the current context, releasing any stuck connections, all you have to do is:
Janus::Context.release_all
Janus::Context.stick_to_primary
You can set a logger instance to ::Janus::Logging::Logger.logger
:
Janus::Logging::Logger.logger = ::Logger.new(STDOUT)
If using ActiveRecord
logging, Janus will append the name of the connection used to any logs e.g. [primary]
or [replica]
.
In general: Any SELECT
statements will execute against your replica(s), anything else will go to the primary.
There are some edge cases:
SET
operations will be sent to all connectionsconnect!
, disconnect!
, reconnect!
, and clear_cache!
are invoked on all underlying connectionsSELECT ... FOR UPDATE
) will always be sent to the primaryJanus does not support Rails' read/write split or sharding using with_connection
.
Amazing project logo by @undevelopedbruce.