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 sr.ht 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 sr.ht 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:

gAAAAABe7-ysKcvmyavwKIT9k1uVLx_GXI6OunjFIHa3OJmK3eBC9NT6507PBr1WbuGtjlZSTYLYvicH2EvJXI1eAejR4kuNExpwoQsogkE9Ua6JhN10KKYzF9kJKW0hA_-737NurotB

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 sr.ht.

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:

gAAAAABe7_K9PeskT6xtLDh_a3JGQa_DV5bkXzKm81gCYqNRV4FLJlVvG3puusCGAwQUrKFLO-4LJc39GBFPZomJhkyqrowsUw==

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=
BARE: EAUxMjM0NQA=

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 {
  ACCOUNTING
  ADMINISTRATION
  CUSTOMER_SERVICE
  DEVELOPMENT

  # 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 {
        panic(err)
    }
    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!

Errata

Articles from blogs I read Generated by openring

Playing osu! Locus 2025

greetings. in this blog post i will compare the musical structure of Twinkle Twinkle Little Star against that of Camellia - crystallized, neither of which are entries in the Locus contestMost of the music in osu! is from outside sources. The Locus (2025) …

via Cadence's Weblog August 20, 2025

Upcoming changes to Hare's event loop library

hare-ev is an important Hare library that provides an event loop for Hare programs, similar to libuv, which most Hare programs which perform asynchronous I/O depend on for that purpose. I’ve been working on some design improvements over the past couple of we…

via Blogs on The Hare programming language July 30, 2025

SourceHut is now accepting payments in Euro

I’m pleased to announce that, as part of our broader plans to migrate SourceHut to Europe, and after many months of hard work, SourceHut has begun to accept subscription payments in Euro today – one of our oldest and most highly demanded feature requests. Th…

via Blogs on Sourcehut July 10, 2025