Should a connection to Redis cluster be made on each Flask request?
<p>I have a Flask API, it connects to a Redis cluster for caching purposes. Should I be creating and tearing down a Redis connection on each flask api call? Or, should I try and maintain a connection across requests?</p>

<p>My argument against the second option is that I should really try and keep the api as stateless as possible, and I also don't know if keeping some persistent across request might causes threads race conditions or other side effects.</p>

<p>However, if I want to persist a connection, should it be saved on the session or on the application context?</p>

<p>It's good idea from the performance standpoint to keep connections to a database opened between requests. The reason for that is that opening and closing connections is not free and takes some time which may become problem when you have too many requests. Another issue that a database can only handle up to a certain number of connections and if you open more, database performance will degrade, so you need to control how many connections are opened at the same time.</p>

<p>To solve both of these issues you may use a connection pool. A connection pool contains a number of opened database connections and provides access to them. When a database operation should be performed from a connection shoul be taken from a pool. When operation is completed a connection should be returned to the pool. If a connection is requested when all connections are taken a caller will have to wait until some connections are returned to the pool. Since no new connections are opened in this processed (they all opened in advance) this will ensure that a database will not be overloaded with too many parallel connections.</p>

<p>If connection pool is used correctly a single connection will be used by only one thread will use it at any moment.</p>

<p>Despite of the fact that connection pool has a state (it should track what connections are currently in use) your API will be stateless. This is because from the API perspective "stateless" means: does not have a state/side-effects visible to an API user. Your server can perform a number of operations that change its internal state like writing to log files or writing to a cache, but since this does not influence what data is being returned as a reply to API calls this does not make this API "stateful".</p>

<p>You can see some examples of using Redis connection pool <a href="http://www.programcreek.com/python/example/22730/redis.ConnectionPool" rel="nofollow">here</a>.</p>

<p>Regarding where it should be stored I would use application context since it fits better to its <a href="http://flask.pocoo.org/docs/0.11/appcontext/#context-usage" rel="nofollow">purpose</a>.</p>

python flask redis