You are on page 1of 2

Redis: it is no sql database which stores 5 different types of data structures in

the memory.

Redis is extremely fast and used as message broker for celery.

Installing Redis From Repository:


$ sudo apt-get update
$ sudo apt-get upgrade

Install redis-server:
$ sudo apt-get -y install redis-server

By default, redis-server is started after installation. You can check using the
service command :
$ sudo service redis-server status
----------------------------------------------------------- Output
vammi:~$ sudo service redis-server status
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor
preset: enabled)
Active: active (running) since Tue 2016-03-08 05:09:17 IST; 48s ago
Main PID: 3700 (redis-server)
CGroup: /system.slice/redis-server.service
└─3700 /usr/bin/redis-server 127.0.0.1:6379

From output above we learned that redis server is already listening on port 6379
and bind to localhost or 127.0.0.1.

Start the redis server using the following:


1. redis-server : it starts the server at port 6379
***** IMPORTANT *****
By default redis does not bind to any IP address, we need to bind them to the
expected incoming ip addresses
On a similar note, we need to open the unix sockets for process communication
There are other parameters in conf file which are not required at inital
stage

sudo redis-server /etc/redis/redis.conf


2. redis-cli : This is client to redis-server to test
For Ex:
vammi:~$ redis-cli
127.0.0.1:6379> get user:vammi
(nil)
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> get user:vammi
(nil)
127.0.0.1:6379> set user:vammi '{name:"vammi nookala", age:3}'
OK
127.0.0.1:6379> get user:vammi
"{name:\"vammi nookala\", age:3}"
127.0.0.1:6379>
-----------------------------------------------------------------------------------
---------------------------------------------------------
Celery : It is asynchromus task queue (executes in the background) used in
conjunction with flask to offload any time consuming tasks and also used as flask
cron.
-----------------------------------------------------------------------------------
---------------------------------------------------------
Celery has three main components:
1. The Celery client : This is used to issue background jobs. When working with
Flask, the client runs with the Flask application.
2. The Celery workers : These are the processes that run the background jobs.
Celery supports local and remote workers, so you can start with a single worker
running on the same machine as the Flask server, and later add more workers as the
needs of your application grow.
3. The message broker : The client communicates with the the workers through a
message queue, and Celery supports several ways to implement these queues. The most
commonly used brokers are RabbitMQ and Redis and we are using redis.

Celery client : We are writing this in the flask


Celery Worker : We need to run a seperate celery worker process to really execute
our tasks
$celery worker -A app.celery --loglevel=info
Message Broker: We are running redis-server to pass the messages between Celery
client & Celery Worker

To find the number of tasks and keys in redis::


-----------------------------------------------
Finding the number of tasks in a queue:
$ redis-cli -h HOST -p PORT -n DATABASE_NUMBER llen QUEUE_NAME
Ex: redis-cli -h 127.0.0.1 -p 6379 -n 0 llen celery

The default queue is named celery. To get all available queues, invoke:
$ redis-cli -h HOST -p PORT -n DATABASE_NUMBER keys \*
Ex: redis-cli -h 127.0.0.1 -p 6379 -n 0 keys \*

-----------------------------------------------------------------------------------
---------------------------------------------------------
To make it work u need to do the following:
-------------------------------------------
1. Start the redis server as below
$sudo redis-server /etc/redis/redis.conf
2. Start the celery worker which are responsible for executing the background task
as below (refer above section for proper execution)
$celery worker -A app.celery --loglevel=info

You might also like