Trending

World technology news

→
Reconnecting to a warehouse stream with gRPC in ASP.NET Core 3.0
Software Engineering · 2019
Software Engineering2019

Reconnecting to a warehouse stream with gRPC in ASP.NET Core 3.0

A 2019 ASP.NET Core retrospective uses a warehouse stream to explain ordered gRPC messages, durable checkpoints, and predictable reconnection after an interruption.

Archive retrospective · Topic year 2019

Developer's JournalPublished: Nov 27, 20266 min read
Reading progress0%
In this article
  1. What changed for .NET services in 2019?
  2. Give the snapshot a checkpoint
  3. Make message fields carry a stable contract
  4. Exercise the reconnection rule with a small state test
  5. Test the connection and the visible screen together
  6. Define success before expanding rollout

What changed for .NET services in 2019?

Microsoft released .NET Core 3.0 and ASP.NET Core 3.0 on September 23, 2019. Its developer material from the period showed the gRPC service template in Visual Studio 2019 and hosting on ASP.NET Core's Kestrel. gRPC already existed. The change considered here is how a .NET team could put a service contract and its streaming operations in the ASP.NET Core model. Protobuf defines the messages, and generated client and server types give both sides a consistent interface. A warehouse screen illustrates the resulting design question. The screen loads stock at the start of a shift. Receipts and dispatches change stock during the day. Periodic refresh can query the complete list again. A server stream can instead send changes as they occur. When the connection drops, the client still needs a rule for recovering the correct state. This retrospective uses a fictional warehouse dataset to make that rule testable.

Give the snapshot a checkpoint

The opening snapshot includes stock rows and a checkpoint. In the sample the screen reads ten items for SKU A-19 and learns it has the state through event 40. It opens the stream requesting changes after 40. Obtain the rows and checkpoint from a consistent data view. If the checkpoint describes a different instant from the rows, a change at the boundary can be omitted or applied twice. This example assumes consecutive sequence numbers within each warehouse. A centrally allocated identifier shared across warehouses will leave normal gaps in any one warehouse's stream. A client following that design needs a different rule from the contiguous counter used here. Specify the scope before assigning meaning to a gap. Also document how long the service keeps old events. A returning client can request a checkpoint older than the retained history, and that case needs an explicit path to a fresh snapshot.

Service architectureConceptual sketch · summary of the article's technical flow

Make message fields carry a stable contract

The example RPC returns a stream of StockChange messages for one warehouse. The request sends the final checkpoint the client confirms it has applied. Each change carries a sequence, SKU, and the absolute quantity after the change. The server validates access to the requested warehouse. The warehouse identifier expresses what the caller wants to read, while authorization follows the server's application rules. Protocol Buffers field numbers are part of the wire format. Keep their meaning stable and reserve a deleted field number to avoid reusing it accidentally. Check how client libraries represent a 64-bit sequence. JavaScript applications can need an explicit representation for values beyond the safe integer range. The sample's small integer values make the state test easy to run. The service contract should preserve the round-trip representation when serializing, storing, and comparing a real cursor.

syntax = "proto3";

package warehouse.v1;
option csharp_namespace = "Warehouse.V1";

service InventoryFeed {
  rpc Follow(FollowRequest) returns (stream StockChange);
}
message FollowRequest {
  string warehouse_id = 1;
  uint64 after_sequence = 2;
}
message StockChange {
  uint64 sequence = 1;
  string sku = 2;
  uint32 on_hand_after = 3;
}

Exercise the reconnection rule with a small state test

gRPC preserves order within one RPC call. After a disconnect, a new call can resume from the last applied checkpoint defined by the application. In the sample, event 41 sets stock to 12. A second copy of 41 has no further effect. When event 43 arrives and 42 is absent, the client reports a gap and preserves the last confirmed state. It can show the user a reconnecting status before retrying from the durable checkpoint. The Map in the fixture represents a local projection. In a client that saves state to disk, persist the stock change and its checkpoint together. A crash between those writes can leave the stored stock inconsistent with the point from which the client resumes. Test deletes and moves separately when the real message contract supports them. Their duplicate-handling behavior should remain clear when the same change arrives after a reconnect.

function applyChange(state, message) {
  if (message.sequence <= state.sequence) return "duplicate";
  if (message.sequence !== state.sequence + 1) return "gap";
  state.stock.set(message.sku, message.onHandAfter);
  state.sequence = message.sequence;
  return "applied";
}

const screen = { sequence: 40, stock: new Map([["A-19", 10]]) };
const first = { sequence: 41, sku: "A-19", onHandAfter: 12 };
console.log(applyChange(screen, first)); // applied
console.log(applyChange(screen, first)); // duplicate
console.log(applyChange(screen, { sequence: 43, sku: "A-19", onHandAfter: 9 })); // gap
console.log(screen.sequence, screen.stock.get("A-19")); // 41 12

Test the connection and the visible screen together

Deliberately close the first call after applying event 41. A reconnect reporting 41 should begin with 42. A request using the previous checkpoint 40 can repeat 41, which the client recognizes. In a separate test, use a checkpoint older than the server's retained event stream. The service should signal that the client needs a new snapshot. The screen can show the last confirmed value with a clear freshness status while recovery runs. Cancel an open call when the viewer closes the screen. Test idle limits and forwarding behavior through the same kind of intermediary the deployment will use. Record how often clients reconnect, how many messages queue, and how long a change takes to become visible. Give the practical client a bounded queue and a defined recovery path. The experience when that bound is reached matters as much as a successful connection in a quiet development environment.

Define success before expanding rollout

A successfully reopened connection is one part of recovery. The operator should also see a valid last-confirmed quantity, understand when it may be stale, and avoid duplicated effects when an event is replayed. A pilot warehouse can compare both screens against the same SKU and sequence. When a discrepancy appears, the team can trace the snapshot and follow the ordered messages that came after it. ASP.NET Core 3.0 gave .NET teams a way to express this service as a gRPC contract in 2019. The inventory example works when that contract has explicit rules for starting state, ordering, retention, and duplicate handling. Those rules can stay testable even if the client interface or persistence technology changes.

Be the first reader ✎ Write a comment

Reader discussion

Discussion prompt

Which data would you verify before applying this development in your field?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.