Bull
Queue Manager
Why queue?
High traffic capacity
System Design
Google Cloud
CDN
Google Cloud
CDN
Compute Engine (AP)
Cloud Storage (Static Files)
CDN
Compute Engine
(AP)
Cloud Storage
(Static Files)
Load Balancing
Compute Engine
(AP)
Compute Engine
(AP)
Cloud SQL
(Database)
CDN
Compute Engine
(AP)
Cloud Storage
(Static Files)
Load Balancing
Compute Engine
(AP)
Compute Engine
(AP)
Cloud SQL Cluster
(Read)
Cloud SQL Cluster
(Write)
Cloud SQL Cluster
(Read)
All stateless nodes are scalable
State Storage is only short part in system
Change session based token to JWT
AP nodes stateless
Schema design
Reduce index in frequently insert table
Replace auto increment pk with uuid, snowflake ...
Pre-insert data, update only use row lock
Only ticket-like record select with FOR UPDATE
DON'T use count() / limit in frequently update table
Data slicing
Slice seats to difference table/database:
- Reduce table flush times into disk
- Reduce each database query analyze CPU usage
- Mark sold when select from AP, and commit it after checkout
Cache the seat available in ap level
- (Crazy) inter-AP communication to trade marked tickets
BUT
If zone A tickets sold out, all customers will change to find a chance to buy zone B tickets.
We cannot always predict the distribution of zone requirements.
Recognize system limit
Someone checkout successful better than everyone block out
Rate Limiter
Numbered queue
const Queue = require('bull');
const express = require('express');
const app = express();
const rateLimiter = new Queue('rateLimited', {
limiter: {
max: 5000,
duration: 1000, // 1 sec
},
});
rateLimiter.process(({ data }) => {
return `OK! ${Date.now()}`;
});
function fetchResponse() {
return new Promise(async (resolve) => {
const job = await rateLimiter.add({ foo: 'bar' });
const response = await job.finished();
resolve(response);
});
}
app.get('/', async (req, res) => {
res.send(await fetchResponse());
});
app.listen(7887, () => {
console.log('Server listen on 7887');
});
Live demo
Thanks
Bull - Rate Limiter
By Chia Yu Pai
Bull - Rate Limiter
- 369