|
| 1 | +--- |
| 2 | +title: REST vs RPC |
| 3 | +categories: [programming] |
| 4 | +--- |
| 5 | +I think there are a few concepts that have been derailed from their |
| 6 | +original meaning... |
| 7 | + |
| 8 | +In the old world, RPC meant stuff like corba, which was a full blown |
| 9 | +OOP framework for distributed computing I think. I think it sold full |
| 10 | +discoverability of methods and "run things as if they were on client, |
| 11 | +and we'll run it wherever it is, but you, as a client, don't have to |
| 12 | +worry". Reality is it meant it neede full sync of client and server |
| 13 | +codes. And knowing how shaky everything was in those Java 1.0 |
| 14 | +times. Imagine the complexity of Hibernate but for RPC, in 1998. Wow. |
| 15 | +It had binary protocols for calls and return values, and the problems |
| 16 | +of "well... I can't really pass a socket or a pointer through a rpc, |
| 17 | +so it can't be reeaaaallly transparent". So that was transport |
| 18 | +protocol, serialization, discoverability... a lot of things. |
| 19 | + |
| 20 | +Then there was SOAP wbservices (wdsl), which was a way of calling |
| 21 | +remote functions, given by an xml spec that contained the specs of the |
| 22 | +available functions, their parameters and their return values. It was |
| 23 | +already using HTTP in a single `POST /`, but the sessions were authed |
| 24 | +in the beginning, and later you kinda got a "connected client", and |
| 25 | +did operations there. Internally, the wsdl protocol used only a |
| 26 | +single post endpoint and funneled everything through body |
| 27 | +parameters. It serialized and deserialized in client and servers' |
| 28 | +code, but it was transparent to you. It also needed care in clients |
| 29 | +when there were updates in server code/specs. At that time, |
| 30 | +versionning was not very popular yet (in the domains I worked at |
| 31 | +least). |
| 32 | + |
| 33 | +Graphql uses also POST at `/` and they don't give any fucks about |
| 34 | +mapping resources into paths. I see that as a case of "throw my hands |
| 35 | +up in the air, declare bankrupcy and do my own shit from scratch, that |
| 36 | +I know it works, because I'm shoehorning things into protocols and I |
| 37 | +feel stupid when I spent too many hours on this while it should be |
| 38 | +easy to do". |
| 39 | + |
| 40 | +Go has gRPC, which is a thinner framework for rpc and serialization |
| 41 | +using protobuffers, but I think it also requires code synchronization, |
| 42 | +and code generation for the new endpoints. K8s works this way |
| 43 | +internally between the internal services of it, but when it offers an |
| 44 | +external api, it gives a rest api, defined by yamls, that are |
| 45 | +generated from the grpc specs [citation needed]. (hairball-gets-bigger). |
| 46 | + |
| 47 | +The autodisoverability is the same as what OpenAPI provides, but |
| 48 | +openapi is totally loosely coupled with the reality of the api. I can |
| 49 | +publish an outdated openapi that doesn't match the real api. |
| 50 | + |
| 51 | +So there's 2 things |
| 52 | + |
| 53 | +* "transport", whether it's http or not (grpc uses different ports and |
| 54 | + protocols), |
| 55 | +* and there's the fact that clients and servers talk "seamlessly" |
| 56 | + calling back and forth calls. |
| 57 | + |
| 58 | +In a world that the communication is only rpc, in principle you |
| 59 | +woudn't even know if a call happens locally or not. That's cool in |
| 60 | +controlled envs, but still dangerous for delays, network issues, |
| 61 | +etc... |
| 62 | + |
| 63 | +I think making HTTP more explicit was a way towards "microservices", |
| 64 | +and put a narrow waist inbetween programs. Imagine that your app |
| 65 | +connects with a socket to an external server and it has no way talking |
| 66 | +to the server unless you "know the language" of what to send through a |
| 67 | +socket, and what to expect back. I think due to network issues |
| 68 | +concerns, the stateless thing and "retryiability" got more traction. |
| 69 | + |
| 70 | +On REST apis, the focus is on the entities, and it kinda surfaces this |
| 71 | +to the client. Making it more about data and nouns than verbs. Idk if |
| 72 | +this is good or bad, but even when we have those `PUT |
| 73 | +/something/122/actions/do-this` (which is not super RESTy, but it's |
| 74 | +what we have to do when REST is not just about simple CRUD), there's |
| 75 | +an implicit `self` there, which is the `something` object with |
| 76 | +`id=122`. |
| 77 | + |
| 78 | +Nowadays public apis offer things on REST-ish endpoints. And for |
| 79 | +"rpc-like" experience you have sdks, that simulate the rpc experience |
| 80 | +for the developer. (when we use the stripe sdk, it's doing http calls |
| 81 | +beneath, but it does the serialization and checks client <-> server ) |
| 82 | + |
| 83 | +On our particular case, I think we're a tiny grain of sand in the |
| 84 | +desert of the industry, and what we do doesn't matter much, but I |
| 85 | +don't feel the mission of recreating anything. I use the minimum |
| 86 | +amount of features that make my thing work. I use the minimum amount |
| 87 | +of features that make my thing work. But my api is not really REST and |
| 88 | +it's a bit more pragmatic IMHO. We sometimes aggregate many entities |
| 89 | +in the same responses if frontend needs them together in the same |
| 90 | +view. |
| 91 | + |
| 92 | +- the path is a namespace and an "address" for that resource. hopefully stable on time. |
| 93 | +- GET if it's a read and can be retried |
| 94 | +- POST if it's an insert |
| 95 | +- PUT everything else |
| 96 | + |
| 97 | +I find new headers to me every other day (ETag?), and it seems headers |
| 98 | +are used as a backchannel to pass things that "shouldn't be there, but |
| 99 | +are needed, so :man-shrugging:" and the industry agrees that headers |
| 100 | +is where people put their dirty metadata/shit. |
| 101 | + |
| 102 | +Somehow it seems REST is focused on doing less, and gravitate to show |
| 103 | +the data and actions being the "weird thing", and rpc is more based on |
| 104 | +tight coupling and less restrictions. A bit of "worse is better", and |
| 105 | +it would explain why REST won. |
| 106 | + |
| 107 | +Bret Victor's talk about the real way to communicate |
| 108 | +https://youtu.be/8pTEmbeENF4?si=eloQA5l7tvcwd5rD&t=772. |
| 109 | + |
| 110 | +Rich Hickey and Alan Kay discussion on data vs |
| 111 | +interpreters/ambassadors https://news.ycombinator.com/item?id=11945722 |
| 112 | + |
| 113 | +Dylan Beattle talk on REST: https://www.youtube.com/watch?v=g8E1B7rTZBI |
0 commit comments