Lighthouse Scanner: Stateless NodeJS Microservices with Redis DB

Which Database to Choose?

Redis Basics

  • Strings
  • Hashmaps
  • Lists, Sets, Ordered Sets
client@redis> lpush jobs:list job1 job2 job3
(integer) 3
client@redis> lrange jobs:list 0 -1
1) "job3"
2) "job2"
3) "job1"
client@redis> lpop jobs:list
"job3"
client@redis> lrange jobs:list 0 -1
1) "job2"
2) "job1"
client@redis>

Building a Stateless Microservice

  • All data must be persisted immediately
  • Persisting and Reading must be fast and efficient
  • Working data must be deleted easily
  • Create and update a job
  • Create a report

Jobs

  • A job object consists of uuid, domain and status.
  • The uuid is the identifier
  • The status changes from created to finish or error
  • The domain is used to retrieve the scan report once it is finished
hset "0b25ab16-6efd-485c-b260-1766505a3811" domain "http://example.com" status "started"
hgetall "0b25ab16-6efd-485c-b260-1766505a3811"
1) "domain"
2) "http://example.com"
3) "status"
4) "started"
hset "0b25ab16-6efd-485c-b260-1766505a3811" status "finished"
expire "0b25ab16-6efd-485c-b260-1766505a3811" 86400

Reports

set "example.com" "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"> ..."
get "example.com"

NodeJS Wrapper

redis.hset("0b25ab16-6efd-485c-b260-1766505a3811", "domain", "http://example.com", "status", "started")
function updateJob(uuid, details) {
redis.hset(uuid, 'domain', details.domain, 'status', details.status);
redis.expire(uuid, 86400);
}
redis.hgetall(uuid);

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store