2 ms·
Rather than using their suggested "catch writes and throw an exception" mechanism, I would instead write my own fast application logic to identify if something
by simonw 5y ago
Rather than using their suggested "catch writes and throw an exception" mechanism, I would instead write my own fast application logic to identify if something is likely to be a write.
For most of the applications I build the HTTP verb is good enough for this - so I would add a tiny piece of Django middleware which looks for a POST to a non-primary region and sends fly-replay straight away at that point.
- chachra 5y agowould one do it in the custom db router itself instead?
- mrkurt 5y agoThis is a good way to do it. Catching errors lets us reliably ship a library that makes this work for almost everyone, but it's not right for all apps: https://github.com/soupedup/fly-rails/blob/main/lib/fly-rails/rails.rb#L6-L9 https://github.com/soupedup/fly-rails/blob/main/lib/fly-rail...
- kenrose 5y agoCries in GraphQL (where you generally POST, even for queries).
- simonw 5y agoGraphQL might work OK though, because it differentiates queries from mutations - so you could have some early logic that says "if this POST request includes a mutation, replay against the leader - otherwise keep running against the replica".
- mrkurt 5y agoIt's true! We designed this specifically because it works with GraphQL. The exception->replay works by default, and it's easy to send early replay command on GraphQL mutations.
- valzam 5y agoWhat is more you could just have your client send all mutations to one API and all queries to another. I suppose you could do that with REST as well by in Graphql the deliniation is very clear and there are only 2 cases you need to handle.
- kortex 5y agoA standard QUERY verb should fix this. https://www.w3.org/2012/ldp/wiki/Proposal_for_HTTP_QUERY_Verb https://www.w3.org/2012/ldp/wiki/Proposal_for_HTTP_QUERY_Ver...
- VWWHFSfQ 5y agoWe follow this pattern as well with a CloudFront -> OpenResty/Nginx router for the different HTTP methods. I've often wondered by CloudFront can't route to custom origins based on request method. You have to do it with a Lambda@Edge function, which is annoying.
- yashap 5y agoI guess where it often gets complex, even with RESTful APIs, is: - Deletes, puts and patches are basically guaranteed to be writes - Get is GENERALLY read only, but it often updates caches - and your cache may have similar concerns. Also, sometimes gets write to the DB, i.e. updating a “last seen/last activity” type field - Posts are generally writes, but definitely not always. Any time you really need a body for a read (big request coming from a browser, or just too much structure in the args to encode in HTTP query params), that read will be a post I like your approach of short-circuiting and immediately sending fly-replay, but instead of doing it by HTTP method, I’d probably do it by manually marking endpoints as write endpoints. And then I’d also have a catch-all similar to the blog post, based on DB errors, that both sends fly-replay and logs. And then keep track of that log, if it ever happens that’s a sign that you need to mark a new endpoint as a writer.