SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

Fly Weight Design Pattern.pptx
Fly Weight Design Pattern.pptxFly Weight Design Pattern.pptx
Fly Weight Design Pattern.pptxSaifullah568810
 
객체지향적인 도메인 레이어 구축하기
객체지향적인 도메인 레이어 구축하기객체지향적인 도메인 레이어 구축하기
객체지향적인 도메인 레이어 구축하기Young-Ho Cho
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
Introduction to Testcontainers
Introduction to TestcontainersIntroduction to Testcontainers
Introduction to TestcontainersVMware Tanzu
 
JPA Week3 Entity Mapping / Hexagonal Architecture
JPA Week3 Entity Mapping / Hexagonal ArchitectureJPA Week3 Entity Mapping / Hexagonal Architecture
JPA Week3 Entity Mapping / Hexagonal ArchitectureCovenant Ko
 
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...Frans Rosén
 
이벤트 기반 분산 시스템을 향한 여정
이벤트 기반 분산 시스템을 향한 여정이벤트 기반 분산 시스템을 향한 여정
이벤트 기반 분산 시스템을 향한 여정Arawn Park
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Mario Cardinal
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpaStaples
 
АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?» Online QADay ...
АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?»  Online QADay ...АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?»  Online QADay ...
АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?» Online QADay ...GoQA
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsYura Nosenko
 
스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략if kakao
 
Pentesting Modern Web Apps: A Primer
Pentesting Modern Web Apps: A PrimerPentesting Modern Web Apps: A Primer
Pentesting Modern Web Apps: A PrimerBrian Hysell
 
파이썬 플라스크 이해하기
파이썬 플라스크 이해하기 파이썬 플라스크 이해하기
파이썬 플라스크 이해하기 Yong Joon Moon
 
Java11 New Features
Java11 New FeaturesJava11 New Features
Java11 New FeaturesHaim Michael
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API07.pallav
 

What's hot (20)

Fly Weight Design Pattern.pptx
Fly Weight Design Pattern.pptxFly Weight Design Pattern.pptx
Fly Weight Design Pattern.pptx
 
객체지향적인 도메인 레이어 구축하기
객체지향적인 도메인 레이어 구축하기객체지향적인 도메인 레이어 구축하기
객체지향적인 도메인 레이어 구축하기
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Introduction to Testcontainers
Introduction to TestcontainersIntroduction to Testcontainers
Introduction to Testcontainers
 
JPA Week3 Entity Mapping / Hexagonal Architecture
JPA Week3 Entity Mapping / Hexagonal ArchitectureJPA Week3 Entity Mapping / Hexagonal Architecture
JPA Week3 Entity Mapping / Hexagonal Architecture
 
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
 
이벤트 기반 분산 시스템을 향한 여정
이벤트 기반 분산 시스템을 향한 여정이벤트 기반 분산 시스템을 향한 여정
이벤트 기반 분산 시스템을 향한 여정
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
 
АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?» Online QADay ...
АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?»  Online QADay ...АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?»  Online QADay ...
АНТОН СЕРПУТЬКО « Якісний перфоманс сценарій в Jmeter, це як?» Online QADay ...
 
Spring Batch
Spring BatchSpring Batch
Spring Batch
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
 
스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략
 
Pentesting Modern Web Apps: A Primer
Pentesting Modern Web Apps: A PrimerPentesting Modern Web Apps: A Primer
Pentesting Modern Web Apps: A Primer
 
Spring aop
Spring aopSpring aop
Spring aop
 
파이썬 플라스크 이해하기
파이썬 플라스크 이해하기 파이썬 플라스크 이해하기
파이썬 플라스크 이해하기
 
Java11 New Features
Java11 New FeaturesJava11 New Features
Java11 New Features
 
Spring AOP in Nutshell
Spring AOP in Nutshell Spring AOP in Nutshell
Spring AOP in Nutshell
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Extreme DDD modelling
Extreme DDD modellingExtreme DDD modelling
Extreme DDD modelling
 

Similar to 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
 

Similar to 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
 

More from Артём Курапов (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 социальных сетей
 

Recently uploaded

Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2ChandrakantDivate1
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...dannyijwest
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdfVinayVadlagattu
 
Compressing and Sparsifying LLM in GenAI Applications
Compressing and Sparsifying LLM in GenAI ApplicationsCompressing and Sparsifying LLM in GenAI Applications
Compressing and Sparsifying LLM in GenAI ApplicationsMFatihSIRA
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptamrabdallah9
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Payal Garg #K09
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 

Recently uploaded (20)

Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Compressing and Sparsifying LLM in GenAI Applications
Compressing and Sparsifying LLM in GenAI ApplicationsCompressing and Sparsifying LLM in GenAI Applications
Compressing and Sparsifying LLM in GenAI Applications
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 

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

Editor's Notes

  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.