Redis is an in-memory data structure store used where speed is necessary but complexity is beyond simple key-value operations. (If you’re just doing caching, prefer memcached). Redis-py is a Python client library for Redis.


To integrate redis-py with your application, add the appropriate client declaration to your context configuration:

      "foo": RedisClient(),

configure it in your application’s configuration file:



# required: what redis instance to connect to
foo.url = redis://localhost:6379/0

# optional: the maximum size of the connection pool
foo.max_connections = 99

# optional: how long to wait for a connection to establish
foo.socket_connect_timeout = 3 seconds

# optional: how long to wait for a command to execute
foo.socket_timeout = 200 milliseconds


and then use the attached Redis-like object in request:

def my_method(request):


class baseplate.clients.redis.RedisClient(redis_client_name='', **kwargs)[source]

Configure a Redis client.

This is meant to be used with baseplate.Baseplate.configure_context().


redis_client_name (str) – The name of this Redis client. Prefer to use client_name

keyword argument to the pool_from_config function. Use this if your using a Redis host or proxy that doesn’t support the CLIENT SETNAME function.

See pool_from_config() for available configuration settings.

baseplate.clients.redis.pool_from_config(app_config, prefix='redis.', **kwargs)[source]

Make a ConnectionPool from a configuration dictionary.

The keys useful to pool_from_config() should be prefixed, e.g. redis.url, redis.max_connections, etc. The prefix argument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on the redis.ConnectionPool constructor.

Supported keys:

  • url (required): a URL like redis://localhost/0.

  • max_connections: an integer maximum number of connections in the pool

  • socket_connect_timeout: how long to wait for sockets to connect. e.g.

    200 milliseconds (Timespan())

  • socket_timeout: how long to wait for socket operations, e.g.

    200 milliseconds (Timespan())

Return type



class baseplate.clients.redis.RedisContextFactory(connection_pool, name='redis', redis_client_name='')[source]

Redis client context factory.

This factory will attach a MonitoredRedisConnection to an attribute on the RequestContext. When Redis commands are executed via this connection object, they will use connections from the provided redis.ConnectionPool and automatically record diagnostic information.


connection_pool (ConnectionPool) – A connection pool.


Report runtime metrics to the stats system.


batch (Client) – A metrics client to report statistics to.

Return type


make_object_for_context(name, span)[source]

Return an object that can be added to the context object.

  • name (str) – The name assigned to this object on the context.

  • span (Span) – The current span this object is being made for.

Return type


class baseplate.clients.redis.MonitoredRedisConnection(context_name, server_span, connection_pool, redis_client_name='')[source]

Redis connection that collects diagnostic information.

This connection acts like redis.StrictRedis except that all operations are automatically wrapped with diagnostic collection.

The interface is the same as that class except for the pipeline() method.

execute_command(*args, **kwargs)[source]

Execute a command and return a parsed response

Return type


pipeline(name, transaction=True, shard_hint=None)[source]

Create a pipeline.

This returns an object on which you can call the standard Redis commands. Execution will be deferred until execute is called. This is useful for saving round trips.

  • name (str) – The name to attach to diagnostics for this pipeline.

  • transaction (bool) – Whether or not the commands in the pipeline are wrapped with a transaction and executed atomically.

Return type


transaction(*args, **kwargs)[source]

Not currently implemented.

Return type


class baseplate.clients.redis.MessageQueue(name, client)[source]

A Redis-backed variant of MessageQueue.

  • name (str) – can be any string.

  • client (ConnectionPool) – should be a redis.ConnectionPool or redis.BlockingConnectionPool from which a client connection can be created from (preferably generated from the pool_from_config() helper).


Read a message from the queue.


timeout (Optional[float, None]) – If the queue is empty, the call will block up to timeout seconds or forever if None, if a float is given, it will be rounded up to be an integer


TimedOutError The queue was empty for the allowed duration of the call.

Return type


put(message, timeout=None)[source]

Add a message to the queue.


message (bytes) – will be typecast to a string upon storage and will come out of the queue as a string regardless of what type they are when passed into this method.

Return type


Not implemented for Redis variant.

Return type



Close queue when finished.

Will delete the queue from the Redis server (Note, can still enqueue and dequeue as the actions will recreate the queue)

Return type


Runtime Metrics

In addition to request-level metrics reported through spans, this wrapper reports connection pool statistics periodically via the Prometheus Exporter system. All metrics are tagged with client, the name given to configure_context() when registering this context factory.

The following metrics are reported:


The size limit for the connection pool.


How many connections have been established and are currently checked out and being used.

New in version 1.5.

Changed in version 2.0: Runtime metrics were changed to use tags.