← Course Index

Redis & RabbitMQ Cheat Sheet

Redis CLI Cheatsheet

Command / Action Redis CLI Syntax Use Case
Set Key with TTL SETEX user:123 300 '{"name": "Alice"}' Cache user profiles for 5 mins (300s)
Get Key GET user:123 Fetch cached string / serialized JSON object
Hash (Create/Get) HSET sess:abc id 100
HGETALL sess:abc
Express sessions, stores key-value objects on one key
Atomic Increment INCR limit:192.168.1.1 API Rate limiting counters
Set Key Expiry EXPIRE limit:192.168.1.1 60 Set expiration on counter (e.g. 60s rate limit window)
Check TTL remaining TTL user:123 Find expiration time in seconds (-1 = no TTL, -2 = gone)
Delete Key DEL user:123 Cache invalidation (run after writing to primary database)

RabbitMQ amqplib Code Snippets

1. Safe Connection Setup

import amqp from 'amqplib';

const connection = await amqp.connect(process.env.RABBITMQ_URL);
const channel = await connection.createChannel();

// Define DLX (Dead Letter Exchange)
await channel.assertExchange('dlx', 'direct', { durable: true });
await channel.assertQueue('jobs.failed', { durable: true });
await channel.bindQueue('jobs.failed', 'dlx', 'jobs.routing-key');

2. Declaring a Reliable Work Queue

// Declare main queue with DLX bindings
await channel.assertQueue('emails', {
  durable: true, // Survives RabbitMQ broker restarts
  arguments: {
    'x-dead-letter-exchange': 'dlx',
    'x-dead-letter-routing-key': 'jobs.routing-key'
  }
});

3. Worker Consumer Implementation

// Limit unacknowledged messages to avoid server resource exhaustion
channel.prefetch(1);

channel.consume('emails', async (msg) => {
  if (!msg) return;
  try {
    const jobData = JSON.parse(msg.content.toString());
    await processEmail(jobData);
    
    // Acknowledge successful execution (removes from queue)
    channel.ack(msg);
  } catch (err) {
    console.error('Job failed:', err);
    // NACK with requeue=false (moves message to the DLX queue)
    channel.nack(msg, false, false);
  }
});
AWS Services Map Next: GitHub Actions Cheat Sheet →