Spedo
SPEDO ENGINE
v0.69.0 PREVIEW
← Documentation

Spedo Reactive Delta Query Binding & Freshness Contracts

Since version v0.63.0

Spedo introduces Reactive Delta Query Binding (bind_query / bind_collection), transforming Spedo from a passive key-value store into an Intelligent Live In-Memory Dataflow Runtime. Instead of repeatedly querying over TCP or guessing cache TTLs, your application binds to query patterns, maintains local in-memory state, and receives differential micro-deltas in real time.


1. The Paradigm Shift: Live Query Bindings vs Passive Caches

Traditional application architectures suffer from standard caching dilemmas:

Spedo's Reactive Binding solves this with Live In-Memory Subscriptions & Micro-Deltas:

+-----------------------------------------------------------------------------------------+
|                    SPEDO REACTIVE DELTA QUERY ARCHITECTURE                             |
+-----------------------------------------------------------------------------------------+

  Application Worker (Python / Node / Go)
  +-------------------------------------------------------------------------------------+
  |                                                                                     |
  |   orders = db.bind_query("order:*", freshness="bounded:50ms")                       |
  |                                                                                     |
  |   +-------------------------------------------------------------------------------+ |
  |   | Local In-Memory Bound View (dict / live collection)                           | |
  |   |  - order:101 -> {"id": 101, "item": "Laptop", "status": "DELIVERED"}          | |
  |   |  - order:102 -> {"id": 102, "item": "Mouse", "status": "SHIPPED"}            | |
  |   +-------------------------------------------------------------------------------+ |
  |         ^                                                                           |
  |         |  0.0004 ms (Sub-microsecond local RAM reads)                              |
  |         |                                                                           |
  |   [ Fast App Read Operations ]                                                      |
  +-------------------------------------------------------------------------------------+
            ^
            |  Differential Delta Stream (DeltaOp: UPDATE_FIELD, SET, DEL)
            |  Only sends modified bytes over CDC ring buffer (99% bandwidth reduction)
            |
  +-------------------------------------------------------------------------------------+
  | Spedo Engine (Embedded or Standalone TCP Server)                                    |
  |   - CDC Ring Buffer / Sequence WAL Tracker                                          |
  |   - Atomic Pattern Matching & Delta Dispatcher                                     |
  +-------------------------------------------------------------------------------------+

2. Freshness Contracts (SLAs)

Every bound query accepts an explicit Freshness Contract, letting developers choose the exact trade-off between read speed and data consistency:

Contract ModeLatencyConsistency GuaranteeBest For
strict~0.1 - 0.5 msLinearizable / Zero Lag: Revalidates sync status before returning.Financial transactions, inventory balance, authorization locks.
bounded:<duration> (e.g. bounded:50ms)< 0.001 ms (Local RAM)Bounded Staleness SLA: Serves from local RAM at microsecond speed as long as lag is $≤ 50\,\text{ms}$; triggers background delta sync if lag exceeds SLA.Real-time dashboards, live user sessions, price feeds, order queues.
eventual< 0.0005 ms (Local RAM)Eventual Consistency: Serves local memory replica immediately with background delta stream.Product catalogs, user preferences, read-heavy microservice states.

3. Python SDK Usage & Examples

A. Basic Query Binding & Differential Updates

import spedo
from spedo import DeltaOp

# Connect via embedded in-process engine or remote TCP cluster:
db = spedo.embedded(max_memory="256MB")

# Bind a live collection of active orders with a 50ms freshness contract:
orders = db.bind_query("order:*", freshness="bounded:50ms")

# Read operations are 100% local in-memory (0.4 µs latency):
order_101 = orders.get("order:101")
print(f"Order Count: {len(orders)}")

# Differential deltas automatically patch the local view:
orders.apply_delta(
    DeltaOp.UPDATE_FIELD,
    key="order:101",
    value="DELIVERED",
    path="status"
)

assert orders.get("order:101")["status"] == "DELIVERED"

B. Filtered Reactive Queries (Predicates)

Bind queries can filter items using custom predicates so only relevant records occupy worker memory:

# Bind only pending high-priority orders:
vip_pending = db.bind_query(
    "order:*",
    freshness="bounded:20ms",
    filter_fn=lambda k, v: isinstance(v, dict) and v.get("status") == "PENDING" and v.get("amount", 0) > 1000
)

# Iterating only evaluates matching items:
for order_id, order_data in vip_pending.items():
    print(f"Priority Order: {order_id} -> ${order_data['amount']}")

C. Reactive Subscribers & Change Notifications

Attach event callbacks to react immediately when bound data changes:

orders = db.bind_query("order:*", freshness="bounded:50ms")

def on_order_updated(key: str, op: str, value: any):
    print(f"🔔 Live Delta Received: {op} on {key} -> {value}")

# Subscribe to change stream:
orders.subscribe(on_order_updated)

# When a mutation occurs, the subscriber is invoked instantly:
orders.apply_delta(DeltaOp.SET, "order:105", {"item": "GPU", "status": "PROCESSING"})

4. Performance & Efficiency Comparison

OperationTraditional Remote Cache (Redis/TCP)Spedo Reactive Bound QueryAdvantage
Read Latency0.650 ms (650 µs)0.0004 ms (0.4 µs)1,625× faster (0 µs network)
Collection TraversalRe-fetch entire array over TCPIterate local Python dict in RAMInstant $O(1)$ lookup
Network BandwidthFull payload per queryMicro-deltas only (few bytes)99.4% Bandwidth Saved
Cache InvalidationManual key deletion / TTL expiryAutomatic real-time CDC delta pushZero Cache Invalidation Bugs

5. Summary & Ecosystem Integration