by eat_veggies · 6y ago · 0 comments
Coupling your front end and back end in this way can give you false confidence in making API changes. If you control both sides of the API on a web app that doesn't have too many users yet, then this can be very productive, but consider this situation:
You've made a breaking API change on the back end, and thanks to your type system, you make the corresponding front end changes as well, which is great. You deploy your code, and the back end changes take effect immediately. Visitors who were in the middle of a session are still running the old front end code, and their requests start failing. Cached copies of the front end start failing too.
You can engineer around this but it's better to have a system that doesn't make introducing breaking API changes too easy. It should be painful.
If your want to change from A to B, your first deploy is deploying B without stopping A from working. It could be a totally new endpoint, or you could just throw a version field into the request body or querystring. Once everyone has switched off of A, you can remove it from your codebase so only B remains without actually affecting anyone's session.
If you don't want to pollute all your querystrings or request bodies with version fields, you could have an optional newVersion boolean field that only needs to appear during a switchover or make a temporary endpoint for updated clients to hit instead of the old endpoint:
# state at start
/foo <- uses API A
# deploy new endpoint
/foo <- uses API A
/foo/new <- uses API B
# wait for everyone to swtich to /foo/new, then deploy API to /foo, removing support for API A (this could be split into two deploys if you wanted)
/foo <- uses API B
/foo/new <- uses API B
# wait for everyone to switch over to /foo, then remove /foo/new
/foo <- uses API BIMHO that's an easier solution than having to implement a backward compatible API or other API versioning solutions.
I’m curious if anyone has a system for managing this that they love. I’ve pretty much only seen painful ways.
It's not perfect (I've ignored the comments before, thinking I knew better... I didn't), but it seems to help.
It wouldn't be difficult to make it more sophisticated, and then completely block PRs it knew weren't backward compatible, but we haven't seen a strong need to do that yet.
Right, just version the API, or have the app refresh itself when appropriate. Isn't this a fairly straightforward issue to address?
Also, as BiteCode_dev points out, this issue has nothing to do with whether you use a static type system.