Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:05:14+00:00 2026-05-28T08:05:14+00:00

I have a Flask, SQLAlchemy webapp which uses a single mysql server. I want

  • 0

I have a Flask, SQLAlchemy webapp which uses a single mysql server. I want to expand the database setup to have a read-only slave server such that I can spread the reads between both master and slave while continuing to write to the master db server.

I have looked at few options and I believe I can’t do this with plain SQLAlchemy. Instead I’m planning to create 2 database handles in my webapp, one each for master and slave db servers. Then using a simple random value use either the master/slave db handle for "SELECT" operations.

However, I’m not sure if this is the right way to go with using SQLAlchemy. Any suggestion/tips on how to pull this off?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:05:15+00:00Added an answer on May 28, 2026 at 8:05 am

    I have an example of how to do this on my blog at http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/ . Basically you can enhance the Session so that it chooses from master or slave on a query-by-query basis. One potential glitch with that approach is that if you have one transaction that calls six queries, you might end up using both slaves in one request….but there we’re just trying to imitate Django’s feature 🙂

    A slightly less magic approach that also establishes the scope of usage more explicitly I’ve used is a decorator on view callables (whatever they’re called in Flask), like this:

    @with_slave
    def my_view(...):
       # ...
    

    with_slave would do something like this, assuming you have a Session and some engines set up:

    master = create_engine("some DB")
    slave = create_engine("some other DB")
    Session = scoped_session(sessionmaker(bind=master))
    
    def with_slave(fn):
        def go(*arg, **kw):
            s = Session(bind=slave)
            return fn(*arg, **kw)
        return go
    

    The idea is that calling Session(bind=slave) invokes the registry to get at the actual Session object for the current thread, creating it if it doesn’t exist – however since we’re passing an argument, scoped_session will assert that the Session we’re making here is definitely brand new.

    You point it at the “slave” for all subsequent SQL. Then, when the request is over, you’d ensure that your Flask app is calling Session.remove() to clear out the registry for that thread. When the registry is next used on the same thread, it will be a new Session bound back to the “master”.

    Or a variant, you want to use the “slave” just for that call, this is “safer” in that it restores any existing bind back to the Session:

    def with_slave(fn):
        def go(*arg, **kw):
            s = Session()
            oldbind = s.bind
            s.bind = slave
            try:
                return fn(*arg, **kw)
            finally:
                s.bind = oldbind
        return go
    

    For each of these decorators you can reverse things, have the Session be bound to a “slave” where the decorator puts it on “master” for write operations. If you wanted a random slave in that case, if Flask had some kind of “request begin” event you could set it up at that point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python/Flask webapp with MongoDB database on DotCloud hosting. I would like
I have a Flask web application using SQLAlchemy with MySQL, and I have set
I have flash (AIR) application running on a single controlled system (there is only
I have installed virtualenv along with flask, werkzeug, jinja2 and SQLAlchemy. I was following
I have just started building my first Flask app, which currently simply returns output
I have written a small webapp using the flask framework that involves plotting using
I have a flash program that loads movie clips dynamically and sometimes they want
I've set up a Flask application to run on a tornado server backed by
I am writing my website's backend using Flask and Python 2.7, and have run
Heroku proxies requests from a client to server, so you have to parse the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.