Watch
Watch allows you to build event-driven systems by subscribing to changes in real time.
Instead of polling for updates, your application receives events whenever data changes.
Why Watch Mattersβ
In traditional systems, you often:
- Poll for changes repeatedly
- Waste resources checking state
- Introduce delays in reacting to updates
Watch replaces this with:
Push-based updates β instant reaction
How Watch Worksβ
You subscribe to changes using a handler:
- Events are delivered when data changes
- You process them in real time
- Subscription continues until stopped
Watching a Single Keyβ
var (subscription, initialRecord) = await client.Watch.WatchKeyAsync(
"order:1001",
new WatchOptions { IncludeInitialSnapshot = true },
ev =>
{
Console.WriteLine($"{ev.EventType} β {ev.Key} = {ev.Value}");
});
Initial Snapshotβ
If IncludeInitialSnapshot = true:
- You immediately receive the current value
- Then real-time updates follow
Snapshot β then stream of changes
Example Flowβ
- Key already exists
- You start watching
- You receive initial value
- Future updates are streamed
Watching a Prefixβ
var subscription = await client.Watch.WatchPrefixAsync(
"orders:",
new WatchOptions { IncludeInitialSnapshot = false },
ev =>
{
Console.WriteLine($"{ev.Key} changed");
});
- Watches all keys matching the prefix
- Useful for groups of data
Event Modelβ
Each event (WatchEvent) contains:
Keyβ affected keyValueβ new value (if applicable)EventTypeβPutorDeleteRevisionβ change version
Example:
Put β key updated or created
Delete β key removed
Example: Reacting to Changesβ
await client.Watch.WatchPrefixAsync(
"jobs:",
null,
ev =>
{
if (ev.EventType == WatchEventType.Put)
{
Console.WriteLine($"Job updated: {ev.Key}");
}
});
Subscription Lifecycleβ
Watch returns a subscription handle.
await subscription.StopAsync();
- Stops receiving events
- Releases resources
Important Behaviorβ
1. Only Matching Keys Trigger Eventsβ
From your test:
- Watching a key β only that key triggers events
- Watching a prefix β only matching keys trigger events
Unrelated keys are ignored
2. Snapshot + Streamingβ
With snapshot enabled:
Initial state β then live updates
Without snapshot:
Only future changes
3. Multiple Watchers Are Isolatedβ
Different watchers:
- Do not interfere
- Only receive relevant events
Example:
prefixA watcher β only prefixA keys
prefixB watcher β only prefixB keys
β Verified in tests
4. Events Are Delivered Continuouslyβ
- Watch runs until explicitly stopped
- Designed for long-running subscriptions
Thread Safetyβ
Your handler may be called concurrently.
Use synchronization if needed:
lock (events)
{
events.Add(ev);
}
β This pattern is used in tests
When to Use Watchβ
Use watch when you need:
- Real-time updates
- Event-driven workflows
- Reactive systems
- Monitoring changes
- Eliminating polling
Common Use Casesβ
1. Job Processingβ
- Watch for new jobs
- Process immediately
2. Cache Invalidationβ
- Watch keys
- Update local cache
3. Distributed Coordinationβ
- React to lease or lock changes
- Trigger failover
4. Presence Trackingβ
- Watch active workers
- Detect joins/leaves
Best Practicesβ
- Use prefix watch for groups
- Enable snapshot when you need initial state
- Stop subscriptions when no longer needed
- Keep handlers lightweight
- Avoid blocking operations inside handlers
Key Takeawayβ
Watch = Real-time event stream over your data
It transforms your system from:
Polling β Reactive
Whatβs Nextβ
π Continue to Scan & Query to explore data