
There are many ways backends can talk to each other.
All have their own pros and cons. We’ll be using queues.
Use docker or a managed service - https://upstash.com/pricing
import { createClient } from "redis";
import express from "express";
app.post("/order", async (req, res) => {
// matching
const userId = req.userId;
const {type, price, qty, market_id, side} = req.body;
await client.lPush("incoming-order", JSON.stringify({
type, price, qty, market_id, side, userId
}))
})
Create a new bun project called engine or orderbook . Move all in memory variables there
import { createClient } from "redis";
const BALANCES = {
}
const ORDERBOOKS = {
SOL: {},
BTC: {}
}
const client = await createClient()
.on("error", (err) => console.log("Redis Client Error", err))
.connect();
while(1) {
const response = await client.brPop("incoming-order", 1);
console.log(response);
}
import { createClient } from "redis";
const BALANCES = {
}
const ORDERBOOKS = {
SOL: {},
BTC: {}
}
const client = await createClient()
.on("error", (err) => console.log("Redis Client Error", err))
.connect();
while(1) {
const response = await client.rPop("incoming-order", 1);
console.log(response);
}
<aside> 💡
Question — How does the engine talk back to the HTTP Server?
</aside>