|
| 1 | +Imagine you are building a smart AI assistant that: |
| 2 | + - Remembers chats, but only temporarily |
| 3 | + - Thinks by meaning, not by matching exact words |
| 4 | + - Cleans up after itself, with no manual scripts |
| 5 | + |
| 6 | +Redis 8 has two powerful capabilities to make this happen: |
| 7 | + - Field-level expiration: Let individual chat messages expire on their own |
| 8 | + - Vector similarity search: Find past messages based on meaning, not keywords |
| 9 | + |
| 10 | +Let’s dive in. |
| 11 | + |
| 12 | +### Short-Term Memory with Field-Level Expiry |
| 13 | +Each chat session is stored as a Redis Hash. |
| 14 | +Each message is a field in the hash. |
| 15 | +Redis 8’s new HSETEX command allows you to assign a TTL to each field, perfect for building ephemeral, session-level memory. |
| 16 | + |
| 17 | +```redis:[run_confirmation=true] Upload Session Data |
| 18 | +// session:42 is the session ID |
| 19 | +// msg:<timestamp> ensures uniqueness and traceability |
| 20 | +HSETEX session:42 msg:1717935301 120 "Hi Chatbot!" |
| 21 | +HSETEX session:42 msg:1717935361 180 "What can you do?" |
| 22 | +HSETEX session:42 msg:1717935440 90 "Can you remind me about my tasks?" |
| 23 | +HSETEX session:42 msg:1717935720 30 "What's the news today?" |
| 24 | +``` |
| 25 | + |
| 26 | +Each field automatically expires after its TTL (in seconds). |
| 27 | +No need for cron jobs or background workers. |
| 28 | +What you get: |
| 29 | + - Clean memory |
| 30 | + - Zero manual cleanup |
| 31 | + - Session-scoped retention, just like short-term memory in humans |
| 32 | + |
| 33 | + |
| 34 | +Try it: After a few minutes, run `HGETALL session:42` and see what's left. |
| 35 | + |
| 36 | +### Vector Search for Semantic Recall |
| 37 | +Now, your assistant needs to “recall” semantically related messages, not just match by words. |
| 38 | +To do that, you’ll: |
| 39 | + - Convert messages to vector embeddings |
| 40 | + - Store them in Redis |
| 41 | + - Use Vector Search with FT.SEARCH for semantic retrieval |
| 42 | + |
| 43 | +```redis:[run_confirmation=true] Create a Vector Index |
| 44 | +FT.CREATE idx:memory ON HASH PREFIX 1 memory: SCHEMA |
| 45 | + message TEXT |
| 46 | + embedding VECTOR FLAT // FLAT = exact vector search |
| 47 | + 6 |
| 48 | + TYPE FLOAT32 |
| 49 | + DIM 8 // DIM = embedding size, DIM 8 is just for demo purposes. In real use, embeddings are usually 128–1536 dimensions. |
| 50 | + DISTANCE_METRIC COSINE // COSINE = measures semantic closeness |
| 51 | +``` |
| 52 | + |
| 53 | +Now, let’s add entries for your chatbots: |
| 54 | + |
| 55 | +```redis:[run_confirmation=true] Add entries for the chatbot |
| 56 | +// Embeddings are stored as binary FLOAT32 vectors - this is a compact format required by Redis Vector Serch indexes |
| 57 | +HSET memory:1 message "Book a dentist appointment" embedding "\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x00@" |
| 58 | +HSET memory:2 message "Remind me to water plants" embedding "\x00\x00\x80@\x00\x00\x80@\x00\x00\x80@\x00\x00\x80?\x00\x00\x80?\x00\x00@@" |
| 59 | +HSET memory:3 message "What’s the weather like?" embedding "\x00\x00@@\x00\x00\x00@\x00\x00\x00@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80?" |
| 60 | +HSET memory:4 message "Cancel my gym session" embedding "\x00\x00@@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80@\x00\x00\x00@\x00\x00\x00@" |
| 61 | +HSET memory:5 message "Start a new shopping list" embedding "\x00\x00\x00@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80@\x00\x00\x80?\x00\x00@@" |
| 62 | +``` |
| 63 | + |
| 64 | +Now your messages are vectorized and ready for search. |
| 65 | + |
| 66 | +### Let Chatbot Think – Semantic Search with Vectors |
| 67 | +When a user sends a new message, convert it to an embedding and run a KNN search: |
| 68 | + |
| 69 | +```redis:[run_confirmation=true] Search For Similar Messages |
| 70 | +// Returns the top 3 semantically similar messages, even if no words match directly. |
| 71 | +FT.SEARCH idx:memory "*=>[KNN 3 @embedding $vec AS score]" |
| 72 | + PARAMS 2 vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@" |
| 73 | + SORTBY score |
| 74 | + DIALECT 2 |
| 75 | +``` |
| 76 | + |
| 77 | +Now your assistant “remembers” things it’s heard before - by meaning. |
| 78 | + |
| 79 | +### Real-Time Session Cleanup – Redis Handles It |
| 80 | +Want to check what's still in memory? |
| 81 | + |
| 82 | +```redis:[run_confirmation=false] Check Sessions |
| 83 | +HGETALL session:42 |
| 84 | +``` |
| 85 | + |
| 86 | +Only the unexpired fields remain. Redis does the cleanup invisibly in the background. |
| 87 | +Your assistant has a clean, focused mind at all times. |
| 88 | + |
| 89 | +### Next Steps |
| 90 | +Now that your assistant has memory and meaning, you can: |
| 91 | + - Tie session messages to store embeddings for per-session recall |
| 92 | + - Use RAG (Retrieval-Augmented Generation) by combining Redis Vector Search with LLMs |
| 93 | + - Add per-user memory: prefix session keys with a user ID (user:42:session:...) |
| 94 | + - Introduce a fallback to persistent storage for long-term memory using Redis Flex |
0 commit comments