SlideShare a Scribd company logo
Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
2022/08/24

toranoana.deno #8 



虎の穴ラボ

奥谷 一陽

Deno Deployと組み合わせるのに

upstashをおすすめしたい

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
自己紹介

奥谷 一陽

所属:虎の穴ラボ株式会社

担当:とらコインSHOPなど新規事業系の開発

興味:TypeScript、Deno

おすすめコンテンツ:

  『プラネテス』

  『暴太郎戦隊ドンブラザーズ』



Twitter:@okutann88

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deploy どのくらい使っ
てますか?

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deploy と組み合わせる前提で、

オススメSaaSを紹介したい

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
upstash

















参考:https://upstash.com/

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
upstash

- カルフォルニアの Upstash, Inc. が運営するSaaS

- Redis Kafka qStashを提供

- キーワード:グローバル 低レイテンシ 上限付きの従量課金



Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
upstash の Redis

- 無料プラン:10K コマンド/日

- 有料プラン:0.2 $/100Kコマンド、月間最大 $120 まで

- 公式が、deno.land/x にモジュールを公開済み

https://deno.land/x/upstash_redis@v1.12.0-next.1

- Redisと銘打つものの、内部でfetchが使用されたhttpリクエストを使用

「Redisっぽく動く」 「Redis互換の」ぐらいの感触

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
upstash の Redis の使用例

import { Application, Router, Context } from "https://deno.land/x/oak/mod.ts";
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
const redis = new Redis({
url: Deno.env.get("UPSTASH_URL"),
token: Deno.env.get("UPSTASH_TOKEN"),
});
const router = new Router();
router.get("/", async (context: Context) => {
const count = await redis.get("count");
context.response.body = count;
});
router.post("/update", async (context: Context) => {
await redis.incr("count");
context.response.body = "updated";
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8080 });


Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
upstash の Redis で実際にやりたいのは?

- キャッシュの仕組みが有ったときに、Webサービスで扱いたいのは

「セッション」では無いだろうか(偏見かもしれません)

- oak_session モジュール + upstash_redis モジュール は組み合わせ可能?

=> できる ただし...、 オプションが必須

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
upstash の Redis を oak_session で使う

import { Application, Context, Router } from
"https://deno.land/x/oak/mod.ts";
import { RedisStore, Session } from
"https://deno.land/x/oak_sessions/mod.ts";
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
const redis = new Redis({
url: Deno.env.get("UPSTASH_URL")!,
token: Deno.env.get("UPSTASH_TOKEN")!,
automaticDeserialization: false, // <== ポイント
});
const router = new Router();
router.get("/", async (context: Context) => {
const name = await context.state.session.get("name");
context.response.body = `
<!DOCTYPE html>
<html>
<body>
<div>
${!name ? "" : "name=" + name}
</div>
<form method="POST">
<input name="name">
<button type="submit">submit</button>
</form>
</body>
</html>
`;
});
router.post("/", async (context: Context) => {
const form = await context.request.body({ type: "form" }).value;
const name = form.get("name");
if (!!name) context.state.session.set("name", name);
context.response.redirect("/");
});
type AppState = {
session: Session;
};
const app = new Application<AppState>();
const store = new RedisStore(redis);
app.use(Session.initMiddleware(store));
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8080 });


upstash_redisは、デフォルトで、jsonの自動デシリアライズが
ONになっているので この機能を停止させる


デシリアライズは、oak_session に任せる!




Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
qStash ?

- upstashの独自サービス

- サーバーレス、エッジランタイム向け、HTTPベースメッセージング/

スケジューリングソリューション

- 無料プラン:50リクエスト/日

- 有料プラン:50Kリクエスト/日 0.4 $/1Kリクエスト 月間最大 $450 

- 最大のポイント Deno Deploy をスケジュール実行できる

=> さらに、upstash公式が deno deploy をサポート

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
qStash の deno deploy サポート

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deployで qStash 使ってみる 

以下の内容を実装する

- qStashから1分に1回 Deno Deploy に置いたアプリを呼び出す

- 呼び出しに対応して upstash Redisに保管した値をカウントする

- ブラウザからアクセスし値を参照する

Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deployで qStash 使ってみる 

ポイント

- 公式が、deno.land/x にモジュールを公開済み

- https://deno.land/x/upstash_qstash@v0.1.7

- qStashからのリクエストであることを upstash_qstash モジュールを

使い検証すること



Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deploy で qStash を使う実装例

import { Application, Router, Context } from
"https://deno.land/x/oak/mod.ts";
import { Redis } from
"https://deno.land/x/upstash_redis/mod.ts";
import { Receiver } from
"https://deno.land/x/upstash_qstash/mod.ts";
const redis = new Redis({
url: Deno.env.get("UPSTASH_URL"),
token: Deno.env.get("UPSTASH_TOKEN"),
})
const receiver = new Receiver({
currentSigningKey:
Deno.env.get("QSTASH_CURRENT_SIGNING_KEY")!,
nextSigningKey:
Deno.env.get("QSTASH_NEXT_SIGNING_KEY")!,
});
const router = new Router();
router.get("/", async(context:Context) => {
const count = await redis.get("count");
context.response.body = count;
});


router.post("/update", async(context:Context) => {
const isValid = await receiver.verify({ // <== ポイント
signature: context.request.headers.get("Upstash-Signature")!,
body: await context.request.body({ type: "text" }).value,
}).catch((err: Error) => {
console.error(err);
return false;
});
if (!isValid) {
return new Response("Invalid signature", { status: 401 });
}
await redis.incr("count");
return new Response("OK", { status: 200 });
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8080 });


Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
現在動作中です。

一分に一回カウントアップされます。
https://late-otter-59.deno.dev/





Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
まとめ

- Deno Deployの有効活用のため、upstashの組み合わせオススメ

- 出来ることが、大きく広がる

- Deno Deploy 起点のフルサーバーレスなサービスを開発できる可能性

例えば?



Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deploy 起点フルサーバーレス サービス インフラ 構想



deno deploy
・APIサーバー
・DB管理
・ストレージ
キャッシュ呼び出し
Redis
qStash
バッチ処理呼び出し
フロントサーバー
Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
Deno Deploy 起点フルサーバーレス サービス インフラ 構想

deno deploy
フロントサーバー
・APIサーバー
・DB管理
・ストレージ
キャッシュ呼び出し
Redis qStash
バッチ処理呼び出し
deno deploy
+      +
bunny cdn ...
API呼び出し
Copyright  (C) 2021 Toranoana Inc. All Rights Reserved.
ありがとうございました


More Related Content

PDF
DatadogでAWS監視やってみた
PDF
Beyond the Twelve-Factor App
PPTX
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
PPTX
20220409 AWS BLEA 開発にあたって検討したこと
PPTX
【改訂版あり】クラウド・ネイティブ時代に最適なJavaベースのマイクロサービス・フレームワーク ~ Helidonの実力を見極めろ!
PDF
DevOps with Database on AWS
PDF
ソーシャルゲームのためのデータベース設計
PDF
そんなトランザクションマネージャで大丈夫か?
DatadogでAWS監視やってみた
Beyond the Twelve-Factor App
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
20220409 AWS BLEA 開発にあたって検討したこと
【改訂版あり】クラウド・ネイティブ時代に最適なJavaベースのマイクロサービス・フレームワーク ~ Helidonの実力を見極めろ!
DevOps with Database on AWS
ソーシャルゲームのためのデータベース設計
そんなトランザクションマネージャで大丈夫か?

What's hot (20)

PDF
あなたのクラウドは大丈夫?NRI実務者が教えるセキュリティの傾向と対策 (Oracle Cloudウェビナーシリーズ: 2021年11月24日)
PDF
製造装置データ収集の選択肢 (AWS IoT Deep Dive #5)
PDF
Open Match Deep Dive
PDF
ネットワークOS野郎 ~ インフラ野郎Night 20160414
PPTX
基礎から学ぶ? EC2マルチキャスト
PDF
インメモリーデータグリッドの選択肢
PDF
Akkaとは。アクターモデル とは。
PDF
SharePoint Framework Extension 基礎講座
PDF
Snowflake Architecture and Performance(db tech showcase Tokyo 2018)
PPTX
はじめての datadog
PPTX
監査要件を有するシステムに対する PostgreSQL 導入の課題と可能性
PDF
俺の俺による俺のための App Service Environment
PDF
C++でCプリプロセッサを作ったり速くしたりしたお話
PDF
オンプレミスからクラウドへ:Oracle Databaseの移行ベストプラクティスを解説 (Oracle Cloudウェビナーシリーズ: 2021年2月18日)
PDF
Vacuum徹底解説
PDF
SaaS テナント毎のコストを把握するための「AWS Application Cost Profiler」のご紹介
PPTX
ActionCableのクライアントはRails外から利用できるのか
PDF
20190320 AWS Black Belt Online Seminar Amazon EBS
PDF
初心者向けWebinar AWSで開発環境を構築しよう
PDF
マルチテナント化で知っておきたいデータベースのこと
あなたのクラウドは大丈夫?NRI実務者が教えるセキュリティの傾向と対策 (Oracle Cloudウェビナーシリーズ: 2021年11月24日)
製造装置データ収集の選択肢 (AWS IoT Deep Dive #5)
Open Match Deep Dive
ネットワークOS野郎 ~ インフラ野郎Night 20160414
基礎から学ぶ? EC2マルチキャスト
インメモリーデータグリッドの選択肢
Akkaとは。アクターモデル とは。
SharePoint Framework Extension 基礎講座
Snowflake Architecture and Performance(db tech showcase Tokyo 2018)
はじめての datadog
監査要件を有するシステムに対する PostgreSQL 導入の課題と可能性
俺の俺による俺のための App Service Environment
C++でCプリプロセッサを作ったり速くしたりしたお話
オンプレミスからクラウドへ:Oracle Databaseの移行ベストプラクティスを解説 (Oracle Cloudウェビナーシリーズ: 2021年2月18日)
Vacuum徹底解説
SaaS テナント毎のコストを把握するための「AWS Application Cost Profiler」のご紹介
ActionCableのクライアントはRails外から利用できるのか
20190320 AWS Black Belt Online Seminar Amazon EBS
初心者向けWebinar AWSで開発環境を構築しよう
マルチテナント化で知っておきたいデータベースのこと
Ad

More from 虎の穴 開発室 (20)

PDF
FizzBuzzで学ぶJavaの進化
PDF
Railsのデバッグ どうやるかを改めて確認する
PDF
虎の穴ラボ エンジニア採用説明資料 .pdf
PDF
toranoana.deno #6 アジェンダ 採用説明
PDF
Deno 向け WEB 開発用のツールを作ったので 紹介します
PDF
Supabase Edge Functions と Netlify Edge Functions を使ってみる – 機能とその比較 –
PDF
GCPの画像認識APIの紹介
PDF
【エンジニアの勉強法ハックLT- vol.7】ゲームから学んだ勉強のこと
PDF
GitHub APIとfreshで遊ぼう
PDF
通販開発部の西田さん「通販開発マネジメントの5ルール」
PDF
社内DX推進!非エンジニア向けにプログラミング講座を実施してみた!
PDF
セキュリティを強化しよう!CloudArmorの機能解説
PDF
JavaScript LT会 〜 React.js Node.js歓迎 〜 Deno で やってみるweb開発
PDF
Amplify Studioを使ってみた
PDF
いいテスト会 (スプリントレビュー) をやろう!
PDF
【Saitama.js】Denoのすすめ
PDF
虎の穴ラボ Tech day#3 チームで戦う!とらのあな通販冬の大感謝祭でのフロント開発について
PDF
【とらのあなラボ Tech Day #3】新規システムにおける技術選定〜GoとgRPCを採用した話〜
PDF
虎の穴ラボ TechDay#3 フルリモート率100%!リモートワークを可能にするマネージメント
PDF
【20220120 toranoana.deno#4】deno を使って「ログイン」するサービスを作る
FizzBuzzで学ぶJavaの進化
Railsのデバッグ どうやるかを改めて確認する
虎の穴ラボ エンジニア採用説明資料 .pdf
toranoana.deno #6 アジェンダ 採用説明
Deno 向け WEB 開発用のツールを作ったので 紹介します
Supabase Edge Functions と Netlify Edge Functions を使ってみる – 機能とその比較 –
GCPの画像認識APIの紹介
【エンジニアの勉強法ハックLT- vol.7】ゲームから学んだ勉強のこと
GitHub APIとfreshで遊ぼう
通販開発部の西田さん「通販開発マネジメントの5ルール」
社内DX推進!非エンジニア向けにプログラミング講座を実施してみた!
セキュリティを強化しよう!CloudArmorの機能解説
JavaScript LT会 〜 React.js Node.js歓迎 〜 Deno で やってみるweb開発
Amplify Studioを使ってみた
いいテスト会 (スプリントレビュー) をやろう!
【Saitama.js】Denoのすすめ
虎の穴ラボ Tech day#3 チームで戦う!とらのあな通販冬の大感謝祭でのフロント開発について
【とらのあなラボ Tech Day #3】新規システムにおける技術選定〜GoとgRPCを採用した話〜
虎の穴ラボ TechDay#3 フルリモート率100%!リモートワークを可能にするマネージメント
【20220120 toranoana.deno#4】deno を使って「ログイン」するサービスを作る
Ad

Deno Deployと組み合わせるのに Upstashをおすすめしたい.pdf

  • 1. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. 2022/08/24
 toranoana.deno #8 
 
 虎の穴ラボ
 奥谷 一陽
 Deno Deployと組み合わせるのに
 upstashをおすすめしたい

  • 2. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. 自己紹介
 奥谷 一陽
 所属:虎の穴ラボ株式会社
 担当:とらコインSHOPなど新規事業系の開発
 興味:TypeScript、Deno
 おすすめコンテンツ:
   『プラネテス』
   『暴太郎戦隊ドンブラザーズ』
 
 Twitter:@okutann88

  • 3. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy どのくらい使っ てますか?

  • 4. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy と組み合わせる前提で、
 オススメSaaSを紹介したい

  • 5. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. upstash
 
 
 
 
 
 
 
 
 参考:https://upstash.com/

  • 6. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. upstash
 - カルフォルニアの Upstash, Inc. が運営するSaaS
 - Redis Kafka qStashを提供
 - キーワード:グローバル 低レイテンシ 上限付きの従量課金
 

  • 7. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. upstash の Redis
 - 無料プラン:10K コマンド/日
 - 有料プラン:0.2 $/100Kコマンド、月間最大 $120 まで
 - 公式が、deno.land/x にモジュールを公開済み
 https://deno.land/x/upstash_redis@v1.12.0-next.1
 - Redisと銘打つものの、内部でfetchが使用されたhttpリクエストを使用
 「Redisっぽく動く」 「Redis互換の」ぐらいの感触

  • 8. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. upstash の Redis の使用例
 import { Application, Router, Context } from "https://deno.land/x/oak/mod.ts"; import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"; const redis = new Redis({ url: Deno.env.get("UPSTASH_URL"), token: Deno.env.get("UPSTASH_TOKEN"), }); const router = new Router(); router.get("/", async (context: Context) => { const count = await redis.get("count"); context.response.body = count; }); router.post("/update", async (context: Context) => { await redis.incr("count"); context.response.body = "updated"; }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8080 }); 

  • 9. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. upstash の Redis で実際にやりたいのは?
 - キャッシュの仕組みが有ったときに、Webサービスで扱いたいのは
 「セッション」では無いだろうか(偏見かもしれません)
 - oak_session モジュール + upstash_redis モジュール は組み合わせ可能?
 => できる ただし...、 オプションが必須

  • 10. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. upstash の Redis を oak_session で使う
 import { Application, Context, Router } from "https://deno.land/x/oak/mod.ts"; import { RedisStore, Session } from "https://deno.land/x/oak_sessions/mod.ts"; import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"; const redis = new Redis({ url: Deno.env.get("UPSTASH_URL")!, token: Deno.env.get("UPSTASH_TOKEN")!, automaticDeserialization: false, // <== ポイント }); const router = new Router(); router.get("/", async (context: Context) => { const name = await context.state.session.get("name"); context.response.body = ` <!DOCTYPE html> <html> <body> <div> ${!name ? "" : "name=" + name} </div> <form method="POST"> <input name="name"> <button type="submit">submit</button> </form> </body> </html> `; }); router.post("/", async (context: Context) => { const form = await context.request.body({ type: "form" }).value; const name = form.get("name"); if (!!name) context.state.session.set("name", name); context.response.redirect("/"); }); type AppState = { session: Session; }; const app = new Application<AppState>(); const store = new RedisStore(redis); app.use(Session.initMiddleware(store)); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8080 }); 
 upstash_redisは、デフォルトで、jsonの自動デシリアライズが ONになっているので この機能を停止させる 
 デシリアライズは、oak_session に任せる! 
 

  • 11. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. qStash ?
 - upstashの独自サービス
 - サーバーレス、エッジランタイム向け、HTTPベースメッセージング/
 スケジューリングソリューション
 - 無料プラン:50リクエスト/日
 - 有料プラン:50Kリクエスト/日 0.4 $/1Kリクエスト 月間最大 $450 
 - 最大のポイント Deno Deploy をスケジュール実行できる
 => さらに、upstash公式が deno deploy をサポート

  • 12. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. qStash の deno deploy サポート

  • 13. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deployで qStash 使ってみる 
 以下の内容を実装する
 - qStashから1分に1回 Deno Deploy に置いたアプリを呼び出す
 - 呼び出しに対応して upstash Redisに保管した値をカウントする
 - ブラウザからアクセスし値を参照する

  • 14. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deployで qStash 使ってみる 
 ポイント
 - 公式が、deno.land/x にモジュールを公開済み
 - https://deno.land/x/upstash_qstash@v0.1.7
 - qStashからのリクエストであることを upstash_qstash モジュールを
 使い検証すること
 

  • 15. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy で qStash を使う実装例
 import { Application, Router, Context } from "https://deno.land/x/oak/mod.ts"; import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"; import { Receiver } from "https://deno.land/x/upstash_qstash/mod.ts"; const redis = new Redis({ url: Deno.env.get("UPSTASH_URL"), token: Deno.env.get("UPSTASH_TOKEN"), }) const receiver = new Receiver({ currentSigningKey: Deno.env.get("QSTASH_CURRENT_SIGNING_KEY")!, nextSigningKey: Deno.env.get("QSTASH_NEXT_SIGNING_KEY")!, }); const router = new Router(); router.get("/", async(context:Context) => { const count = await redis.get("count"); context.response.body = count; }); 
 router.post("/update", async(context:Context) => { const isValid = await receiver.verify({ // <== ポイント signature: context.request.headers.get("Upstash-Signature")!, body: await context.request.body({ type: "text" }).value, }).catch((err: Error) => { console.error(err); return false; }); if (!isValid) { return new Response("Invalid signature", { status: 401 }); } await redis.incr("count"); return new Response("OK", { status: 200 }); }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8080 }); 

  • 16. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. 現在動作中です。
 一分に一回カウントアップされます。 https://late-otter-59.deno.dev/
 
 

  • 17. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. まとめ
 - Deno Deployの有効活用のため、upstashの組み合わせオススメ
 - 出来ることが、大きく広がる
 - Deno Deploy 起点のフルサーバーレスなサービスを開発できる可能性
 例えば?
 

  • 18. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy 起点フルサーバーレス サービス インフラ 構想
 
 deno deploy ・APIサーバー ・DB管理 ・ストレージ キャッシュ呼び出し Redis qStash バッチ処理呼び出し フロントサーバー
  • 19. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. Deno Deploy 起点フルサーバーレス サービス インフラ 構想
 deno deploy フロントサーバー ・APIサーバー ・DB管理 ・ストレージ キャッシュ呼び出し Redis qStash バッチ処理呼び出し deno deploy +      + bunny cdn ... API呼び出し
  • 20. Copyright  (C) 2021 Toranoana Inc. All Rights Reserved. ありがとうございました