R/W Splitting in Rails

Eec11c7d221770d15a4b16104f3cf07e?s=47 Kohei Suzuki
September 19, 2014

R/W Splitting in Rails

Eec11c7d221770d15a4b16104f3cf07e?s=128

Kohei Suzuki

September 19, 2014
Tweet

Transcript

  1. 384QMJUUJOHJO3BJMT ,PIFJ4V[VLJ

  2. 4FMGJOUSPEVDUJPO ,PIFJ4V[VLJ !FBHMFUNU 5XJUUFS (JU)VC ʜ  ! $PPLQBE*OD

  3. 3BJMTBOENVMUJQMF%#T 3BJMTTVQQPSUTUPFTUBCMJTIDPOOFDUJPOTUPEJ⒎FSFOU EBUBCBTFTGPSFBDINPEFMT class BlogBase < ActiveRecord::Base self.abstract_class = true

    establish_connection :production_blog end ! class Article < BlogBase; end class Category < BlogBase; end ! class Comment < ActiveRecord::Base establish_connection :production_comment end
  4. 3BJMTBOENVMUJQMF%#T 38TQMJUUJOHJT/05TVQQPSUFE XSJUFUPUIFNBTUFS SFBEGSPNUIFTMBWF 5IFSF`TTFWFSBMHFNTUPEP38TQMJUUJOHJO3BJMT PDUPQVT ECDIBSNFS NVMUJ@EC  BDUT@BT@SFBEPOMZBCMF

    
  5. PDUPQVT IUUQTHJUIVCDPNUDIBOEZPDUPQVT 4IBSEJOHBOE38TQMJUUJOHBSFTVQQPSUFE .JHSBUJPOJTBMTPTVQQPSUFE "DUJWFMZNBJOUBJOFE *UXPSLTXJUI3BJMT  .BOZGFBUVSFT MBSHFDPEFCBTF

  6. BDUT@BT@SFBEPOMZBCMF IUUQTSVCZHFNTPSHHFNTBDUT@BT@SFBEPOMZBCMF 0MEHFN MBTUVQMPBE  $PPLQBEVTFEJUVOUJMSFDFOUUJNFT )FBWJMZNPEJpFEUPTVQQPSU3BJMT %P38TQMJUUJOHPOMZ +VTU-0$ BTPGPVS3BJMTTVQQPSUFE

    WFSTJPO
  7. 38TQMJUUJOH 8FOFFE38TQMJUUJOHPOMZ BDUT@BT@SFBEPOMZBCMFJTUPPPMEBOEGSBHJMFUP BHHSFTTJWFDIBOHFTPG3BJMT *SFJOWFOUFEBOFXHFNMJLFBDUT@BT@SFBEPOMZBCMF IUUQTHJUIVCDPNFBHMFUNUTXJUDI@QPJOU

  8. TXJUDI@QPJOU %P38TQMJUUJOHPOMZ /PTIBSEJOH /PNJHSBUJPOT 'FXFSNPOLFZQBUDIFT JOBXFMMPSEFSFE NBOOFS IUUQTXXXqJDLSDPNQIPUPTFMMSCSPXO

  9. $POpHVSBUJPO EBUBCBTFZNM production_blog_master: adapter: mysql2 database: blog username: blog_writable host:

    db-blog-master ! production_blog_slave: adapter: mysql2 database: blog username: blog_readonly host: db-blog-slave
  10. $POpHVSBUJPO *OJUJBMJ[FS SwitchPoint.configure do |config| config.define_switch_point :blog, readonly: :"#{Rails.env}_blog_slave", writable:

    :"#{Rails.env}_blog_master" end
  11. $POpHVSBUJPO .PEFMT class Article < ActiveRecord::Base use_switch_point :blog end !

    class Category < ActiveRecord::Base use_switch_point :blog end ! class Comment < ActiveRecord::Base use_switch_point :comment end
  12. 1SPYZ 5IFSF`TBQSPYZCFUXFFONPEFMTBOEEBUBCBTFT BOEJUEPFT38TQMJUUJOH Article Category Comment blog comment db-blog-master db-blog-slave

    db-comment-master db-comment-slave .PEFM 1SPYZ TXJUDIQPJOU %BUBCBTF
  13. 4XJUDIJOH Model.with_readonly {}$POOFDUUPUIFTMBWF Model.with_writable {}$POOFDUUPUIFNBTUFS with_readonlyBOEwith_writableDBOCFOFTUFE "MNPTUUIFTBNFTFNBOUJDTXJUIfrom_slaveBOE from_masterPGBDUT@BT@SFBEPOMZBCMF

  14. 5ZQJDBMVTBHF 6TFwith_readonlyJOBSPVOE@BDUJPOPG(&5 SFRVFTU 6TFwith_writableGPSXSJUJOHPS4&-&$5GSPN UIFNBTUFS 6TFwith_readonlyGPSTPNFCMPDLTFOEQPJOUT UIBUFYFDVUFTIFBWZRVFSZ

  15. *OQSPEVDUJPO $PPLQBEJTVTJOHTXJUDI@QPJOUGPSUIFNBJO BQQMJDBUJPO -PUTPG"DUJWF3FDPSENPEFMT   JOUPUBM  .VMUJQMFNBTUFSTMBWFEBUBCBTFT 5IPVTBOETPGEBUBCBTFDPOOFDUJPOT

  16. 1PMJDZ ,FFQGFXFSEFQFOEFODJFTPO3BJMTJNQMFNFOUBUJPO /PCSBODIJOHEFQFOEJOHPO ActiveRecord::VERSION 6TFDPNNPOMZVTFEGFBUVSFTBOECFIBWJPST %PPOFKPCŠ38TQMJUUJOH %PJUBUUIFSJHIUMBZFS

  17. $PODMVTJPO *DSFBUFEBTJNQMFHFNUIBUFOBCMFT38TQMJUUJOH JO3BJMT IUUQTHJUIVCDPNFBHMFUNUTXJUDI@QPJOU