fakeredis is a drop-in replacement for redis-py and valkey-py that runs entirely
in-memory. Write tests that depend on Redis, Valkey, DragonflyDB,
KeyDB, or — without spinning up a real server, a container, or a network connection.
# Behave like Redis 6 — which is what KeyDB is compatible with...
r = fakeredis.FakeStrictRedis(version=6)
# ...or like Valkey, DragonflyDB or KiviDB
r = fakeredis.FakeStrictRedis(server_type="valkey")
r = fakeredis.FakeStrictRedis(server_type="dragonfly")
r = fakeredis.FakeStrictRedis(server_type="kividb")
The fake clients above only help when you can hand one to the code under test. When the code opens its own
connection — or isn't Python at all — TcpFakeServer serves the same in-memory server over a real TCP socket, so
anything that speaks the Redis protocol can connect to it:
from threading import Thread
import redis
from fakeredis import TcpFakeServer
server = TcpFakeServer(("127.0.0.1", 6379), server_type="redis")
thread = Thread(target=server.serve_forever, daemon=True)
thread.start()
r = redis.Redis(host="127.0.0.1", port=6379) # a regular client, not a fake one
r.set("foo", "bar")
r.get("foo") # b'bar'
# When you are done:
r.close()
server.shutdown()
server.server_close()
thread.join()
Any client — redis-py, valkey-py, redis-cli, or a client in another language. All connections share
one FakeServer, so they see the same data.
RESP2 and RESP3 — connections start in RESP2 and switch when the client sends HELLO 3, as with a real
server. Pub/sub and blocking commands work over the socket too.
Server type and version — pass server_type ("redis", "valkey", "dragonfly", "kividb") and
server_version (defaults to (8, 0)), e.g. TcpFakeServer(address, server_type="valkey", server_version=(8, 1)).
Free port — bind to port 0 to let the OS choose one, then read it back from server.server_address.
TcpFakeServer is a standard-library ThreadingTCPServer, handling each client connection
on its own thread. It is meant for tests and local development, not as a production Redis replacement.
See the official documentation for the full list of supported commands and configuration options.
❤️ Sponsor
fakeredis-py is developed and maintained for free. If it saves you time, please consider
becoming a sponsor — it directly supports continued development.