SBE:一条消息如何排布

了解简单二进制编码(SBE)如何在线路上排列每条消息——消息头、固定根块、重复组和变长数据——让交易解码器无需解析就能读取字段。

以 Markdown 查看

简单二进制编码(SBE)是 FIX 交易社区为低延迟交易消息制定的二进制线路格式。交易所和交易公司,包括 CME Group、B3 和 Binance,用 SBE 传送实时行情和报单。与 JSON 或标签-值格式的 FIX 不同,SBE 不在线路上放字段名或标签:一份外部的 XML 消息模式固定了每个字段的字节偏移,解码器可以直接从接收缓冲区读取值,不必先解析前面的字节(二进制消息是怎样组成的)。

本页用 Binance 的一条真实成交流消息,按线路上的确切顺序讲解一条 SBE 消息的四个部分:消息头、固定根块、重复组和末尾的变长数据。消息外面的数据包头或长度前缀属于 SBE 模式之外的分帧。

TradesStreamEvent 的排布

看下面自带的 Binance Spot Streams 示例中的 TradesStreamEvent。文本声明左侧的边栏显示每个字段在所在块中的起始位置(父级偏移)和它的 大小;布局表中的 偏移 从根块第 0 字节起计算每个缓冲区偏移。注意三个部分的顺序:定长字段(从 eventTime 到 qtyExponent),一个重复组(trades),以及变长数据(symbol)。

TradesStreamEvent :: message @10000 {
    eventTime:     utcTimestampUs :: type int64; // UTC timestamp in microsecondsutcTimestampUs;  @1
    transactTime:  utcTimestampUs :: type int64; // UTC timestamp in microsecondsutcTimestampUs;  @2
    priceExponent: exponent8 :: type int8; // Exponent for a decimal floating point number.exponent8;       @3
    qtyExponent:   exponent8 :: type int8; // Exponent for a decimal floating point number.exponent8;       @4
    trades:        group            @100 {
        id:           tradeId :: type int64;tradeId;                        @1
        price:        mantissa64 :: type int64; // 64-bit mantissa for a decimal floating point number. The name of the field containing the exponent value is specified in the mbx:exponent attribute of the mantissa64 field. The exponent field will always precede the mantissa field.mantissa64;                     @2
        qty:          mantissa64 :: type int64; // 64-bit mantissa for a decimal floating point number. The name of the field containing the exponent value is specified in the mbx:exponent attribute of the mantissa64 field. The exponent field will always precede the mantissa field.mantissa64;                     @3
        isBuyerMaker: boolEnum :: enum uint8 // 1 BboolEnum;                       @4
        isBestMatch:  boolEnum :: enum uint8 // 1 BboolEnum const = boolEnum.True; @5
    }
    symbol:        data varString8 :: composite // Variable length UTF-8 string.varString8; @200
}
Binance Spot Streams SBE schema 中的 TradesStreamEvent,按 Market Data Visualizer 文本视图的写法
Binance Spot Streams SBE schema 中 TradesStreamEvent 的布局,按 Market Data Visualizer 的展示方式
偏移大小名称类型出现方式起始版本
018TradesStreamEventblock0
08eventTimeutcTimestampUsrequired0
88transactTimeutcTimestampUsrequired0
161priceExponentexponent8required0
171qtyExponentexponent8required0
186tradesgroup groupSizeEncoding0
182.blockLengthuint16
204.numInGroupuint32
2425[0]entry
248.idtradeIdrequired0
328.pricemantissa64required0
408.qtymantissa64required0
481.isBuyerMakerenum boolEnumrequired0
—0.isBestMatchconst enum boolEnumconstant0
491symboldata varString80

在 Market Data Visualizer 中打开 TradesStreamEvent

消息头

每条 SBE 消息都以一个定长的复合类型消息头开始(通常是 messageHeader),其中有四个字段:

  • templateId:标识消息(等于模式中该消息的 id,TradesStreamEvent 为 10000)。
  • schemaId:标识模式。
  • version:发送方使用的模式版本。
  • blockLength:随后根块的字节长度。

完整的分派步骤见消息头。

根块

消息头之后紧跟着根块:所有定长字段按模式顺序排列,紧密相连,没有隐式的对齐填充(字段与填充)。多字节数字使用模式的 byteOrder(默认小端序;见字节序)。

模式仍可以用三种方式有意加入填充:显式的字段 offset(跳过若干字节,使字段对齐或与某个结构体一致),比字段总长更大的消息 blockLength(根块末尾填充),或比条目字段总长更大的组 blockLength(条目末尾填充)。写入方用零填充这些字节,读取方从不把它们当作数据。

在 Market Data Visualizer 中打开根块的第一个字段 eventTime

块中有四类字段表现特殊:

  • 复合类型:内联排布,成员一个接一个。
  • 集合(例如 Binance Spot API 示例中的 orderTypes):无符号整数中的位字段。
  • 常量(例如 TradesStreamEvent/trades 中的 isBestMatch):值存在模式里,在线路上占零字节。
  • 可选字段(例如 NewOrderAckResponse 中的 orderListId):总是占满自身字节宽度,用保留的空值表示缺失。

读完根块后,按消息头中的 blockLength 前进,而不是按本地模式的块大小,这样新版模式追加的字段会被干净地跳过(模式版本管理)。

组和变长数据

根块之后依次是:

  1. 重复组,按模式顺序排列(变长数据和列表)。每个组以一个维度复合类型开头(dimensionType,包含 blockLength 和 numInGroup,还可以有 numGroups 和 numVarDataFields,让旧解码器跳过未知的嵌套组或数据字段),后面紧接着各个条目。每个条目内部的成员重复消息的三段顺序(定长字段、嵌套组、条目级变长数据)。按线路上的 blockLength 跳过每个条目的定长部分,读取条目前先检查 numInGroup == 0。
  2. 变长数据字段放在最后,每个都是一个长度前缀,后跟相应数量的字节。

组的条目数和数据长度只有在运行时才知道,所以解码器按模式顺序依次遍历组和数据字段。

解码时要检查什么

  • 读取 SBE 消息头之前先去掉交易所的外层分帧;Market Data Visualizer 的布局从根块第 0 字节开始。
  • 用线路上的 blockLength 跳过根块,绝不用编译出的结构体大小。
  • 严格按模式顺序读取组和变长字段。

来源

由 Structure 于 2026-11-26 审阅。 最后修改于 2026-11-23。