The PutEvents operation accepts multiple custom events in a single request, but the total size of all events in the request must not exceed 256 KB (262,144 bytes). Calculate the size of each CloudEvent before batching to stay within this limit.
How event size is calculated
A CloudEvent consists of metadata attributes and a data payload. The total size equals the sum of the UTF-8 byte length of each attribute plus the raw byte length of the data field.
| Attribute | Size calculation |
|---|---|
time | Generally 36 bytes |
specversion | UTF-8 encoded byte length |
id | UTF-8 encoded byte length |
type | UTF-8 encoded byte length |
source | UTF-8 encoded byte length |
subject | UTF-8 encoded byte length |
dataschema | UTF-8 encoded byte length |
datacontenttype | UTF-8 encoded byte length |
data | Raw byte array length (byte[]) |
Sample code
Calculate the size of a single CloudEvent (Java):
int getSize(CloudEvent event) {
int size = 0;
// time is generally a 36-byte timestamp when present
if (event.getTime() != null) {
size += 36;
}
// Metadata attributes: measure UTF-8 encoded byte length
size += event.getSpecversion().getBytes(StandardCharsets.UTF_8).length;
size += event.getId().getBytes(StandardCharsets.UTF_8).length;
size += event.getType().getBytes(StandardCharsets.UTF_8).length;
size += event.getSource().toString().getBytes(StandardCharsets.UTF_8).length;
size += event.getSubject().getBytes(StandardCharsets.UTF_8).length;
size += event.getDataschema().toString().getBytes(StandardCharsets.UTF_8).length;
size += event.getDatacontenttype().getBytes(StandardCharsets.UTF_8).length;
// Data payload: measure raw byte array length
size += event.getData().length;
return size;
}