Give each key its own memory
How per-key state is stored, updated, and expired so processing can use running totals, previously seen identifiers, or interaction history. · associate
State is information kept from earlier inputs so the next input can use it. In per-key state, each customer, device, or order has its own retained context. Choose a key that matches the entity whose history matters: a customer ID for customer totals, for example.
The application looks up the input's key, reads its state, and applies an update. For a running purchase total, it reads the customer's previous total, adds the purchase amount, and saves the result. If that customer has no state yet, the application uses a defined starting value, such as zero. Another customer's total stays unchanged.
Keep only the context the operation needs. A total needs one accumulated value; duplicate detection needs previously seen identifiers; interaction processing might need the latest status or a bounded history. These choices determine how much state each key consumes.
| Use | Key | Retained state |
|---|---|---|
| Running purchase total | Customer ID | Accumulated amount |
| Duplicate detection | Customer ID | Seen event IDs |
| Order interactions | Order ID | Latest status |