Introducing the BARE message encoding June 21, 2020 on Drew DeVault's blog

I like stateless tokens. We started with stateful tokens: where a generated string acts as a unique identifier for a resource, and the resource itself is looked up separately. For example, your OAuth token is a stateful token: we just generate a random number and hand it to you, something like “a97c4aeeec705f81539aa”. To find the information associated with this token, we query the database — our local state — to find it.

Click here to skip the context and read the actual announcement ->

But, increasingly, we’ve been using stateless tokens, which are a bloody good idea. The idea is that, instead of using random numbers, you encode the actual state you need into the token. For example, your login session cookie is a JSON blob which is encrypted and base64 encoded. Rather than associating your session with a record in the database, we just decrypt the cookie when your browser sends it to us, and the session information is right there. This improves performance and simplicity in a single stroke, which is a huge win in my book.

There is one big problem, though: stateless tokens tend to be a lot larger than their stateful counterparts. For a stateful token, we just need to generate enough random numbers to be both unique and unpredictable, and then store the rest of the data elsewhere. Not so for a stateless token, whose length is a function of the amount of state which has been sequestered into it. Here’s an example: the cursor fields on the new GraphQL APIs are stateless. This is one of them:


A whopping 141 characters long! It’s hardly as convenient to lug this monster around. Most of the time it’ll be programs doing the carrying, but it’s still annoying when you’re messing with the API and debugging your programs. This isn’t an isolated example, either: these stateless tokens tend to be large throughout

In general, JSON messages are pretty bulky. They represent everything as text, which can be 2x as inefficient for certain kinds of data right off the bat. They’re also self-describing: the schema of the message is encoded into the message itself; that is, the names of fields, hierarchy of objects, and data types.

There are many alternatives that attempt to address this problem, and I considered many of them. Here were a selected few of my conclusions:

There were others, but hopefully this should give you an idea of what I was thinking about when evaluating my options.

There doesn’t seem to be anything which meets my criteria just right:

The solution is evident.

xkcd comic 927, “Standards”

BARE: Binary Application Record Encoding

BARE meets all of the criteria:

Stateless tokens aren’t the only messages that I’ve wanted a simple binary encoding for. On many occasions I’ve evaluated and re-evaluated the same set of existing solutions, and found none of them quite right. I hope that BARE will help me solve many of these problems in the future, and I hope you find it useful, too!

The cursor token I shared earlier in the article looks like this when encoded with BARE:


100 characters (41 fewer than JSON), which happens to be the minimum size of a padded Fernet message. If we compare only the cleartext:

JSON: eyJjb3VudCI6MjUsIm5leHQiOiIxMjM0NSIsInNlYXJjaCI6bnVsbH0=

Much improved!

BARE also has an optional schema language for defining your message structure. Here’s a sample:

type PublicKey data<128>
type Time string # ISO 8601

enum Department {

  # Reserved for the CEO
  JSMITH = 99

type Customer {
  name: string
  email: string
  address: Address
  orders: []{
    orderId: i64
    quantity: i32
  metadata: map[string]data

type Employee {
  name: string
  email: string
  address: Address
  department: Department
  hireDate: Time
  publicKey: optional
  metadata: map[string]data

type Person (Customer | Employee)

type Address {
  address: [4]string
  city: string
  state: string
  country: string

You can feed this into a code generator and get types which can encode & decode these messages. But, you can also describe your schema just using your language’s existing type system, like this:

type Coordinates struct {
    X uint  // uint
    Y uint  // uint
    Z uint  // uint
    Q *uint // optional<uint>

func main() {
    var coords Coordinates
    payload := []byte{0x01, 0x02, 0x03, 0x01, 0x04}
    err := bare.Unmarshal(payload, &coords)
    if err != nil {
    fmt.Printf("coords: %d, %d, %d (%d)\n", /* coords: 1, 2, 3 (4) */
        coords.X, coords.Y, coords.Z, *coords.Q)

Bonus: you can get the schema language definition for this struct with schema.SchemaFor(coords).

BARE is under development

There are some possible changes that could come to BARE before finalizing the specification. Here are some questions I’m thinking about:

Feedback welcome!


Have a comment on one of my posts? Start a discussion in my public inbox by sending an email to ~sircmpwn/ [mailing list etiquette]

Articles from blogs I read Generated by openring

SourceHut API 2.0 dev log

Completing the project now known as “API 2.0” is one of the most important steps in finalizing the beta, and I’ve been trying to keep the community abreast of developments, especially on the mailing list. I’m summarizing for the blog as well t…

via Blogs on Sourcehut September 25, 2020

People expect technology to suck because it actually sucks

I get at least 27 tech annoyances on a single day

via September 25, 2020

Rewriting the Technical Interview

Frigitt, vi danser For et øyeblikk, vi leker Vi tusen små bladskiper Gleder oss, på det klare morgenlys The mothercone unfurls her softening scales, and in the time of days I am emancipated. Adrift in the chill dawn light, my siblings flutter, slender forms c…

via Aphyr: Posts September 18, 2020