Upgrade to Pro — share decks privately, control downloads, hide ads and more …

R/W Splitting in Rails

Kohei Suzuki
September 19, 2014

R/W Splitting in Rails

Kohei Suzuki

September 19, 2014
Tweet

More Decks by Kohei Suzuki

Other Decks in Programming

Transcript

  1. 384QMJUUJOHJO3BJMT
    ,PIFJ4V[VLJ

    View full-size slide

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

    !
    $PPLQBE*OD

    View full-size slide

  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

    View full-size slide

  4. 3BJMTBOENVMUJQMF%#T
    38TQMJUUJOHJT/05TVQQPSUFE
    XSJUFUPUIFNBTUFS
    SFBEGSPNUIFTMBWF
    5IFSF`TTFWFSBMHFNTUPEP38TQMJUUJOHJO3BJMT
    PDUPQVT ECDIBSNFS NVMUJ@EC
    BDUT@BT@SFBEPOMZBCMF

    View full-size slide

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

    .BOZGFBUVSFT MBSHFDPEFCBTF

    View full-size slide

  6. BDUT@BT@SFBEPOMZBCMF
    IUUQTSVCZHFNTPSHHFNTBDUT@BT@SFBEPOMZBCMF
    0MEHFN MBTUVQMPBE

    $PPLQBEVTFEJUVOUJMSFDFOUUJNFT
    )FBWJMZNPEJpFEUPTVQQPSU3BJMT
    %P38TQMJUUJOHPOMZ
    +VTU-0$ BTPGPVS3BJMTTVQQPSUFE
    WFSTJPO

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  10. $POpHVSBUJPO
    *OJUJBMJ[FS
    SwitchPoint.configure do |config|
    config.define_switch_point :blog,
    readonly: :"#{Rails.env}_blog_slave",
    writable: :"#{Rails.env}_blog_master"
    end

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  13. 4XJUDIJOH
    Model.with_readonly {}$POOFDUUPUIFTMBWF
    Model.with_writable {}$POOFDUUPUIFNBTUFS
    with_readonlyBOEwith_writableDBOCFOFTUFE
    "MNPTUUIFTBNFTFNBOUJDTXJUIfrom_slaveBOE
    from_masterPGBDUT@BT@SFBEPOMZBCMF

    View full-size slide

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

    View full-size slide

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

    .VMUJQMFNBTUFSTMBWFEBUBCBTFT
    5IPVTBOETPGEBUBCBTFDPOOFDUJPOT

    View full-size slide

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

    View full-size slide

  17. $PODMVTJPO
    *DSFBUFEBTJNQMFHFNUIBUFOBCMFT38TQMJUUJOH
    JO3BJMT
    IUUQTHJUIVCDPNFBHMFUNUTXJUDI@QPJOU

    View full-size slide