DEV
skip to content
[email protected]

Search is only available in production builds.
Try building and previewing the site to test it out locally.

Memcache

/ 2 min read

Table of Contents

Prompt

How do memcaches work? How about distributed caches?

The problem: DBs are slow

start with something familiar

Let’s say we have a website with a database of all Pokemon cards, where users can inspect any existing card for consideration in their collection., Typically, the cards would typically be stored in a database as hard disk space.

Every time a user looks up a card, they

  • make a request to the webserver,
  • the server makes a DB request
  • db reads the entry from disk
  • server replies with page containing card

Suppose this site was made for competitive play, where only the most recent cards are relevant. The most recent and most popular cards would be requested first. Thisi s where a memcache comes in handy. We can knowingly store the most frequent entries in memory, so we can avoid the disk read altogether.

This layer is typicaly implemented as a KV application, like Redis. It’s basically a hash map serving the role of a database in memory.

Retrieving data from the database can be costly because reading from the db usually means a disk read, a very slow operation.

For frequently requested data, each connection forces the server to do a database read.

The solution is simple: Instead, we can

In the context of webdev, a memcache is a layer of memory that stores frequently used database entries.

What is a memcache?

A memcache is just a database that’s stored in memory instead of disk. It’s used to cache frequently accessed data, so we can skip a lengthy disk read. The trade-off is that volatile memory stores magnitudes less data than a database’s disk can. Using frequent data for this limited space replaces the most disk reads with memory reads.

They’re typically implemented as applications, with a key-value store API. Applications like Redis run as a

Using a memcache with Redis NodeJS API

import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
await client.set('key', 'value');
const value = await client.get('key');
await client.hSet('user-session:123', {
name: 'John',
surname: 'Smith',
company: 'Redis',
age: 29
})
let userSession = await client.hGetAll('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/*
{
"surname": "Smith",
"name": "John",
"company": "Redis",
"age": "29"
}
*/

It’s just a hashmap running over HTTP.

Tradeoffs: Database vs Memcache

Tradeoffs are the typical disk-memory tradeoffs

  • signiticantly less space
  • volatile data

Use cases

You’d want to use this for frequently accessed data, or very low-latency data. Like I said eaerlier,