With no limit on how many streams you can create, many of the applications built on S2 model their data as a stream per entity (per agent session, per device, per collaborative document, per user or per job) rather than a handful of big topics that everything gets multiplexed into. Each of those streams is its own durable, ordered log that can be read from any position, retained on its own schedule, and deleted once it is done. So the number of streams grows with your users and sessions rather than with your infrastructure!
Offering that as a serverless service means the complexity lands on our side, and we have to manage it without wrecking our unit economics. On S3, that mostly comes down to request costs, and packing small writes from many streams together cut the PUTs they cost us by over 100×.
The writes across those streams are anything but uniform. A few streams can be hot, like a busy firehose, a popular room or an agent in the middle of a long task, and push data continuously. Others can be quiet: for example, an agent session exchanges occasional messages between long pauses, a sensor reports a reading every few seconds, or a document receives sporadic edits.
Here bytes and writes are split very differently:
- Most of the data comes from a handful of hot streams.
- Most of the writes to object storage would come from the many quiet streams, because any stream with new data has to be written out on a regular schedule, even if it only wrote a few kilobytes.
Tiering to object storage
Records are written twice. They are first made durable in chunk storage, a shared write-ahead log that combines writes from many streams into chunks, and then tiered into object storage in the background as "segments".
A segment is cut once it is large enough or a timer fires. The simplest design writes each one to object storage as its own object, and we do this for hot streams. A hot stream reaches the size limit within seconds, so its segments are large. A quiet stream almost always hits the timer first, so every interval it cuts a segment of just a few kilobytes.
Paying per request
On S3, you pay for storage by the byte and for every request you make, and a PUT costs the same whether it carries 2 KiB or 200 MiB. A hot stream spreads that cost over megabytes of data, while a quiet stream pays it for every few kilobytes.
When we look at a week of segment sizes from one of our cells, the skew is easy to see. The median segment is around 14 KiB, about 70× smaller than a 1 MiB one, while the 99th percentile ranges from about 0.5 MiB to 5.1 MiB.
Consider a quiet stream that tiers a 14 KiB segment every 20 seconds. That is about 131,000 PUTs a month, which at the S3 Standard price of $0.005 per thousand requests comes to $0.66 per stream, per month in requests alone, compared to roughly $0.04 a month to store those 1.8 GiB of data 🫠. At 100,000 quiet streams, that's $65,700 a month in PUTs.
Batching across streams
So why not just tier quiet streams less often? Waiting minutes instead of seconds would definitely cut the request bill, but a chunk sticks around until every stream in it has been tiered, and holding quiet streams back has two downsides:
- Memory. Chunks also live in memory, so a quiet stream pins whole shared chunks in limited RAM, not just its own data.
- Crash recovery. More untiered chunks means more to replay after a crash, so recovery takes longer.
So instead of batching one stream over time, we batch across streams. At any given moment, lots of quiet streams have a small segment waiting to be tiered, and they can all go into one object, much like Git packfiles or Facebook's Haystack photo store.
Packing segments
We gather small segments from many streams into a batch and upload them together as one packed object, with a single PUT. Each batch is cut on a size-or-timer trigger too (16 MiB or 4 seconds), except pooling across streams makes it much easier to hit the size limit before the timer fires. Large segments are still written as their own object.
Even at a conservative 50 streams per object, packing cuts that $0.66 to about $0.013 per stream, per month, well below the cost of storing the bytes. Across 100,000 streams, $65,700 becomes $1,314.
Most of the remaining PUTs are standalone large segments from hot streams, while PUTs for the small segments that get packed drop by over 100×.
Packing failures may leave behind unused objects to clean up later, but never result in data loss. A prefix of chunk storage is only trimmed after every record in it has been safely persisted to object storage, and we verify these guarantees through deterministic simulation testing.
Reading a packed segment
So how do you read one stream's data out of an object full of other streams? A packed segment's metadata records its packed object and byte range. Reading it is a ranged GET for exactly those bytes.
Caches usually work in large pages, and a large page of a packed object is mostly other streams' data. Our open-source cache, cachey, caches exact byte ranges for packed objects and whole pages for everything else, so a reader only pulls and caches its own segment.
Packed objects are internal storage. Reads still go through access control for the stream and are verified against the segment's own checksum. If a stream uses encryption, its records stay encrypted inside the packed object too.
What's the catch?
Packing fixes the write request problem, leaving two smaller ones:
- Space amplification. A packed object can only be deleted once every segment in it is gone, so one segment with long retention keeps its whole object around.
- Read amplification. Reading a quiet stream's long history still means many small
GETs across many objects, because packed objects group data by write time rather than by stream. That is no worse than one object per segment, but packing alone doesn't fix it.
Compaction can address both. For example, once the live data in packed objects drops below a threshold, it copies their surviving segments into a new object, grouped by stream, so the old ones can be deleted and long reads need fewer, larger GETs. As with LSM compaction, deciding when to compact trades rewrite cost against space and read amplification, so the right policy depends on retention and read patterns. We have improvements planned here as those patterns evolve.
A stream that writes a few kilobytes a minute should cost about as much as a few kilobytes, and packing is one of the ways we make that true!
