Slide 1

Slide 1 text

BERNARDO AMORIM | 2023-10-26 Optimizing performance of Phoenix Apps

Slide 2

Slide 2 text

“I made this one longer only because I did not have the leisure to make it shorter.” BLAISE PASCAL

Slide 3

Slide 3 text

Used Tools • • • • • • • • • • • • • • Environment Setup Toxiproxy - Network Latency K6 - Load Generation / Testing Ballerina Lang - Created "microservices" for testing Observability Grafana Grafana Tempo Prometheus Elixir Profiling & Benchmark fprof + erlgrind + gprof2dot + dot eflambe + speedscope Database Profiling Postgres' EXPLAIN PEV2

Slide 4

Slide 4 text

A 3-tiered application using Postgres, Ecto, LiveView (or maybe just a JSON or GraphQL API) WHAT IS A PHOENIX APP? Data Layer Application Layer Presentation Layer Postgres Phoenix Contexts + Ecto LiveView/Absinthe/Controller

Slide 5

Slide 5 text

OUR DEMO APP: MODEL posts BIGSERIAL id VARCHAR title TEXT content BIGINT topic_id TIMESTAMP inserted_at TIMESTAMP updated_at topics BIGSERIAL id VARCHAR name BIGINT parent_id TIMESTAMP inserted_at TIMESTAMP updated_at post_contributors BIGSERIAL id BIGINT post_id BIGINT user_id BOOLEAN author users BIGSERIAL id CITEXT email VARCHAR hashed_password TIMESTAMP confirmed_at TIMESTAMP inserted_at TIMESTAMP updated_at users_tokens BIGSERIAL id BIGINT user_id BYTEA token VARCHAR context VARCHAR sent_to TIMESTAMP inserted_at topic_id post_id user_id user_id parent_id

Slide 6

Slide 6 text

OUR DEMO APP

Slide 7

Slide 7 text

PERFORMANCE IMPROVEMENT LIFE CYCLE DISCOVERY PROFILE OPTIMIZE • • User Reports Monitoring • • • Distributed Tracing Lang/VM specific tools EXPLAIN ANALYZE • • • Batching / Preloading Database Indexes Faster Algorithms

Slide 8

Slide 8 text

Discovery

Slide 9

Slide 9 text

• • Unified observability standards and SDKs Combines metrics, logs, and traces OpenTelemetry • • • Trace: Lifecycle of a request across services Span: Single unit of work in a trace Event: Specific action within a span Distributed Tracing OPENTELEMETRY

Slide 10

Slide 10 text

OBSERVABILITY STACK Honeycomb.io Grafana Stack

Slide 11

Slide 11 text

• Some problems only became visible with larger data sets Data Size PRODUCTION VS DEV • Some problems only happen when there are concurrent requests coming from many users Load • Latencies between services can make some problems much worse Latency

Slide 12

Slide 12 text

Simulate Production

Slide 13

Slide 13 text

Profile N+1

Slide 14

Slide 14 text

Optimize N+1

Slide 15

Slide 15 text

Profile Slow Algorithm

Slide 16

Slide 16 text

• • Built into OTP Pair with erlgrind & gprof2dot (or qcachegrind) for nice visualization fprof • Pair with speedscope for nice flame graph visualization eflambe

Slide 17

Slide 17 text

Optimize Slow Algorithm

Slide 18

Slide 18 text

1 Level

Slide 19

Slide 19 text

2 Levels

Slide 20

Slide 20 text

3 Levels

Slide 21

Slide 21 text

4 Levels

Slide 22

Slide 22 text

2 Wide Levels

Slide 23

Slide 23 text

Profile HOST | DATE Slow SELECT

Slide 24

Slide 24 text

Optimize HOST | DATE Slow SELECT

Slide 25

Slide 25 text

Profile HOST | DATE Slow DELETE

Slide 26

Slide 26 text

Optimize HOST | DATE Slow DELETE

Slide 27

Slide 27 text

Profile HOST | DATE GenServer Bottleneck

Slide 28

Slide 28 text

Optimize HOST | DATE GenServer Bottleneck

Slide 29

Slide 29 text

• • • • • OTEL Tracing alone will make it explicit Solution is normally batching/preloading Ecto: preload LiveView: update_many Absinthe: DataLoader N+1 SUMMARY • • • Requires lower level profiling fprof, cprof, eprof, eflambe Solution will vary, but a lot of the times some preprocessing goes a long way Slow Code • • • • Distributed tracing can help pointing out the specific query to analyse Postgres EXPLAIN will give you insight on what is wrong PEV2 will help you visualize it Solutions will vary: indexes, data denormalization, preprocessing, extra conditions to reduce result set Slow Query

Slide 30

Slide 30 text

Used Tools • • • • • • • • • • • • • • Environment Setup Toxiproxy - Network Latency K6 - Load Generation / Testing Ballerina Lang - Created "microservices" for testing Observability Grafana Grafana Tempo Prometheus Elixir Profiling & Benchmark fprof + erlgrind + gprof2dot + dot eflambe + speedscope Database Profiling Postgres' EXPLAIN PEV2

Slide 31

Slide 31 text

Thank you KEEP IN TOUCH github.com/bamorim x.com/BernardoDCGA linkedin.com/in/bdornellas