15

I am creating flask app with Redis database. And I have one connection question

I can have Redis connection global and keep non-closed all time:

init.py

import os
from flask import Flask
import redis

app = Flask(__name__)

db = redis.StrictRedis(host='localhost', port=6379, db=0)

Also I can reconnect every request (Flask doc http://flask.pocoo.org/docs/tutorial/dbcon/):

init.py

import os
from flask import Flask
import redis

app = Flask(__name__)

#code...

@app.before_request
def before_request():
    g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
    db = getattr(g, 'db', None)
    if db is not None:
        db.close()

Which method is better? Why I should use it?

Thanks for the help!

2
  • "Better" is kind of hard to address and not really the kind of question that SO is designed to answer. For instance do you mean "faster", "safer", "more efficient"? Also, how are you using your database - what sort of volume of requests will you be making and are they single instructions or a series of instructions?
    – will-hart
    Aug 2, 2013 at 18:45
  • Its very important speed and stability. And database is used for user data, like user id, public key, private key and etc.
    – KiraLT
    Aug 2, 2013 at 19:40

2 Answers 2

29

By default redis-py uses connection pooling. The github wiki says:

Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool.

This means that for most applications and assuming your redis server is on the same computer as your flask app, its unlikely that "opening a connection" for each request is going to cause any performance issues. The creator of Redis Py has suggested this approach:

a. create a global redis client instance and have your code use that.
b. create a global connection pool and pass that to various redis instances throughout your code.

Additionally, if you have a lot of instructions to execute at any one time then it may be worth having a look at pipelining as this reduces that back and forth time required for each instruction.

-2

Using Flask, global variables are not recommended. We can use g to manage redis client during a request. Like manage a database connection using factory pattern.

from flask import g
import redis

def get_redis():
    if 'db' not in g:
        g.db = redis.Redis(host='localhost', port=6379, db=0)
    return g.db

Reconnect every request is better for you.

The application context is a good place to store common data during a request or CLI command. Flask provides the g object for this purpose. It is a simple namespace object that has the same lifetime as an application context.

1
  • You should add a teardown function.
    – ikreb
    Mar 7, 2022 at 13:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.