SlideShare uma empresa Scribd logo
1 de 10
Baixar para ler offline
Fazendo milagres com uma
instância
Amazon ec2 T2.MICRO e
uma aplicação DJANGO
Dois problemas em uma Amazon ec2 t2.micro
- CPU Stolen: roubo de processamento
- acontece quando o uso de CPU é maior que o uso de
de CPU contratada.
https://www.google.com.br/?ion=1&espv=2#q=cpu%20stolen%20ec2%20micro
- Memória
Use REDIS para guardar suas sessões
e elimine uma consulta no banco de dados.
e também muitas outras.
Use .iterators(), .exists() e .count()
O queryset cache é um problema se você não precisa de todos os resultados
mais de uma vez.
# O `iterator()` method garante que apenas uma linha por vez seja retornada,
salvando memória.
for p in Produto.objects.all().iterator():
print(p.name)
if Produto.objects.all().exists(): # não salva todo retorno em memória
print(‘existe’)
print Produto.objects.count() # muiiito melhor que len(Produto.objects.all())
Use select_related e prefetch_related
class ModelA(models.Model):
nameA = models.CharField(max_length=255)
class ModelB(models.Model):
modela = ForeignKey(ModelA)
nameB = models.CharField(max_length=255)
class ModelC(models.Model):
mm = ManyToManyField(ModelA)
Use select_related e prefetch_related
for b in ModelB.objects.all():
print b.modela.nameA # lento aqui
for b in ModelB.objects.select_related('modela').all(): # Forward FK relationship
print b.modela.nameA # rápido agora (JOINS por trás)
# --------------------------------------------------------------------
for a in ModelA.objects.all():
for b in a.modelb_set.all(): # lento aqui (um hit no banco a cada iteração)
print b.nameB
for in ModelA.objects.prefetch_related('modelb_set').all() # Reverse FK relationship
for b in a.modelb_set.all(): # rápido agora (carregado com um SELECT IN por trás)
print b.nameB
Use select_related e prefetch_related
for c in ModelC.objects.all():
for a in c.mm.all(): # lento aqui (um hit no banco a cada iteração)
print a.nameA
for in ModelC.objects.prefetch_related(mm).all() # ManyToMany relationship
for a in c.mm.all(): # rápido agora (carregado com um SELECT IN por trás)
print a.nameA
Separe tudo
Use um servidor de banco de dados separado da aplicação (Amzon RDS por ex)
Use um servidor de mídia separado separado da aplicação (Amazon S3 por ex)
Use um servidor de cache separado separado da aplicação (Amazon
ElastiCache)
Use um servidor para buscas internas separado da aplicação (Um Elasticsearch
rodando em outra VM)
Faça uma partição SWAP
Use cache sempre
O uso de cache evita processamento pesado na máquina, deixando ela bem
tranquila.
Do próprio django:
- per-view cache
- Template fragment caching
Terceiros: Varnish

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Fazendo milagres com uma instância Amazon ec2 T2.MICRO e uma aplicação DJANGO

  • 1. Fazendo milagres com uma instância Amazon ec2 T2.MICRO e uma aplicação DJANGO
  • 2. Dois problemas em uma Amazon ec2 t2.micro - CPU Stolen: roubo de processamento - acontece quando o uso de CPU é maior que o uso de de CPU contratada. https://www.google.com.br/?ion=1&espv=2#q=cpu%20stolen%20ec2%20micro - Memória
  • 3. Use REDIS para guardar suas sessões e elimine uma consulta no banco de dados. e também muitas outras.
  • 4. Use .iterators(), .exists() e .count() O queryset cache é um problema se você não precisa de todos os resultados mais de uma vez. # O `iterator()` method garante que apenas uma linha por vez seja retornada, salvando memória. for p in Produto.objects.all().iterator(): print(p.name) if Produto.objects.all().exists(): # não salva todo retorno em memória print(‘existe’) print Produto.objects.count() # muiiito melhor que len(Produto.objects.all())
  • 5. Use select_related e prefetch_related class ModelA(models.Model): nameA = models.CharField(max_length=255) class ModelB(models.Model): modela = ForeignKey(ModelA) nameB = models.CharField(max_length=255) class ModelC(models.Model): mm = ManyToManyField(ModelA)
  • 6. Use select_related e prefetch_related for b in ModelB.objects.all(): print b.modela.nameA # lento aqui for b in ModelB.objects.select_related('modela').all(): # Forward FK relationship print b.modela.nameA # rápido agora (JOINS por trás) # -------------------------------------------------------------------- for a in ModelA.objects.all(): for b in a.modelb_set.all(): # lento aqui (um hit no banco a cada iteração) print b.nameB for in ModelA.objects.prefetch_related('modelb_set').all() # Reverse FK relationship for b in a.modelb_set.all(): # rápido agora (carregado com um SELECT IN por trás) print b.nameB
  • 7. Use select_related e prefetch_related for c in ModelC.objects.all(): for a in c.mm.all(): # lento aqui (um hit no banco a cada iteração) print a.nameA for in ModelC.objects.prefetch_related(mm).all() # ManyToMany relationship for a in c.mm.all(): # rápido agora (carregado com um SELECT IN por trás) print a.nameA
  • 8. Separe tudo Use um servidor de banco de dados separado da aplicação (Amzon RDS por ex) Use um servidor de mídia separado separado da aplicação (Amazon S3 por ex) Use um servidor de cache separado separado da aplicação (Amazon ElastiCache) Use um servidor para buscas internas separado da aplicação (Um Elasticsearch rodando em outra VM)
  • 10. Use cache sempre O uso de cache evita processamento pesado na máquina, deixando ela bem tranquila. Do próprio django: - per-view cache - Template fragment caching Terceiros: Varnish