graph-node
is an open source software that indexes blockchain data, as known as indexer. Though the cost of running a self-hosted graph node could be pretty high. We're going to deploy a self-hosted graph node on Amazon Elastic Kubernetes Service (EKS).
In this article, we have two approaches to deploy self-hosted graph node:
- A single graph node with a single PostgreSQL database
- A graph node cluster with a primary-secondary PostgreSQL cluster
- A graph node cluster consists of one index node and multiple query nodes
- For the database, we will simply use a Multi-AZ DB cluster on AWS RDS
ref:
https://github.com/graphprotocol/graph-node
Create a PostgreSQL Database on AWS RDS
Hardware requirements for running a graph-node:
https://thegraph.com/docs/en/network/indexing/#what-are-the-hardware-requirements
https://docs.thegraph.academy/official-docs/indexer/testnet/graph-protocol-testnet-baremetal/1_architectureconsiderations
A Single DB Instance
We use the following settings on staging.
- Version:
PostgreSQL 13.7-R1
- Template:
Dev/Test
- Deployment option:
Single DB instance
- DB instance identifier:
graph-node
- Master username:
graph_node
- Auto generate a password:
Yes
- DB instance class:
db.t3.medium
(2 vCPU 4G RAM)
- Storage type:
gp2
- Allocated storage:
500 GB
- Enable storage autoscaling:
No
- Compute resource:
Don’t connect to an EC2 compute resource
- Network type:
IPv4
- VPC:
eksctl-perp-staging-cluster/VPC
- DB Subnet group:
default-vpc
- Public access:
Yes
- VPC security group:
graph-node
- Availability Zone:
ap-northeast-1d
- Initial database name:
graph_node
A Multi-AZ DB Cluster
We use the following settings on production.
- Version:
PostgreSQL 13.7-R1
- Template:
Production
- Deployment option:
Multi-AZ DB Cluster
- DB instance identifier:
graph-node-cluster
- Master username:
graph_node
- Auto generate a password:
Yes
- DB instance class:
db.m6gd.2xlarge
(8 vCPU 32G RAM)
- Storage type:
io1
- Allocated storage:
500 GB
- Provisioned IOPS:
1000
- Enable storage autoscaling:
No
- Compute resource:
Don’t connect to an EC2 compute resource
- VPC:
eksctl-perp-production-cluster/VPC
- DB Subnet group:
default-vpc
- Public access:
Yes
- VPC security group:
graph-node
Unfortunately, AWS currently do not have Reserved Instances (RIs) Plan for Multi-AZ DB clusters. Use "Multi-AZ DB instance" or "Single DB instance" instead if the cost is a big concern to you.
RDS Remote Access
You could test your database remote access. Also, make sure the security group's inbound rules include 5432
port for PostgreSQL.
Create a Dedicated EKS Node Group
This step is optional.
Deploy graph-node in Kubernetes
Deployments for a Single DB Instance
Since Kubernetes Secrets are, by default, stored unencrypted in the API server's underlying data store (etcd). Anyone with API access can retrieve or modify a Secret. They're not secret at all. So instead of storing sensitive data in Secrets, you might want to use Secrets Store CSI Driver.
ref:
https://github.com/graphprotocol/graph-node/blob/master/docs/environment-variables.md
https://hub.docker.com/r/graphprotocol/graph-node
Deployments for a Multi-AZ DB Cluster
There are two types of nodes in a graph node cluster:
- Index Node: Only indexing data from the blockchain, not serving queries at all
- Query Node: Only serving GraphQL queries, not indexing data at all
Indexing subgraphs doesn't require too much CPU and memory resources, but serving queries does, especially when you enable GraphQL caching.
Index Node
Technically, we could further split an index node into Ingestor and Indexer: the former fetches blockchain data from RPC providers periodically, and the latter indexes entities based on mappings. That's another story though.
ref:
https://github.com/graphprotocol/graph-node/blob/master/docs/config.md
https://github.com/graphprotocol/graph-node/blob/master/docs/environment-variables.md
The key factors to the efficiency of syncing/indexing subgraphs are:
- The latency of the RPC provider
- The write thoughput of the database
I didn't find any graph-node
configs or environment variables that can speed up the syncing process observably. If you know, please tell me.
ref:
https://github.com/graphprotocol/graph-node/issues/3756
If you're interested in building a RPC proxy with healthcheck of block number and latency, see Deploy Ethereum RPC Provider Load Balancer with HAProxy in Kubernetes (AWS EKS). graph-node
itself cannot detect if the RPC provider's block delays.
Query Nodes
The most important config is DISABLE_BLOCK_INGESTOR: "true"
which basically configures the node as a query node.
ref:
https://github.com/graphprotocol/graph-node/blob/master/docs/config.md
https://github.com/graphprotocol/graph-node/blob/master/docs/environment-variables.md
It's also strongly recommended to mark subgraph schemas as immutable with @entity(immutable: true)
. Immutable entities are much faster to write and to query, so should be used whenever possible. The query time reduces by 80% in our case.
ref:
https://thegraph.com/docs/en/developing/creating-a-subgraph/#defining-entities
Setup an Ingress for graph-node
WebSocket connections are inherently sticky. If the client requests a connection upgrade to WebSockets, the target that returns an HTTP 101 status code to accept the connection upgrade is the target used in the WebSockets connection. After the WebSockets upgrade is complete, cookie-based stickiness is not used. You don't need to enable stickiness for ALB.
Deploy a Subgraph
ref:
https://github.com/graphprotocol/graph-cli
Here are also some useful commands for maintenance tasks:
ref:
https://github.com/graphprotocol/graph-node/blob/master/docs/graphman.md