by djha-skin · 3y ago · 0 comments
Consider writing an HTTP based RPC server. I parse the first line. It tells me the location and the method. Followed by a bunch of key value pair headers where both the key and the value are strings. So far so good, I could write all of the above in C or Go pretty easily. Finally comes the body. I can unmarshall this in Go or parse this in C easily because I can I know what to expect in the body by the time I get there. This is because I already know the method and location.
Contrast this with JSON RPC. I have to partially parse the entire object before I know what function is being called. Only then can I fully parse the arguments because I don't know what type they are (or in other words, which struct the arguments correspond to) until I know what function is being called, what version of RPC is being used, etc.
Super annoying. And HTTP is just sitting there waiting to be used.
HTTP allows for incremental parsing. I can parse the first few lines separately from the body. It makes handling input really nice.
Having everything in a single JSON object doesn't allow for incremental parsing because the standard says I can't guarantee order of key value pairs when an object is concerned.
This gives you the advantage of load balancers being able to fast drop misbehaving clients for load shedding. You also have the security advantage of authenticating a legitimate client before handing off the payload to a parser (both as a DoS vector, or the risk of parsing untrusted data).
The JSON RPC spec says nothing about authentication, you can use whatever authentication method you wish.
This is a variation of Hyrum's Law: With a sufficient number of users of an API, it does not matter what you promise in the contract: all observable behaviors of your system will be depended on by somebody. https://www.hyrumslaw.com
Really? For a JSON-RPC over HTTP API? Where?
If you are using a generic JSON-RPC framework (does that exist?) which supports multiple transports then credentials in the request object seems like a good optional feature but surely you could disable it and stick your regular HTTP auth middleware in front?
Once one had the body - which will typically be read in one go for usual RPC use cases - it’s as easy to deserialize json as anything else that could be transferred there.
E.g. I've implemented a simple indexed binary format for JSON-like data: https://github.com/7mind/sick
The problems with JSON-RPC are mostly in its design, it's not ergonomic and its type system is a joke.
There are many better alternatives ranging from gRPC (still not ergonomic and not modular) to Protoforce (extremely powerful and ergonomic but only marginally adopted).
It's the overhead of having to parse it to a generic JSON-like data structure at all, before knowing what the keys and values mean, so you can then transform the JSON-like data structure to the program's API structs.
If you knew the shape of the RPC method before parsing the JSON body, the body would be parsed directly to target language structs and variables, without using any generic key-value objects (dicts/hashes/maps), any generic arrays, or even any strings in some cases (when JSON strings represent enums). For many RPCs there would be no memory allocations during JSON body parsing. But you don't know the shape, so you either have to allocate memory and store the input in key-value objects, arrays and strings temporarily, or parse the JSON body in two passes, the first pass to get the shape for the second pass.
You don't have to do that.
> If you knew the shape of the RPC method
The whole idea of RPC is about "knowing the shape" (typing) and good RPC implementations do that.
> you either have to allocate memory and store the input in key-value objects
If I use JSON-like data structures I don't have to use classic parsers with intermediate AST representations.
> or parse the JSON body in two passes
I've shown you a small library which allows me not to parse anything at all.
I looked at your repo. It doesn't implement JSON-RPC or parse JSON-RPC requests, so I guess you are talking about alternatives to JSON-RPC. An RPC protocol using your library's SICK format may well be efficient (though I'm not sure it's efficient to serialise).
When I saw your initial reply I thought your were saying the criticism of JSON-RPC was incorrect in the comment you were replying to. But now I realise you were saying a different protocol not using JSON can be more efficient than JSON-RPC, using a differently request format which could be used as a drop-in replacement for JSON in JSON-RPC.
> If I use JSON-like data structures I don't have to use classic parsers with intermediate AST representations.
Indeed, but JSON-like data structures are potentially in the "too much overhead" category by themselves, regardless of whether a classic parser is used. Parsing per se isn't necessarily the main overhead: JSON itself can be streamed as tokens. Even with your SICK format, the data structure has to be converted to target language structs, enums, etc, which is a type of parsing, even though it's not parsing text.
> The whole idea of RPC is about "knowing the shape" (typing) and good RPC implementations do that.
Indeed. The type of a JSON-RPC request is known only when the "method" key is read, and that key can occur anywhere in the top-level object. Start, middle or end. Finding "method" requires at minimum a pattern-matching scan of the JSON body. Until that's done there's no way to know what types the other values in the JSON body are to be mapped to in the API's target language.
So we can agree good RPC implementations "know the shape" when reading a request body, and JSON-RPC doesn't have that property.
(A variant of JSON-RPC which uses an array with the method in the first element does have that property though. That one has the merit of the high level of cross-language tool compatibility due to JSON, a 1:1 correspondence with JSON-RPC requests, plus the capability to serialise/parse directly from/to target API structs, variables, enums etc in whatever language without requiring any intermediate JSON-like data structure.)
JSON can be parsed linearly so long as every key fully determines the format of the value.
1. Client libraries may not send keys in a defined order, so the key that tells the server what method to call might end up in the middle or end of the payload.
2. The vast majority of default or common JSON parsers out there for the various languages and frameworks most people use just don't support incremental parsing.
Your post isn't particularly relevant, since it proscribes a different way of structuring your payloads than JSON-RPC (and others). It doesn't matter if these others are "wrong"; they exist, and have wide support, so people will gravitate to them, rather than rolling their own.
You could probably combine it with a partial (proved) HTTP parser and get max efficiency (at the cost of abstraction-breaking layer fusion...).
Most typed languages give you two options for parsing JSON:
- 1. General JSON values (hash maps of JSON scalar values).
- 2. Strongly typed - convert JSON string to a specific type that you defined in your application code.
You could use 1 to then use in a switch statement to branch to 2.
You may have to parse the JSON twice in some languages to do this, but JSON is typically small and fast to parse.