A Python abstraction layer that makes Redis or DragonflyDB behave like a shared Python object across processes/microservices.
Project description
🗄️ synced-memory
A production-ready Python class for seamless, multiprocessing-safe, persistent key-value storage using Redis or DragonflyDB as a backend. If the backend is unavailable, values are cached locally and queued for syncing when it comes back online. All values are serialized as JSON, and you interact with it using natural Python attribute access.
Purpose
The intention is to use this with agentic workflows deployed as microservices, allowing for multiple instances of the same pod to share their state.
✨ Features
- 🔄 Multiprocessing-safe: All processes share the same state via Redis or DragonflyDB.
- 🧠 Pythonic API: Set and get attributes as if they were regular object properties.
- 🕰️ Persistence: Values survive process restarts and context blocks.
- 🚦 Resilient: If the backend is down, changes are queued and flushed when it returns.
- 🧩 Customizable: Prefixes and conversation IDs for namespacing.
- 🧵 Background sync: Queued changes are flushed automatically in the background.
🚀 Quickstart
pip install synced-memory
from synced_memory.redis import Memory
mem = Memory()
mem.answer = 42
print(mem.answer) # 42
# Across processes or instances:
mem2 = Memory()
print(mem2.answer) # 42
mem.settings = {"theme": "dark", "volume": 0.75}
print(mem.settings) # {'theme': 'dark', 'volume': 0.75}
To use DragonflyDB instead:
from synced_memory.dragonflydb import Memory
mem = Memory()
mem.answer = 42
🧑💻 Context Management
You can use Memory as a context manager for automatic resource handling:
with Memory() as memory:
memory.session = "active"
print(memory.session) # "active"
# Later, in a new context:
with Memory() as memory:
print(memory.session) # "active"
🔄 Auto-Synced Collections
Lists and dictionaries are automatically wrapped as SyncedList and SyncedDict, which sync changes to the backend immediately:
mem = Memory()
mem.items = [1, 2, 3]
mem.items.append(4) # Automatically syncs
mem2 = Memory()
print(mem2.items) # [1, 2, 3, 4]
mem.config = {"theme": "dark"}
mem.config["lang"] = "en" # Automatically syncs
print(mem2.config) # {'theme': 'dark', 'lang': 'en'}
Nested structures work too:
mem.data = {"user": {"preferences": {"color": "blue"}}}
mem.data["user"]["preferences"]["color"] = "red" # Syncs!
Converting to Plain Python Types
For libraries that need plain Python objects (serialization, pickling, etc.):
mem.items = [1, 2, 3]
plain_list = mem.items.aslist() # Returns regular list
mem.config = {"key": "value"}
plain_dict = mem.config.asdict() # Returns regular dict
import pickle
pickle.dump(plain_list, file) # Works!
🗂️ Namespacing
By default, synced-memory uses memory: as its key prefix. Override with REDIS_PREFIX:
mem = Memory()
mem.state = {"step": 1}
print(mem.state) # {'step': 1}
Scoping
Use PrefixedMemory to isolate memory by a custom scope prefix:
from synced_memory.redis import PrefixedMemory
mem = PrefixedMemory(prefix="session_abc123")
mem.state = {"step": 1}
print(mem.state) # {'step': 1}
⚙️ Environment Variables
REDIS_HOST: Backend hostname (default:redis)REDIS_PORT: Backend port (default:6379)REDIS_PREFIX: Key prefix (default:memory:)
--- WHEN UPDATING README.md: YOU CAN KEEP EVERYTHING BELOW THIS LINE ---
🛠️ Development
The only requirement is 🐳 Docker.
(The .devcontainer and tasks.json are prepared assuming a *nix system, but if you know the commands, this will work on Windows, too.)
- Clone the repo.
- Branch out.
- Open in "devcontainer" on VS Code and start developing. Run
pytestunderteststo test. - Alternatively, if you are a fan of Test-Driven Development like me, you can run the tests without getting on a container.
.vscode/tasks.jsonhas the command to do so, but it's also listed here:
docker compose -f tests/docker-compose.yaml up --build --abort-on-container-exit --exit-code-from test
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file synced_memory-0.1.0.tar.gz.
File metadata
- Download URL: synced_memory-0.1.0.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5617c622a403494028b0bc6c192ff899484a729679f61cbd1d28ad2b31e1c98
|
|
| MD5 |
3485f03e0a846cb02bfaaa493ad2b954
|
|
| BLAKE2b-256 |
5374cf37e0480e96b18b67f757936afada74f50f9e654be5cb6177ea42ff2cc3
|
Provenance
The following attestation bundles were made for synced_memory-0.1.0.tar.gz:
Publisher:
ci.yaml on sinan-ozel/synced-memory
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
synced_memory-0.1.0.tar.gz -
Subject digest:
e5617c622a403494028b0bc6c192ff899484a729679f61cbd1d28ad2b31e1c98 - Sigstore transparency entry: 1416386917
- Sigstore integration time:
-
Permalink:
sinan-ozel/synced-memory@87268fa821490f924e902adf234abce45cbea451 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sinan-ozel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@87268fa821490f924e902adf234abce45cbea451 -
Trigger Event:
push
-
Statement type:
File details
Details for the file synced_memory-0.1.0-py3-none-any.whl.
File metadata
- Download URL: synced_memory-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d3958288ab07aabe56abf9805ae7d80ba557d2d22643ed6d414b3b21917f3c0
|
|
| MD5 |
eaa40d300754ee5c1bb1e1c0885a3dec
|
|
| BLAKE2b-256 |
ea37b3e9d5d7b34a91eda2e229103bf935832ec32f3a903bc55582da8eab824e
|
Provenance
The following attestation bundles were made for synced_memory-0.1.0-py3-none-any.whl:
Publisher:
ci.yaml on sinan-ozel/synced-memory
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
synced_memory-0.1.0-py3-none-any.whl -
Subject digest:
6d3958288ab07aabe56abf9805ae7d80ba557d2d22643ed6d414b3b21917f3c0 - Sigstore transparency entry: 1416387013
- Sigstore integration time:
-
Permalink:
sinan-ozel/synced-memory@87268fa821490f924e902adf234abce45cbea451 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sinan-ozel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@87268fa821490f924e902adf234abce45cbea451 -
Trigger Event:
push
-
Statement type: