fakeredis¶
A fast, pure-Python implementation of the Redis protocol — no server required.
fakeredis is a drop-in replacement for redis-py and valkey-py that runs entirely in-memory. Write and run tests that depend on Redis, Valkey, DragonflyDB, or KeyDB — without spinning up a real server, a container, or a network connection.
That's it — no server to install, no port to manage, no teardown. For the full list of commands, see Supported commands.
✨ Why fakeredis?¶
- 🚀 Zero setup — no Redis server, Docker, or network required. Pure Python.
- 🔌 Drop-in compatible — same API as
redis.Redisandredis.asyncio.Redis. - ⚡ Fast & isolated — in-memory, so tests run quickly and start from a clean slate.
- 🧩 Multi-backend — emulate Redis, Valkey, DragonflyDB, or KeyDB, and pin a specific server version.
- 📦 Redis Stack support — JSON, Bloom/Cuckoo filters, TimeSeries, and Geo commands.
- 🤝 Share or isolate state — one shared in-memory server across clients, or independent servers per test.
- 🌐 Real TCP mode — expose the fake server over a socket for clients you can't inject.
📥 Installation¶
pip install fakeredis # core, no extras
pip install "fakeredis[lua]" # EVAL / EVALSHA scripting
pip install "fakeredis[json]" # JSON.* commands
pip install "fakeredis[bf]" # Bloom / Cuckoo / Count-Min / Top-K filters
pip install "fakeredis[probabilistic,json]" # probabilistic filters + JSON
Tip
On macOS / zsh you must quote the extras, e.g. pip install "fakeredis[json]".
How to Use¶
FakeRedis supports two connection modes:
graph LR
subgraph direct["Direct (in-process)"]
D1["FakeRedis()"] -->|"in-memory"| DB1["FakeServer\n(shared state)"]
D2["FakeAsyncRedis()"] --> DB1
end
subgraph tcp["TCP server (socket-compatible)"]
T["TcpFakeServer\n(Thread)"] -->|"listens on\n127.0.0.1:port"| DB2["FakeServer\n(shared state)"]
C1["redis.Redis(host=...)"] -->|"TCP"| T
C2["Any Redis client"] -->|"TCP"| T
end
Use direct mode for unit/integration tests — zero setup, fastest possible execution.
Use TCP server mode when your code creates its own redis.Redis connection and you can't inject a fake client.
Start a server on a thread¶
It is possible to start a server on a thread and use it as a connect to it as you would a real redis server.
from threading import Thread
from fakeredis import TcpFakeServer
server_address = ("127.0.0.1", 6379)
server = TcpFakeServer(server_address, server_type="redis")
t = Thread(target=server.serve_forever, daemon=True)
t.start()
import redis
r = redis.Redis(host=server_address[0], port=server_address[1])
r.set("foo", "bar")
assert r.get("foo") == b"bar"
# When you are done with the server, you can stop it with:
server.shutdown()
server.server_close()
t.join()
Use as a pytest fixture¶
import pytest
@pytest.fixture
def redis_client(request):
import fakeredis
redis_client = fakeredis.FakeRedis()
return redis_client
General usage¶
FakeRedis can imitate Redis server version 6.x or 7.x or 8.x, Valkey server, and dragonfly server. Redis version 7 is used by default.
The intent is for fakeredis to act as though you're talking to a real redis server. It does this by storing the state internally. For example:
>>> import fakeredis
>>> r = fakeredis.FakeStrictRedis(server_type="redis")
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'
>>> r.lpush('bar', 1)
1
>>> r.lpush('bar', 2)
2
>>> r.lrange('bar', 0, -1)
[2, 1]
The state is stored in an instance of FakeServer. If one is not provided at
construction, a new instance is automatically created for you, but you can
explicitly create one to share state:
>>> import fakeredis
>>> server = fakeredis.FakeServer()
>>> r1 = fakeredis.FakeStrictRedis(server=server)
>>> r1.set('foo', 'bar')
True
>>> r2 = fakeredis.FakeStrictRedis(server=server)
>>> r2.get('foo')
'bar'
>>> r2.set('bar', 'baz')
True
>>> r1.get('bar')
'baz'
>>> r2.get('bar')
'baz'
It is also possible to mock connection errors, so you can effectively test your error handling.
Set the connected attribute of the server to False after initialization.
>>> import fakeredis
>>> server = fakeredis.FakeServer()
>>> server.connected = False
>>> r = fakeredis.FakeStrictRedis(server=server)
>>> r.set('foo', 'bar')
ConnectionError: FakeRedis is emulating a connection error.
>>> server.connected = True
>>> r.set('foo', 'bar')
True
Fakeredis implements the same interface as redis-py, the popular
redis client for python, and models the responses of redis 6.x or 7.x.
async Redis¶
Async redis client is supported. Instead of using fakeredis.FakeRedis, use fakeredis.FakeAsyncRedis.
>>> from fakeredis import FakeAsyncRedis
>>> r1 = FakeAsyncRedis()
>>> await r1.set('foo', 'bar')
True
>>> await r1.get('foo')
'bar'
Use to test django cache¶
Update your cache settings:
from fakeredis import FakeConnection
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': '...',
'OPTIONS': {
'connection_class': FakeConnection
}
}
}
For django-redis library, use the following OPTIONS:
You can use
django @override_settings decorator
Use to test django-rq¶
There is a need to override django_rq.queues.get_redis_connection with a method returning the same connection.
import django_rq
# RQ
# Configuration to pretend there is a Redis service available.
# Set up the connection before RQ Django reads the settings.
# The connection must be the same because in fakeredis connections
# do not share the state. Therefore, we define a singleton object to reuse it.
def get_fake_connection(config: Dict[str, Any], strict: bool):
from fakeredis import FakeRedis, FakeStrictRedis
redis_cls = FakeStrictRedis if strict else FakeRedis
if "URL" in config:
return redis_cls.from_url(
config["URL"],
db=config.get("DB"),
)
return redis_cls(
host=config["HOST"],
port=config["PORT"],
db=config.get("DB", 0),
username=config.get("USERNAME", None),
password=config.get("PASSWORD"),
)
django_rq.queues.get_redis_connection = get_fake_connection
Use to test FastAPI¶
See info on this issue
If you're using FastAPI dependency injection to provide a Redis connection, then you can override that dependency for testing.
Your FastAPI application main.py:
from typing import Annotated, Any, AsyncIterator
from redis import asyncio as redis
from fastapi import Depends, FastAPI
app = FastAPI()
async def get_redis() -> AsyncIterator[redis.Redis]:
# Code to handle creating a redis connection goes here, for example
async with redis.from_url("redis://localhost:6379") as client: # type: ignore[no-untyped-call]
yield client
@app.get("/")
async def root(redis_client: Annotated[redis.Redis, Depends(get_redis)]) -> Any:
# Code that does something with redis goes here, for example:
await redis_client.set("foo", "bar")
return {"redis_keys": await redis_client.keys()}
Assuming you use pytest-asyncio, your test file (or you can put the fixtures in conftest.py as usual):
from typing import AsyncIterator
from unittest import mock
import fakeredis
import httpx
import pytest
import pytest_asyncio
from redis import asyncio as redis
from main import app, get_redis
@pytest_asyncio.fixture
async def redis_client() -> AsyncIterator[redis.Redis]:
async with fakeredis.FakeAsyncRedis() as client:
yield client
@pytest_asyncio.fixture
async def app_client(redis_client: redis.Redis) -> AsyncIterator[httpx.AsyncClient]:
async def get_redis_override() -> redis.Redis:
return redis_client
transport = httpx.ASGITransport(app=app) # type: ignore[arg-type] # https://github.com/encode/httpx/issues/3111
async with httpx.AsyncClient(transport=transport, base_url="http://test") as app_client:
with mock.patch.dict(app.dependency_overrides, {get_redis: get_redis_override}):
yield app_client
@pytest.mark.asyncio
async def test_app(app_client: httpx.AsyncClient) -> None:
response = await app_client.get("/")
assert response.json()["redis_keys"] == ["foo"]
Architecture¶
How it works¶
FakeRedis intercepts redis-py (or valkey-py) at the connection layer and routes commands through an in-memory implementation that mirrors Redis behaviour, without any network I/O.
flowchart LR
Client["redis-py / valkey-py\nclient"] -->|"send_command()"| FC["FakeConnection\n_connection.py"]
FC -->|"_connect()"| FS["FakeSocket\n(mixin stack)"]
FS -->|"dispatch"| MX["Command mixin\ncommands_mixins/"]
MX -->|"read / write"| DB["Database\n_helpers.py"]
DB -->|"result"| FS
FS -->|"read_response()"| Client
SRV["FakeServer\n_server.py"] -. "owns" .-> DB
FC -. "references" .-> SRV
Sharing state between clients¶
Each FakeRedis instance uses a FakeServer to hold all database state. Passing the same FakeServer to
multiple clients lets them share data, just like connections to the same real Redis instance.
graph TD
SRV["FakeServer"]
SRV -->|"db index 0"| D0["Database 0\ndict + TTLs"]
SRV -->|"db index 1"| D1["Database 1\ndict + TTLs"]
SRV -->|"pub/sub state"| PS["PubSub channels\n& patterns"]
SRV -->|"script cache"| SC["Lua script\ncache"]
C1["FakeRedis (client 1)"] -->|"server="| SRV
C2["FakeRedis (client 2)"] -->|"server="| SRV
C3["FakeAsyncRedis (client 3)"] -->|"server="| SRV
Known Limitations¶
Apart from unimplemented commands, there are a number of cases where fakeredis won't give identical results to real redis. The following are differences that are unlikely to ever be fixed; there are also differences that are fixable (such as commands that do not support all features) which should be filed as bugs in GitHub.
-
Hyperloglogs are implemented using sets underneath. This means that the
typecommand will return the wrong answer, you can't usegetto retrieve the encoded value, and counts will be slightly different (they will in fact be exact). -
When a command has multiple error conditions, such as operating on a key of the wrong type and an integer argument is not well-formed, the choice of error to return may not match redis.
-
The
incrbyfloatandhincrbyfloatcommands in redis use the Clong doubletype, which typically has more precision than Python'sfloattype. -
Redis makes guarantees about the order in which clients blocked on blocking commands are woken up. Fakeredis does not honor these guarantees.
-
Where redis contains bugs, fakeredis generally does not try to provide exact bug compatibility. It's not practical for fakeredis to try to match the set of bugs in your specific version of redis.
-
There are a number of cases where the behavior of redis is undefined, such as the order of elements returned by set and hash commands. Fakeredis will generally not produce the same results, and in Python versions before 3.6 may produce different results each time the process is re-run.
-
SCAN/ZSCAN/HSCAN/SSCAN will not necessarily iterate all items if items are deleted or renamed during iteration. They also won't necessarily iterate in the same chunk sizes or the same order as redis. This is aligned with redis behavior as can be seen in tests
test_scan_delete_key_while_scanning_should_not_returns_it_in_scan. -
DUMP/RESTORE will not return or expect data in the RDB format. Instead, the
picklemodule is used to mimic an opaque and non-standard format. WARNING: Do not use RESTORE with untrusted data, as a malicious pickle can execute arbitrary code.
Local development environment¶
To ensure parity with the real redis, there are a set of integration tests that mirror the unittests. For every unittest that is written, the same test is run against a real redis instance using a real redis-py client instance. To run these tests, you must have a redis server running on localhost, port 6379 (the default settings). WARNING: the tests will completely wipe your database!
First install uv if you don't have it, and then install all the dependencies:
To run all the tests:
If you only want to run tests against fake redis, without a real redis::
Because this module is attempting to provide the same interface as redis-py, the python bindings to redis, a
reasonable way to test this to take each unittest and run it against a real redis server.
Fakeredis and the real redis server should give the same result.
To run tests against a real redis instance instead:
If redis is not running, and you try to run tests against a real redis server, these tests will have a result of 's' for skipped.
There are some tests that test redis blocking operations that are somewhat slow. If you want to skip these tests during day-to-day development, they have all been tagged as 'slow' so you can skip them by running:
Contributing¶
Contributions are welcome. You can contribute in many ways:
- Report bugs you found.
- Check out issues with
Help wantedlabel. - Implement commands which are not yet implemented. Follow the guide how to implement a new command.
- Write additional test cases. Follow the guide how to write a test-case.
Additionally, you can install pre-commit hook in the repo to add it as a git hook by
running: pre-commit install. It is configured to check all change files based on configuration in
.pre-commit-config.yaml.
Please follow coding standards listed in the contributing guide.
Sponsor¶
fakeredis-py is developed for free.
You can support this project by becoming a sponsor using this link.