Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to have a master/replica DB configuration #2223

Merged
merged 1 commit into from
Jun 2, 2016
Merged

Conversation

pvalsecc
Copy link
Contributor

@pvalsecc pvalsecc commented May 12, 2016

Basically, this guy switches the DB connection to a replica (readonly) DB if the HTTP method is GET or OPTIONS.

@tsauerwein
Copy link

The session is bound to a request, so binding the connection would work (like you say, assuming that no writes are done in GET requests). You are proposing to use a DNS to support multiple read-only instances. We could also allow to specify multiple read-only connections and then use a simple random load-balancing strategy to avoid the name-resolution step.

Maybe you want to add some documentation on how to configure the actual replication in Postgres?

And on a side node: It would be great if we could avoid a historically racially charged term and maybe use "primary - replica/read-only" instead (for example like Drupal or CouchDB).

@pvalsecc
Copy link
Contributor Author

pvalsecc commented May 17, 2016

@tsauerwein, slavery is not necessarily linked to races. Romans were enslaving their own, sometimes. But OK, I'm removing references to our dark past (and present) and I'm using Postgres' other terms: master/replica. And added a link to their doc for how to set it up.

Regarding the DNS or multiple addresses, after discussion with our IT experts, it's not enough. If you want failover, you need to setup two HAProxy:

  • one for the master with the functionality to elect a new master and point to it in case of failure
  • one for the replicas

I've added that to the doc, but without much details. It's out of my league/responsibilities ;-)

@pvalsecc
Copy link
Contributor Author

@fredj, I've heard you've worked on that problem in the past. Any opinion?

@pvalsecc pvalsecc changed the title [RFC] Allow to have a master/slave DB configuration [RFC] Allow to have a master/replica DB configuration May 17, 2016
@tsauerwein
Copy link

@tsauerwein, slavery is not necessarily linked to races. Romans were enslaving their own, sometimes. But OK, I'm removing references to our dark past (and present) and I'm using Postgres' other terms: master/replica. And added a link to their doc for how to set it up.

Thanks! 👍

@fredj
Copy link
Member

fredj commented May 17, 2016

We created 2 distinct engines (one for reads and one for writes) and we used one or the other depending on the operation.
It was not automatic but the principle is the same

@sbrunner
Copy link
Member

I don't found the therm replica relay useful, every body know whats a master-slave architecture is and we don't do philosophy...

@pvalsecc pvalsecc changed the title [RFC] Allow to have a master/replica DB configuration Allow to have a master/replica DB configuration May 23, 2016
Basically, this guy switches the DB connection to a replica (readonly) DB
if the HTTP method is GET or OPTIONS.
@tsauerwein
Copy link

...like you say, assuming that no writes are done in GET requests.

Here is an example of a GET request that also writes to the database:

@view_config(route_name="shortener_get")
def get(self):
ref = self.request.matchdict["ref"]
short_urls = DBSession.query(Shorturl).filter(Shorturl.ref == ref).all()
if len(short_urls) != 1:
raise HTTPNotFound("Ref '%s' not found" % ref)
short_urls[0].nb_hits += 1
short_urls[0].last_hit = datetime.now()

@pvalsecc
Copy link
Contributor Author

@tsauerwein, you should look at the last version of my patch. I've added the possibility to force the usage of the master or the replica for some URLs. And the shortener is part of it.

@tsauerwein
Copy link

I've added the possibility to force the usage of the master or the replica for some URLs. And the shortener is part of it.

Perfect, I missed that.

@pvalsecc
Copy link
Contributor Author

pvalsecc commented Jun 1, 2016

So... what now? Merge or not?
To: @tsauerwein @sbrunner

@tsauerwein
Copy link

LGTM

@pvalsecc pvalsecc merged commit 0af0588 into 2.0 Jun 2, 2016
@tsauerwein tsauerwein deleted the master_slave branch June 3, 2016 09:40
@sbrunner sbrunner added this to the 2.0 milestone Jun 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants