Shopifyのレート制限?リーキーバケットアルゴリズムやGraphQLについてもイラストで解説

SHOPIFY rate limit? Explanation of Leaky Bucket Algorithm and GRAPHQL with illustrations

Jul 30, 2024

こんにちは!今回はShopifyのレート制限やGraphQLについて説明する記事となっています。「レート制限ってなに?」「GraphQLなにそれ?」という疑問が少しだけ解決するように難しい表現は使用せずご説明します。ただ、少し聞き慣れない単語が出てくるかもしれません。特に API について少しだけでも理解できていることが前提となりますので「APIという単語は全く聞いたことがない」という場合はYouTube動画などを見て少しだけ勉強してみてくださいね。


REST API and GraphQL

SHOPIFY has two types of API (Admin API) for acquiring, creating, updating, and deleting customer information and order information, Rest API and GRAPHQL.

I will explain the difference between REST API and GraphQL in SHOPIFY. It is not a specialized content, but it is only explained to grasp the shape, so please check it out for details.

This time, I will explain Shopify's customer information as an example.


Rest API

For example, if you want to know the name and address of a certain customer, you can use the SHOPIFY REST API.

The side to call the API "Please give me information on the customer number XXXX"

SHOPIFY "Customer number XXXX number information" (all in name, address, number of purchases, purchases, etc.)

REST APIの場合

As described above, Shopify returns data with "put all" even if you only want a specific information. Based on the returned data, the processing out only the necessary information is required for the call of the API.

https://shopify.dev/docs/api/admin-rest/2024-07/resources/customer


Graphql

If you want to know the name and address of a certain customer, like the REST API, this is the case with GraphQL.

The side that calls the API "Please give me information on the customer number XXXX number Email, name, and address"

SHOPIFY "Customer number XXXX number EMAIL / Name / Address is here"

GraphQLの場合

In this way, it is possible to acquire only the necessary information. Of course, you can get not only name and address but also information such as the most recently purchased products.

https://shopify.dev/docs/api/admin-graphql/2024-07/queries/customer


Very very much, I explained the difference between REST API and GraphQL in SHOPIFY. Please keep in mind that this difference will be involved in the rate restrictions we will talk about in the future, so it is okay to just blur.


SHOPIFY rate limit

SHOPIFY has a rate limit, and its limits are slightly different between REST API and GraphQL.


What is rate restriction ...?

In the first place, the rate limit is that the API provider (in this case, SHOPIFY) is a mechanism that restricts the number of API requests that can be made within a specific time frame.


As shown in the figure below, if you are unlimitedly accepting requests such as "Give me that information", "Give this information", "Please add this information", high load on the API server. The system may not work properly.

APIを呼び過ぎた場合

In order to avoid the crash of unintended server, there are various rate restrictions in services that provide data as well as SHOPIFY.


SHOPIFY rate limit

Let's take a look at restrictions on Rest API and GraphQL API.
Shopify API Rate Limites

REST API rate limit

The REST API is a request -based limit, and the standard limit is up to 2 requests per second.

What does this "1 request" mean?

  • Take GET information

  • Add post information

  • Delete Delete information

  • Edit PUT information

Each one is one request, and if you want to get 10 information, 10 requests, 1 request if you delete one information.

It's very simple.


GraphQL rate limit

REST APIの制限は大変シンプルでした。ではGraphQLの制限はどうでしょうか?

上の図にはメソッドの欄に「Calculated Query Score」、リミットは「100points/second」と記載があります。これはどういうことでしょうか…?


GraphQLでは、「1つの情報を取る=1リクエスト」という計算にはなりません。

APIをコールする際、 オブジェクトの取得は「1ポイント」データの更新、作成、削除を要求するミューテーションは「各10ポイント」 となっています。

https://shopify.dev/docs/api/usage/rate-limits#calculated-query-costs


20件の情報を 取得するだけ であれば、合計20ポイント。「100points/second」という制限内で問題なく処理が可能です。

では、 20件の情報を更新 する場合は?ミューテーションは1件10ポイントのため20件では200ポイントという計算になります。


REST APIがあまりにもシンプル過ぎるため少し複雑に感じるかもしれませんが、GraphQLのポイント計算も思ったより簡単かなと思います。


上記の数字を見ただけでも、REST APIよりもGraphQLの方が効率的であると感じますよね。Shopify公式でもGraphQLの使用を推奨しています。

触ったことがない方は少し学習に時間がかかるかもしれませんがぜひ挑戦してみましょう。


Hole bucket

"Hole bucket" is a splendid IT term. You may have heard of the word "Leaky Bucket Algorithm" in the name of the English name ...?


Leaky bucket algorithm is one of the algorithm when processing rate restrictions.
The request is processed like a bucket with a hole.


REST API bucket

The size of the bucket that can be processed at once, the size of the bucket is 40 requests (shop units / application units), which is leaked by 2 requests per second.

If you explain it roughly, it looks like this. Not only the type of request, but also "1 request", so it is easy to return the Too Many Requests error as the number increases.

REST APIの穴あきバケツ

GraphQL bucket

GraphQLのバケツサイズは1000 cost points(ショップ単位・アプリ単位)で1秒に50 pointsリークしていきます。

表現は少し複雑に感じますが、REST APIと考え方は基本的に同じです。

GraphQLの穴あきバケツ

REST APIと同じ数のリクエストを送信しましたが、GraphQLの場合はまだ十分にバケツの中身が残っているためさらに多くのデータを処理することが可能です。

上記の例ではMutationのリクエストを送信したと仮定して説明していますが、これがデータ取得だけのQueryであればリクエストのコストポイントは10分の1の「1 point」のため、さらに余裕のある状況になります。


REST APIに比べ、コストはぐっと低いですがそれでもLine Itemと言われる商品を一つ一つ大量に処理しようとするとどうしてもレート制限がかかりやすくなってしまいます。

解決方法として、ShopifyのGraphQLにはBulk Operationという非同期での一括処理の方法も用意されています。

https://shopify.dev/docs/api/usage/bulk-operations/queries


カスタムアプリを作ってみたけれどもすぐに制限がかかってしまう、処理を高速化したいなどのお悩みなどありましたらぜひ一度GO RIDEへお問い合わせください。


Learn GraphQL and efficiently use Shopify data

The REST API is relatively simple, and I think anyone who has learned a web application may have touched it once. If the required amount of data and the number of APIs are small, the REST API can operate the application sufficiently.

On the other hand, GraphQL is a slightly special method, but if the data type and amount of data, such as SHOPIFY, are large, can efficiently handle data.

In addition, GraphQL is also recommended by SHOPIFY formula. At first, the hurdles are a bit high and the learning costs cost, but if you are interested, please study.


Various commentary videos are released on Shopifydevs YouTube channel of Shopify. I am very indebted to me, and I am also referring to this article.

The hurdles are a little high because it is full -length English, but if you are worried, please take a look.

https://www.youtube.com/@shopifydevs


Utilize SHOPIFY even more

今回の記事は少しだけ専門的な内容でした。正直にお伝えすると、Shopifyでストアを開いて運用するだけであればあまり必要のない知識かもしれません。

しかし、Shopifyの可能性は無限大!自分の好きなようにストアをどんどん拡張することが可能です。

Shopify App Storeですでに用意されているアプリケーションを導入することはもちろんですが、自分ならでは、自社ならではの需要にあったものはAPIを使ってカスタムアプリケーションを作ってみるのもいいかも…?


Shopifyのストア構築でお悩みの方はぜひGO RIDEへお問い合わせください。皆さまのストアに合ったアプリケーションのご提案や構築などをお手伝いさせていただきます!

Kanayo

Kanayo UEHARA

Go Ride Engineer

Fukuoka-> yokohama-> Vancouver, Canada