Why Squirix shed its web stack
The first versions of Squirix, a distributed cache for .NET, ran as an ASP.NET Core app. Kestrel accepted connections, and a shared .proto file told the C# client and server how to talk to each other over gRPC. That saved me time when I was working out which replica could safely acknowledge a write.
For 0.2.0-preview.1, I wanted to look at a cheaper operation: reading a short string that's already in memory. Even on a cache hit, the server has to decode the request, look up the key and send the value back. I wanted to be able to change how Squirix did that last part, right down to when it flushed the connection.
The change happened in stages. For a while, Squirix was running a binary protocol over TLS, hosted by Kestrel, with Protobuf still encoding the messages.
Four reads on one connection
The SDK was the first piece to move off gRPC. Consider what happens when four small reads finish on the same connection. If all four replies are ready, the server can write them into the output buffer and flush once. If only one is ready, it can go out on its own. There's no timer holding up that first reply in case another one comes along.
There was still gRPC traffic inside the cluster. Nodes used it to talk to each other even after the SDK had switched protocols. Moving that traffic to a separate binary connection with mutual TLS removed the remaining server-side gRPC dependency. It also changed the upgrade procedure: all nodes in a cluster have to switch together.
What replaced Kestrel
At that point Squirix still started as a web application. In 0.2.0-preview.1, the node opens its own sockets and uses .NET's SslStream for TLS. The SDK connects to one listener and cluster peers connect to another. Health checks and Prometheus metrics still use HTTP/1.1 over TLS, on a separate management address.
Dropping the ASP.NET Core shared framework took more work than replacing the listener. The node now starts and stops its own background workers, manages service lifetimes and handles logging. Its readiness checks cover journal recovery and replica state. Bearer-token validation also moved out of ASP.NET Core's JWT middleware.
One place you can see the difference is the Dockerfile. The final server image uses dotnet/runtime:10.0.15. You can also start a node inside a console app with SquirixServer.StartAsync.
If your own application uses ASP.NET Core, you can still connect through IDistributedCache. The integration package keeps the Microsoft abstractions the application needs.
What a cache miss looks like on the wire
After the move to sockets, the SDK and server still used Protobuf. I later removed the cache and replication .proto files, the gRPC build tools and the Google.Protobuf runtime. Squirix now has its own codec for cache requests and replies.
Take GetValueAsync. For a small reply, the server writes an eight-byte header containing the request ID, status and body length. When a lookup succeeds but the key is missing, the body length is zero. Those eight bytes are the whole cache reply before TLS framing.
When the key holds a small value, the writer reserves room in the output pipe for the header and the encoded response body. It writes both into that space. This step needs no temporary byte array for the encoded reply.
What the allocation numbers tell us
During the migration, three CI runs on Ubuntu with .NET 10 measured managed allocations for calls through the public SDK. These two cases read and wrote short strings against one node with persistence off.
| Operation | Previous CI budget | Average across three runs |
|---|---|---|
| Read a value | 10,530 B/op | 3,217 B/op |
| Write a value | 11,780 B/op | 3,787 B/op |
The server was already using the binary transport when CI recorded those numbers. It still used Protobuf. The left column is the previous allocation budget, so the table records why I could lower that budget. It doesn't tell me how much time the codec change saved on a read. To answer that, I'd time a complete SDK call with one read in flight, then repeat with several reads sharing a connection.
I’d use gRPC again for an early version of a project like this. It let me work on replication while the cache was taking shape. In Squirix today, I want to be able to change how a cache miss is encoded and when a reply goes out. Owning the protocol gives me that control. It also makes a client disconnecting halfway through a frame my problem to handle.
Links
- Release notes and upgrade details
- The server loop that writes and flushes replies
- The binary codec change
- Allocation budgets, methodology and CI runs
Originally published on the Squirix blog.