SlideShare a Scribd company logo
BACKGROUND
PROCESSING TO
IMPROVE RESPONSE
TIME
by Chew Kok Hoor kokhoor@solutionx.com.
Python @ SolutionX
 Django Web Applications
 Django Mobile Apps backend
 Django-based Point-of-Sales system (POS)
 Device Connectors
Issue
 Issue: Webpages / Views that takes time to
response
 Contact Us Page
 Registration Page
 Possible causes:
 Sending email
 Slow writes to database, business rules etc.
What happens next
 Users / Visitors / Customer complains
 Boss ask us to resolve it, ASAP.
Short Term Resolution
 We spin while we can
What happens next after what
happened next earlier
 More Users / Visitors / Customers
 Timeout errors
Why timeout?
 In Linux, we use NGINX – GUNICORN - DJANGO
 There is a limit of exact concurrent request that a
server can handle.
 General formula for no. of workers is:
(2 x [number of cores]) + 1
 Therefore, 8 cores = (2 x 8) + 1 = 17
 Slow response = handle less requests per second
Possible solution
Background Processing
Pros n Cons
 Disadvantages
 New process required
 On failure unable to notify users thru response to
request.
 Worse case, lost contact to user who submitted
request.
 Need to monitor tasks not done / failed
 Advantages
 Timeout issue resolved
 Users get quicker response
 Can choose to process N tasks at a time
 Can retry tasks etc., background process can afford
slower response
What should be done?
1. Break down tasks of slow responding requests
2. Process tasks in background
Preparation
Stage
Perform slow
running task in
Background

Submit task for
processing
 
Tasks Breakdown
Preparation Stage
 record necessary
information for task to
execute properly or for
user to track status.
 activities to prevent
inconsistency or
breaking of business
rules
 preferably a means of
contacting user when it
is done (email, mobile
notification, etc.)
Slow Running tasks
 Email sending
 Create and setup
new database
 Setup new user
login roles, etc.
 Complex
calculations or setup
Choosing Task Queue software
Software Pro Cons
django-background-
tasks
(https://pypi.python.org/
pypi/django-
background-tasks)
- Very easy setup
- Easy to use
- Good for small volume
- Uses Django ORM for
backend
- Absolutely terrible for
medium-to-high volume
RQ
(http://python-rq.org/)
(https://github.com/ui/dj
ango-rq)
- Flexible, powered by
Redis
- Lower memory
footprint than Celery
- Great for high volume
- Not as many features
as Celery
- Medium difficulty
setup
- Redis only option for
storage
Celery
(http://www.celeryprojec
t.org/)
- De facto Django
standard
- Many
different storage types
- Flexible,
- Full-featured
- Great for high volume
- Challenging setup
- overkill for slower
traffic sites
From Two Scoops of Django - Best Practices For Django 1.8
by Daniel Roy Greenfeld & Audrey Roy Greenfeld (http://twoscoopspress.org/)
Choosing Task Queue Software
Django Background Tasks
- background processing code
INSTALLATION
 pip install django-background-tasks
 settings.py
INSTALLED_APPS += (
‘background_task’,
)
Django Background Tasks
- background processing code
import time
from background_task import background
@background(schedule=0)
def dbt_task(data):
print "[START] django-background-tasks for: " + data.get("name","")
time.sleep(10)
print "[DONE] django-background-tasks for: " + data.get("name","")
Django Background Tasks
- initiate background task
from . import tasks
tasks.dbt_task(request.POST.copy())
Django Background Tasks
- start worker (only 1 allowed)
To start background processing process:
python manage.py process_tasks
django-rq
- background processing code
INSTALLATION
 pip install django-rq
 settings.py
INSTALLED_APPS += (
'django_rq',
)
RQ_QUEUES = {
'default': {
'HOST': 'localhost',
'PORT': 6379,
'DB': 0,
'DEFAULT_TIMEOUT': 360,
}
}
django-rq
- background processing code
import time
def rq_task(data):
print "[START] rq for: " + data.get("name","")
time.sleep(10)
print "[DONE] rq for: " + data.get("name","")
django-rq
- initiate background task
from . import rq_tasks
import django_rq
django_rq.enqueue(rq_tasks.rq_task, request.POST.copy())
django-rq
- start worker(s)
 First, start redis (e.g. /usr/local/bin/redis-server)
 To start background processing process
(supports more than 1 worker(s))
python manage.py rqworker default
Demo
Other Practical Usage of Background
Processing
Not just for response time
 Scheduling (require additional components, e.g.
RQ Scheduler)
 Enforce a separation between front-end logic and
back-end logic
 Batch processing (daily import / export of data)
 Reports generation
 Maintenance - expiring old sessions, sweeping
caches
FAQ
Chew Kok Hoor
Director
SolutionX Software Sdn Bhd
kokhoor@solutionx.com.my

More Related Content

PDF
Introduction to performance tuning perl web applications
PDF
PyGotham 2014 Introduction to Profiling
PDF
Building Scalable Websites with Perl
ODP
Choosing a Web Architecture for Perl
PPT
Zend Con 2008 Slides
PDF
04 web optimization
ODP
HTTP Basic - PHP
PDF
Scaling WordPress
Introduction to performance tuning perl web applications
PyGotham 2014 Introduction to Profiling
Building Scalable Websites with Perl
Choosing a Web Architecture for Perl
Zend Con 2008 Slides
04 web optimization
HTTP Basic - PHP
Scaling WordPress

What's hot (20)

PDF
Use Xdebug to profile PHP
PPT
Startups to Scale
PPTX
Service workers - Velocity 2016 Training
PDF
Less and faster – Cache tips for WordPress developers
PPTX
Web Performance Automation - NY Web Performance Meetup
PPTX
Getting the most out of WebPageTest
PPTX
Building a scalable online backup system in python
PDF
Go performance tooling
PDF
Alternative Infrastucture
PPTX
Performance in business terms
PDF
Functional Programming with Streams in node.js
PDF
WordPress Need For Speed
PPT
5 things MySql
PDF
Search in WordPress - how it works and howto customize it
PDF
Django and Nginx reverse proxy cache
PDF
WebPagetest Power Users - Velocity 2014
PDF
MongoDB and server performance
PPT
Web::Scraper
PDF
Preparing your web services for Android and your Android app for web services...
ODP
Frontend Caching - The "new" frontier
Use Xdebug to profile PHP
Startups to Scale
Service workers - Velocity 2016 Training
Less and faster – Cache tips for WordPress developers
Web Performance Automation - NY Web Performance Meetup
Getting the most out of WebPageTest
Building a scalable online backup system in python
Go performance tooling
Alternative Infrastucture
Performance in business terms
Functional Programming with Streams in node.js
WordPress Need For Speed
5 things MySql
Search in WordPress - how it works and howto customize it
Django and Nginx reverse proxy cache
WebPagetest Power Users - Velocity 2014
MongoDB and server performance
Web::Scraper
Preparing your web services for Android and your Android app for web services...
Frontend Caching - The "new" frontier
Ad

Similar to Background Processing - PyCon MY 2015 (20)

PDF
Profiling PHP with Xdebug / Webgrind
PDF
Making it fast: Zotonic & Performance
PPTX
Java Enterprise Performance - Unburdended Applications
PPSX
Magento performancenbs
PDF
Learned lessons in real world projects by Jordi Anguela at Mallorca Software ...
PPTX
Scaling python webapps from 0 to 50 million users - A top-down approach
PPTX
PHP Performance: Principles and tools
PPT
Front-end performances
PDF
Server Monitoring (Scaling while bootstrapped)
PPT
Scalable Apache for Beginners
PPT
WE18_Performance_Up.ppt
PPT
LatJUG. Google App Engine
ODP
Scaling Streaming - Concepts, Research, Goals
PPTX
Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
PPTX
Handling Data in Mega Scale Systems
PPTX
Getting Started with Heroku
PDF
Node js vs Django: Which is Better Backend Framework.pdf
PPTX
How NOT to get lost in the current JavaScript landscape
PPTX
Training Webinar: Detect Performance Bottlenecks of Applications
PDF
Porting Rails Apps to High Availability Systems
Profiling PHP with Xdebug / Webgrind
Making it fast: Zotonic & Performance
Java Enterprise Performance - Unburdended Applications
Magento performancenbs
Learned lessons in real world projects by Jordi Anguela at Mallorca Software ...
Scaling python webapps from 0 to 50 million users - A top-down approach
PHP Performance: Principles and tools
Front-end performances
Server Monitoring (Scaling while bootstrapped)
Scalable Apache for Beginners
WE18_Performance_Up.ppt
LatJUG. Google App Engine
Scaling Streaming - Concepts, Research, Goals
Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Handling Data in Mega Scale Systems
Getting Started with Heroku
Node js vs Django: Which is Better Backend Framework.pdf
How NOT to get lost in the current JavaScript landscape
Training Webinar: Detect Performance Bottlenecks of Applications
Porting Rails Apps to High Availability Systems
Ad

Recently uploaded (20)

PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
innovation process that make everything different.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
E -tech empowerment technologies PowerPoint
PPTX
Funds Management Learning Material for Beg
DOCX
Unit-3 cyber security network security of internet system
PPTX
artificial intelligence overview of it and more
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
Internet___Basics___Styled_ presentation
PPTX
Introduction to Information and Communication Technology
PDF
Introduction to the IoT system, how the IoT system works
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
WebRTC in SignalWire - troubleshooting media negotiation
Decoding a Decade: 10 Years of Applied CTI Discipline
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Introuction about WHO-FIC in ICD-10.pptx
PptxGenJS_Demo_Chart_20250317130215833.pptx
Cloud-Scale Log Monitoring _ Datadog.pdf
innovation process that make everything different.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
E -tech empowerment technologies PowerPoint
Funds Management Learning Material for Beg
Unit-3 cyber security network security of internet system
artificial intelligence overview of it and more
Design_with_Watersergyerge45hrbgre4top (1).ppt
Internet___Basics___Styled_ presentation
Introduction to Information and Communication Technology
Introduction to the IoT system, how the IoT system works
Power Point - Lesson 3_2.pptx grad school presentation
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Job_Card_System_Styled_lorem_ipsum_.pptx
The New Creative Director: How AI Tools for Social Media Content Creation Are...
WebRTC in SignalWire - troubleshooting media negotiation

Background Processing - PyCon MY 2015

  • 1. BACKGROUND PROCESSING TO IMPROVE RESPONSE TIME by Chew Kok Hoor kokhoor@solutionx.com.
  • 2. Python @ SolutionX  Django Web Applications  Django Mobile Apps backend  Django-based Point-of-Sales system (POS)  Device Connectors
  • 3. Issue  Issue: Webpages / Views that takes time to response  Contact Us Page  Registration Page  Possible causes:  Sending email  Slow writes to database, business rules etc.
  • 4. What happens next  Users / Visitors / Customer complains  Boss ask us to resolve it, ASAP.
  • 5. Short Term Resolution  We spin while we can
  • 6. What happens next after what happened next earlier  More Users / Visitors / Customers  Timeout errors
  • 7. Why timeout?  In Linux, we use NGINX – GUNICORN - DJANGO  There is a limit of exact concurrent request that a server can handle.  General formula for no. of workers is: (2 x [number of cores]) + 1  Therefore, 8 cores = (2 x 8) + 1 = 17  Slow response = handle less requests per second
  • 9. Pros n Cons  Disadvantages  New process required  On failure unable to notify users thru response to request.  Worse case, lost contact to user who submitted request.  Need to monitor tasks not done / failed  Advantages  Timeout issue resolved  Users get quicker response  Can choose to process N tasks at a time  Can retry tasks etc., background process can afford slower response
  • 10. What should be done? 1. Break down tasks of slow responding requests 2. Process tasks in background Preparation Stage Perform slow running task in Background  Submit task for processing  
  • 11. Tasks Breakdown Preparation Stage  record necessary information for task to execute properly or for user to track status.  activities to prevent inconsistency or breaking of business rules  preferably a means of contacting user when it is done (email, mobile notification, etc.) Slow Running tasks  Email sending  Create and setup new database  Setup new user login roles, etc.  Complex calculations or setup
  • 12. Choosing Task Queue software Software Pro Cons django-background- tasks (https://pypi.python.org/ pypi/django- background-tasks) - Very easy setup - Easy to use - Good for small volume - Uses Django ORM for backend - Absolutely terrible for medium-to-high volume RQ (http://python-rq.org/) (https://github.com/ui/dj ango-rq) - Flexible, powered by Redis - Lower memory footprint than Celery - Great for high volume - Not as many features as Celery - Medium difficulty setup - Redis only option for storage Celery (http://www.celeryprojec t.org/) - De facto Django standard - Many different storage types - Flexible, - Full-featured - Great for high volume - Challenging setup - overkill for slower traffic sites From Two Scoops of Django - Best Practices For Django 1.8 by Daniel Roy Greenfeld & Audrey Roy Greenfeld (http://twoscoopspress.org/) Choosing Task Queue Software
  • 13. Django Background Tasks - background processing code INSTALLATION  pip install django-background-tasks  settings.py INSTALLED_APPS += ( ‘background_task’, )
  • 14. Django Background Tasks - background processing code import time from background_task import background @background(schedule=0) def dbt_task(data): print "[START] django-background-tasks for: " + data.get("name","") time.sleep(10) print "[DONE] django-background-tasks for: " + data.get("name","")
  • 15. Django Background Tasks - initiate background task from . import tasks tasks.dbt_task(request.POST.copy())
  • 16. Django Background Tasks - start worker (only 1 allowed) To start background processing process: python manage.py process_tasks
  • 17. django-rq - background processing code INSTALLATION  pip install django-rq  settings.py INSTALLED_APPS += ( 'django_rq', ) RQ_QUEUES = { 'default': { 'HOST': 'localhost', 'PORT': 6379, 'DB': 0, 'DEFAULT_TIMEOUT': 360, } }
  • 18. django-rq - background processing code import time def rq_task(data): print "[START] rq for: " + data.get("name","") time.sleep(10) print "[DONE] rq for: " + data.get("name","")
  • 19. django-rq - initiate background task from . import rq_tasks import django_rq django_rq.enqueue(rq_tasks.rq_task, request.POST.copy())
  • 20. django-rq - start worker(s)  First, start redis (e.g. /usr/local/bin/redis-server)  To start background processing process (supports more than 1 worker(s)) python manage.py rqworker default
  • 21. Demo
  • 22. Other Practical Usage of Background Processing Not just for response time  Scheduling (require additional components, e.g. RQ Scheduler)  Enforce a separation between front-end logic and back-end logic  Batch processing (daily import / export of data)  Reports generation  Maintenance - expiring old sessions, sweeping caches
  • 23. FAQ Chew Kok Hoor Director SolutionX Software Sdn Bhd kokhoor@solutionx.com.my