SlideShare une entreprise Scribd logo
1  sur  68
Scaling
GraphQL
subscriptions
Artjom Kurapov
Principal Software Engineer,
Engineering Platform
🛠️ I develop in NodeJS / Go / PHP
🔨 Work on GraphQL for last 3 years
🌍 I focus on complexity, performance and scalability
🙏Open source maintainer - github.com/tot-ra
️ Interested in image recognition and
🐝 Beekeeping - github.com/Gratheon
Artjom Kurapov
Some examples you can
use to start with
Code
Architecture
Why it makes product more efficient
What they are replacing
What frontend can consume
How we now should be
able to scale
Schema
Subscriptions
GraphQL
Complexity
Expect it to grow with
every section
Questions
Ask at the end of every section
Agenda
Time
ETA 50 min
~70 slides
New York City
St. Pete
London
Dublin
Lisbon
Berlin
Prague
Tallinn
Tartu
Riga
About Pipedrive ️
>100k
clients
offices
400+
engineers
hosting regions
200+
libraries
730+
microservices
440k
rpm at peak
30+
teams
10
5
Data synchronization / async event delivery
Why your business needs this?
Intuitive UX ->
Interactivity ->
Data consistency ->
Simplicity -> Growth 🌳
Efficiency -> Retention👏
Trust -> Retention 👏
How? Pusher
How? Liveblocks
How? Firebase
Pipedrive (oversimplified) architecture
Why GraphQL?
Agenda
REST problems / schema documentation
Overfetching
Underfetching + request dependencies
Manual API composition
● Synchronous and slow
● Denormalized, Not really REST
● Complex and hard to maintain
○ Timeouts
○ Error handling
○ Conditional API joins - custom GET params
○ Redundant internal requests
○ N+1 internal requests
Multiple API composition endpoints
API composition mission 🚀
Aleksander Gasna
Oleksandr Shvechykov
Artjom Kurapov Erik Schults
Make API efficient
Add GraphQL here 💡
Apollo federation
graphql-schema-registry service
graphql-query-cost library
13% decrease of initial
pipeline page load
(5.4s → 4.7s)
25% decrease of cached request
pipeline page load (5.4s → 4s)
Mission results
What about this part?
Why subscriptions?
Why is Pipedrive is consuming
80GB traffic per day?*
*Before socketqueue traffic was compressed
Problems - denormalized undocumented schema
Problems - scalability
Problems - scalability with fixed queues
Socketqueue 1
Socketqueue 2
Socketqueue 3
queue 1
queue 2
queue 3
client 1
client 2
client 3
client 4
Problems - noisy neighbour -> CPU -> infra waste
Problems - reliability
Loss of events if RabbitMQ is down
Loss of frontend state if user connection is down
Proof of concept
● Websocket transport
● Apollo server / subscriptions-transport-ws
GraphQL subscriptions mission 🚀
Make events efficient
Kristjan Luik
Abhishek Goswami
Artjom Kurapov Pavel Nikolajev
Scope
● Authentication
● Filtering + Limiting stream to specific user
● Routing
● Enrichment with timeouts
● Handle state on connection loss
● Test multiple subscriptions per view
● Testing scalability
● Testing performance. CPU, memory
Mission progress
- Tried graph-gophers/graphql-go
- Tried 99designs/gqlgen
- Ended up still with nodejs:
apollosolutions/federation-subscription-tools
enisdenjo/graphql-ws
Dynamic federated schema merging
Schema
Mini demo
Raw events
● Delivers data from kafka
● camelCase remapping
● Permission check
● Fast & most reliable
Delta field
● Subscribe & deliver only changed fields (diff)
● Relies on original event to have this data
● JSON type - no property usage tracking
● Frontend likely needs to query original entity first
● Useful if frontend has storage layer
Event types - Universal vs Domain Driven
● Both have filtering by IDs
● Universal - for FE storage sync
○ ORDER of events is important
● Custom - flexibility for specific views
Enriched events
● Allows deep enrichment
● Queries federated gateway
● Query is hard-coded
● Entity ID is taken from kafka event
● Useful if frontend has no
unified storage layer (react views)
Live queries
● Query + Subscribe
● Async iterator magic
Architecture
Services
Scalability
● Workers scale to max amount of kafka partitions
● WS connections scale horizontally
● Redis CPU is the weakest spot
● For more efficiency, in the future,
subscription state (users, ids)
could be passed to workers
Redis pub-sub channels
● We use high-availability redis sentinel
● Routing idea is similar to RabbitMQ,
except there are no in-memory queues
● redis-cli -h localhost -p 6379
● > PUBLISH 123.deal.456 some-json-here
Company id
Entity type
Entity id
Performance testing
● One entity type ~ 200 events / sec
● Rolled out to 100% of customers
● Stress tested in live with 50 subscriptions per connection
Limitations
● Max amount of IDs per subscription
○ Redis channels per subscription
● Connection per user per pod
○ Subscriptions per connection
● Subscriptions per pod
● Enrichment request timeout
● Connection time to live
● Disconnect users
○ On logout
○ On permission change
● Automatic reconnect (by graphql-ws)
Code
Transport
Server Sent Events over HTTP2 transport
● Only unidirectional data flow
● Basically a HTTP long polling on steroids - no HTTP connection closing when
individual data item is sent, no need to explicitly re-establish connection
● Very simple to set up both client & BE
○ Built in support for re-connection and event id
● A text/event-stream with JSON payload separated by new line characters
● Adds 5 bytes per msg overhead
● Only UTF :(
● Over HTTP1, limited to 6 connections :(
● enisdenjo/graphql-sse lib could now be used - first commit Jun 22, 2021, after
mission
Websocket transport
● Bi-directional full duplex (send at any time)
● HTTP 1.x upgrade header
○ Some old firewalls may deny this
● Sub-protocols & versions
● Binary (Blob) or UTF8 (ArrayBuffer)
● Low-level, has many implementation libraries (we use sockjs in
socketqueue)
● HTTPS pages require WSS
● Stateful connection
● Nginx needs long-lived connections, otherwise it dies after default 1 min
subscriptions-transport-ws
- Originally written by Apollo
- 🪦2016-2018
graphql-ws
- Easy adoption, has both frontend and backend examples
- Security
- ws payload must be compliant to the protocol, otherwise connection is dropped
- Connection/auth initialization is complete
- Allows to even send queries & mutations over ws
- Less auth overhead with long-running connection
- Automatic reconnect, exponential back-off
- Connection termination removed -> ws equivalent
- Keep-alive ping-pongs can be customized
Frontend
Subscription (with generator function)
Subscription (with async iterator + promises)
Subscription (with in-memory pub-sub)
Subscription (with redis pub-sub)
import ws from 'ws';
import { RedisPubSub } from 'graphql-redis-subscriptions';
const wsServer = new ws.Server({
server: fastify.server,
path: '/graphql',
});
const redisSub = new RedisPubSub({
publisher: redisClient, // ioRedis instance
subscriber: redisClient,
});
Subscription with redis pub-sub
import { useServer } from 'graphql-ws/lib/use/ws';
useServer({
execute, // from 'graphql'
subscribe, // from 'graphql'
context: (ctx) => contextHandler(ctx),
onConnect: (ctx) => connectHandler(ctx, fastify, redisSub),
onDisconnect: (ctx: any) => {},
onClose: (ctx: any) => {},
onSubscribe: (ctx, msg) => subscribeHandler(ctx, fastify,
msg),
onError: (ctx, message, errors) => {},
},
wsServer,
);
Connection limits, set
redis to ctx
Setting datasources &
context from connection to
resolvers
Subscription with redis pub-sub
Subscription: {
dealAdded: {
subscribe: withFilter(
() => ctx.redis.asyncIterator('123.deal.456'), // bind to redis channel
(payload, variables) => {
return true; // check permissions
},
),
async resolve(rawPayload, _, ctx, info) => {}, // enrich
},
},
Pipedrive engineering blog
Thank you!
Any questions? Contact me!
Some code is available in my pet project:
🐝 github.com/Gratheon/event-stream-filter
🐝 github.com/Gratheon/web-app
Artjom Kurapov
artkurapov@gmail.com

Contenu connexe

Tendances

Agile Transformation in Telco Guide
Agile Transformation in Telco GuideAgile Transformation in Telco Guide
Agile Transformation in Telco GuideACM
 
Airflow Clustering and High Availability
Airflow Clustering and High AvailabilityAirflow Clustering and High Availability
Airflow Clustering and High AvailabilityRobert Sanders
 
ksqlDB - Stream Processing simplified!
ksqlDB - Stream Processing simplified!ksqlDB - Stream Processing simplified!
ksqlDB - Stream Processing simplified!Guido Schmutz
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureJosé Paumard
 
Circuit Breaker Pattern
Circuit Breaker PatternCircuit Breaker Pattern
Circuit Breaker PatternTung Nguyen
 
ksqlDB: Building Consciousness on Real Time Events
ksqlDB: Building Consciousness on Real Time EventsksqlDB: Building Consciousness on Real Time Events
ksqlDB: Building Consciousness on Real Time Eventsconfluent
 
Microservices Part 3 Service Mesh and Kafka
Microservices Part 3 Service Mesh and KafkaMicroservices Part 3 Service Mesh and Kafka
Microservices Part 3 Service Mesh and KafkaAraf Karsh Hamid
 
Scaling your Data Pipelines with Apache Spark on Kubernetes
Scaling your Data Pipelines with Apache Spark on KubernetesScaling your Data Pipelines with Apache Spark on Kubernetes
Scaling your Data Pipelines with Apache Spark on KubernetesDatabricks
 
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안SANG WON PARK
 
Validating Delivered Business Value – Going Beyond “Actual Business Value”
Validating Delivered Business Value – Going Beyond “Actual Business Value”Validating Delivered Business Value – Going Beyond “Actual Business Value”
Validating Delivered Business Value – Going Beyond “Actual Business Value”Yuval Yeret
 
WebLogic im neuen Gewand
WebLogic im neuen GewandWebLogic im neuen Gewand
WebLogic im neuen GewandVolker Linz
 
Raffi Krikorian, Twitter Timelines at Scale
Raffi Krikorian, Twitter Timelines at ScaleRaffi Krikorian, Twitter Timelines at Scale
Raffi Krikorian, Twitter Timelines at ScaleMariano Amartino
 
Service Mesh - Observability
Service Mesh - ObservabilityService Mesh - Observability
Service Mesh - ObservabilityAraf Karsh Hamid
 
10 Essential SAFe(tm) patterns you should focus on when scaling Agile
10 Essential SAFe(tm) patterns you should focus on when scaling Agile10 Essential SAFe(tm) patterns you should focus on when scaling Agile
10 Essential SAFe(tm) patterns you should focus on when scaling AgileYuval Yeret
 
Agile evolution lifecycle - From implementing Agile to being Agile
Agile evolution lifecycle - From implementing Agile to being AgileAgile evolution lifecycle - From implementing Agile to being Agile
Agile evolution lifecycle - From implementing Agile to being AgileMichal Epstein
 
Agile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven DesignAgile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven DesignAraf Karsh Hamid
 

Tendances (20)

Intro to GraphQL
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
 
Agile Transformation in Telco Guide
Agile Transformation in Telco GuideAgile Transformation in Telco Guide
Agile Transformation in Telco Guide
 
Airflow Clustering and High Availability
Airflow Clustering and High AvailabilityAirflow Clustering and High Availability
Airflow Clustering and High Availability
 
ksqlDB - Stream Processing simplified!
ksqlDB - Stream Processing simplified!ksqlDB - Stream Processing simplified!
ksqlDB - Stream Processing simplified!
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
 
Circuit Breaker Pattern
Circuit Breaker PatternCircuit Breaker Pattern
Circuit Breaker Pattern
 
ksqlDB: Building Consciousness on Real Time Events
ksqlDB: Building Consciousness on Real Time EventsksqlDB: Building Consciousness on Real Time Events
ksqlDB: Building Consciousness on Real Time Events
 
Agile Transformation at Scale
Agile Transformation at ScaleAgile Transformation at Scale
Agile Transformation at Scale
 
Microservices Part 3 Service Mesh and Kafka
Microservices Part 3 Service Mesh and KafkaMicroservices Part 3 Service Mesh and Kafka
Microservices Part 3 Service Mesh and Kafka
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Scaling your Data Pipelines with Apache Spark on Kubernetes
Scaling your Data Pipelines with Apache Spark on KubernetesScaling your Data Pipelines with Apache Spark on Kubernetes
Scaling your Data Pipelines with Apache Spark on Kubernetes
 
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
 
Validating Delivered Business Value – Going Beyond “Actual Business Value”
Validating Delivered Business Value – Going Beyond “Actual Business Value”Validating Delivered Business Value – Going Beyond “Actual Business Value”
Validating Delivered Business Value – Going Beyond “Actual Business Value”
 
WebLogic im neuen Gewand
WebLogic im neuen GewandWebLogic im neuen Gewand
WebLogic im neuen Gewand
 
Raffi Krikorian, Twitter Timelines at Scale
Raffi Krikorian, Twitter Timelines at ScaleRaffi Krikorian, Twitter Timelines at Scale
Raffi Krikorian, Twitter Timelines at Scale
 
Service Mesh - Observability
Service Mesh - ObservabilityService Mesh - Observability
Service Mesh - Observability
 
10 Essential SAFe(tm) patterns you should focus on when scaling Agile
10 Essential SAFe(tm) patterns you should focus on when scaling Agile10 Essential SAFe(tm) patterns you should focus on when scaling Agile
10 Essential SAFe(tm) patterns you should focus on when scaling Agile
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Agile evolution lifecycle - From implementing Agile to being Agile
Agile evolution lifecycle - From implementing Agile to being AgileAgile evolution lifecycle - From implementing Agile to being Agile
Agile evolution lifecycle - From implementing Agile to being Agile
 
Agile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven DesignAgile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven Design
 

Similaire à Scaling GraphQL Subscriptions

Slow things down to make them go faster [FOSDEM 2022]
Slow things down to make them go faster [FOSDEM 2022]Slow things down to make them go faster [FOSDEM 2022]
Slow things down to make them go faster [FOSDEM 2022]Jimmy Angelakos
 
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...SQUADEX
 
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...Amazon Web Services
 
Unified Stream Processing at Scale with Apache Samza - BDS2017
Unified Stream Processing at Scale with Apache Samza - BDS2017Unified Stream Processing at Scale with Apache Samza - BDS2017
Unified Stream Processing at Scale with Apache Samza - BDS2017Jacob Maes
 
Kafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupKafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupMingmin Chen
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...HostedbyConfluent
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to StreamingBravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to StreamingYaroslav Tkachenko
 
GE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoTGE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoTKai Zhao
 
How Uber scaled its Real Time Infrastructure to Trillion events per day
How Uber scaled its Real Time Infrastructure to Trillion events per dayHow Uber scaled its Real Time Infrastructure to Trillion events per day
How Uber scaled its Real Time Infrastructure to Trillion events per dayDataWorks Summit
 
Hadoop summit - Scaling Uber’s Real-Time Infra for Trillion Events per Day
Hadoop summit - Scaling Uber’s Real-Time Infra for  Trillion Events per DayHadoop summit - Scaling Uber’s Real-Time Infra for  Trillion Events per Day
Hadoop summit - Scaling Uber’s Real-Time Infra for Trillion Events per DayAnkur Bansal
 
Event Driven Microservices
Event Driven MicroservicesEvent Driven Microservices
Event Driven MicroservicesFabrizio Fortino
 
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ UberKafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uberconfluent
 
Performance testing in scope of migration to cloud by Serghei Radov
Performance testing in scope of migration to cloud by Serghei RadovPerformance testing in scope of migration to cloud by Serghei Radov
Performance testing in scope of migration to cloud by Serghei RadovValeriia Maliarenko
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...François-Guillaume Ribreau
 
Node.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scaleNode.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scaleDmytro Semenov
 
Introduction to kubernetes
Introduction to kubernetesIntroduction to kubernetes
Introduction to kubernetesRishabh Indoria
 
Zero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesZero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesAlexander Penev
 
Kafka elastic search meetup 09242018
Kafka elastic search meetup 09242018Kafka elastic search meetup 09242018
Kafka elastic search meetup 09242018Ying Xu
 

Similaire à Scaling GraphQL Subscriptions (20)

Slow things down to make them go faster [FOSDEM 2022]
Slow things down to make them go faster [FOSDEM 2022]Slow things down to make them go faster [FOSDEM 2022]
Slow things down to make them go faster [FOSDEM 2022]
 
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
Tooling for Machine Learning: AWS Products, Open Source Tools, and DevOps Pra...
 
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
How Netflix Uses Amazon Kinesis Streams to Monitor and Optimize Large-scale N...
 
Unified Stream Processing at Scale with Apache Samza - BDS2017
Unified Stream Processing at Scale with Apache Samza - BDS2017Unified Stream Processing at Scale with Apache Samza - BDS2017
Unified Stream Processing at Scale with Apache Samza - BDS2017
 
Kafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupKafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetup
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streamin...
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to StreamingBravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
 
GE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoTGE Predix 新手入门 赵锴 物联网_IoT
GE Predix 新手入门 赵锴 物联网_IoT
 
Netty training
Netty trainingNetty training
Netty training
 
How Uber scaled its Real Time Infrastructure to Trillion events per day
How Uber scaled its Real Time Infrastructure to Trillion events per dayHow Uber scaled its Real Time Infrastructure to Trillion events per day
How Uber scaled its Real Time Infrastructure to Trillion events per day
 
Hadoop summit - Scaling Uber’s Real-Time Infra for Trillion Events per Day
Hadoop summit - Scaling Uber’s Real-Time Infra for  Trillion Events per DayHadoop summit - Scaling Uber’s Real-Time Infra for  Trillion Events per Day
Hadoop summit - Scaling Uber’s Real-Time Infra for Trillion Events per Day
 
Netty training
Netty trainingNetty training
Netty training
 
Event Driven Microservices
Event Driven MicroservicesEvent Driven Microservices
Event Driven Microservices
 
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ UberKafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
Kafka Summit NYC 2017 - Scalable Real-Time Complex Event Processing @ Uber
 
Performance testing in scope of migration to cloud by Serghei Radov
Performance testing in scope of migration to cloud by Serghei RadovPerformance testing in scope of migration to cloud by Serghei Radov
Performance testing in scope of migration to cloud by Serghei Radov
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
 
Node.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scaleNode.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scale
 
Introduction to kubernetes
Introduction to kubernetesIntroduction to kubernetes
Introduction to kubernetes
 
Zero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesZero Downtime JEE Architectures
Zero Downtime JEE Architectures
 
Kafka elastic search meetup 09242018
Kafka elastic search meetup 09242018Kafka elastic search meetup 09242018
Kafka elastic search meetup 09242018
 

Plus de Артём Курапов (9)

Variety of automated tests
Variety of automated testsVariety of automated tests
Variety of automated tests
 
Symfony
SymfonySymfony
Symfony
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
Php storm intro
Php storm introPhp storm intro
Php storm intro
 
Android intro
Android introAndroid intro
Android intro
 
В облаке AWS
В облаке AWSВ облаке AWS
В облаке AWS
 
Devclub hääletamine
Devclub hääletamineDevclub hääletamine
Devclub hääletamine
 
Visualization of evolutionary cascades of messages using force-directed graphs
Visualization of evolutionary cascades of messages using force-directed graphsVisualization of evolutionary cascades of messages using force-directed graphs
Visualization of evolutionary cascades of messages using force-directed graphs
 
OAuthоризация и API социальных сетей
OAuthоризация и API социальных сетейOAuthоризация и API социальных сетей
OAuthоризация и API социальных сетей
 

Dernier

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Dernier (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

Scaling GraphQL Subscriptions

  • 2. 🛠️ I develop in NodeJS / Go / PHP 🔨 Work on GraphQL for last 3 years 🌍 I focus on complexity, performance and scalability 🙏Open source maintainer - github.com/tot-ra ️ Interested in image recognition and 🐝 Beekeeping - github.com/Gratheon Artjom Kurapov
  • 3. Some examples you can use to start with Code Architecture Why it makes product more efficient What they are replacing What frontend can consume How we now should be able to scale Schema Subscriptions GraphQL Complexity Expect it to grow with every section Questions Ask at the end of every section Agenda Time ETA 50 min ~70 slides
  • 4.
  • 5. New York City St. Pete London Dublin Lisbon Berlin Prague Tallinn Tartu Riga About Pipedrive ️ >100k clients offices 400+ engineers hosting regions 200+ libraries 730+ microservices 440k rpm at peak 30+ teams 10 5
  • 6.
  • 7. Data synchronization / async event delivery
  • 8. Why your business needs this? Intuitive UX -> Interactivity -> Data consistency -> Simplicity -> Growth 🌳 Efficiency -> Retention👏 Trust -> Retention 👏
  • 14. REST problems / schema documentation
  • 16. Underfetching + request dependencies
  • 17. Manual API composition ● Synchronous and slow ● Denormalized, Not really REST ● Complex and hard to maintain ○ Timeouts ○ Error handling ○ Conditional API joins - custom GET params ○ Redundant internal requests ○ N+1 internal requests
  • 19. API composition mission 🚀 Aleksander Gasna Oleksandr Shvechykov Artjom Kurapov Erik Schults Make API efficient
  • 24. 13% decrease of initial pipeline page load (5.4s → 4.7s) 25% decrease of cached request pipeline page load (5.4s → 4s) Mission results
  • 27. Why is Pipedrive is consuming 80GB traffic per day?* *Before socketqueue traffic was compressed
  • 28. Problems - denormalized undocumented schema
  • 30. Problems - scalability with fixed queues Socketqueue 1 Socketqueue 2 Socketqueue 3 queue 1 queue 2 queue 3 client 1 client 2 client 3 client 4
  • 31. Problems - noisy neighbour -> CPU -> infra waste
  • 32. Problems - reliability Loss of events if RabbitMQ is down Loss of frontend state if user connection is down
  • 33. Proof of concept ● Websocket transport ● Apollo server / subscriptions-transport-ws
  • 34. GraphQL subscriptions mission 🚀 Make events efficient Kristjan Luik Abhishek Goswami Artjom Kurapov Pavel Nikolajev
  • 35. Scope ● Authentication ● Filtering + Limiting stream to specific user ● Routing ● Enrichment with timeouts ● Handle state on connection loss ● Test multiple subscriptions per view ● Testing scalability ● Testing performance. CPU, memory
  • 36. Mission progress - Tried graph-gophers/graphql-go - Tried 99designs/gqlgen - Ended up still with nodejs: apollosolutions/federation-subscription-tools enisdenjo/graphql-ws Dynamic federated schema merging
  • 39. Raw events ● Delivers data from kafka ● camelCase remapping ● Permission check ● Fast & most reliable
  • 40. Delta field ● Subscribe & deliver only changed fields (diff) ● Relies on original event to have this data ● JSON type - no property usage tracking ● Frontend likely needs to query original entity first ● Useful if frontend has storage layer
  • 41. Event types - Universal vs Domain Driven ● Both have filtering by IDs ● Universal - for FE storage sync ○ ORDER of events is important ● Custom - flexibility for specific views
  • 42. Enriched events ● Allows deep enrichment ● Queries federated gateway ● Query is hard-coded ● Entity ID is taken from kafka event ● Useful if frontend has no unified storage layer (react views)
  • 43. Live queries ● Query + Subscribe ● Async iterator magic
  • 46. Scalability ● Workers scale to max amount of kafka partitions ● WS connections scale horizontally ● Redis CPU is the weakest spot ● For more efficiency, in the future, subscription state (users, ids) could be passed to workers
  • 47. Redis pub-sub channels ● We use high-availability redis sentinel ● Routing idea is similar to RabbitMQ, except there are no in-memory queues ● redis-cli -h localhost -p 6379 ● > PUBLISH 123.deal.456 some-json-here Company id Entity type Entity id
  • 48. Performance testing ● One entity type ~ 200 events / sec ● Rolled out to 100% of customers ● Stress tested in live with 50 subscriptions per connection
  • 49. Limitations ● Max amount of IDs per subscription ○ Redis channels per subscription ● Connection per user per pod ○ Subscriptions per connection ● Subscriptions per pod ● Enrichment request timeout ● Connection time to live ● Disconnect users ○ On logout ○ On permission change ● Automatic reconnect (by graphql-ws)
  • 50. Code
  • 52. Server Sent Events over HTTP2 transport ● Only unidirectional data flow ● Basically a HTTP long polling on steroids - no HTTP connection closing when individual data item is sent, no need to explicitly re-establish connection ● Very simple to set up both client & BE ○ Built in support for re-connection and event id ● A text/event-stream with JSON payload separated by new line characters ● Adds 5 bytes per msg overhead ● Only UTF :( ● Over HTTP1, limited to 6 connections :( ● enisdenjo/graphql-sse lib could now be used - first commit Jun 22, 2021, after mission
  • 53. Websocket transport ● Bi-directional full duplex (send at any time) ● HTTP 1.x upgrade header ○ Some old firewalls may deny this ● Sub-protocols & versions ● Binary (Blob) or UTF8 (ArrayBuffer) ● Low-level, has many implementation libraries (we use sockjs in socketqueue) ● HTTPS pages require WSS ● Stateful connection ● Nginx needs long-lived connections, otherwise it dies after default 1 min
  • 55. graphql-ws - Easy adoption, has both frontend and backend examples - Security - ws payload must be compliant to the protocol, otherwise connection is dropped - Connection/auth initialization is complete - Allows to even send queries & mutations over ws - Less auth overhead with long-running connection - Automatic reconnect, exponential back-off - Connection termination removed -> ws equivalent - Keep-alive ping-pongs can be customized
  • 57.
  • 59.
  • 60. Subscription (with async iterator + promises)
  • 61.
  • 63.
  • 64. Subscription (with redis pub-sub) import ws from 'ws'; import { RedisPubSub } from 'graphql-redis-subscriptions'; const wsServer = new ws.Server({ server: fastify.server, path: '/graphql', }); const redisSub = new RedisPubSub({ publisher: redisClient, // ioRedis instance subscriber: redisClient, });
  • 65. Subscription with redis pub-sub import { useServer } from 'graphql-ws/lib/use/ws'; useServer({ execute, // from 'graphql' subscribe, // from 'graphql' context: (ctx) => contextHandler(ctx), onConnect: (ctx) => connectHandler(ctx, fastify, redisSub), onDisconnect: (ctx: any) => {}, onClose: (ctx: any) => {}, onSubscribe: (ctx, msg) => subscribeHandler(ctx, fastify, msg), onError: (ctx, message, errors) => {}, }, wsServer, ); Connection limits, set redis to ctx Setting datasources & context from connection to resolvers
  • 66. Subscription with redis pub-sub Subscription: { dealAdded: { subscribe: withFilter( () => ctx.redis.asyncIterator('123.deal.456'), // bind to redis channel (payload, variables) => { return true; // check permissions }, ), async resolve(rawPayload, _, ctx, info) => {}, // enrich }, },
  • 68. Thank you! Any questions? Contact me! Some code is available in my pet project: 🐝 github.com/Gratheon/event-stream-filter 🐝 github.com/Gratheon/web-app Artjom Kurapov artkurapov@gmail.com

Notes de l'éditeur

  1. Show of hands how many people use GraphQL in production..? And how many use subscriptions?
  2. This is how you can use our palette to build charts and graphs.
  3. This is a section separator. The text block has a green background to cover the white line behind. Please write headings and subheadings within the same text block (as shown).
  4. This is a regular base template and how it looks with illustrations.
  5. This is a regular base template and how it looks with illustrations.
  6. This is a regular base template and how it looks with illustrations.
  7. This is a regular base template and how it looks with illustrations.
  8. This is a regular base template and how it looks with illustrations.
  9. This is a regular base template and how it looks with illustrations.
  10. This is a regular base template and how it looks with illustrations.
  11. This is an alternative title slide. Use these for openings, introductions and new sections whenever there are multiple speakers is a general topic where no key images are necessary
  12. This is a section separator. The text block has a green background to cover the white line behind. Please write headings and subheadings within the same text block (as shown).
  13. This is a section separator. The text block has a green background to cover the white line behind. Please write headings and subheadings within the same text block (as shown).
  14. Here’s an example of how the closing slide could look. Use these for endings whenever there are multiple speakers.