Zeeshan Adil
Book a call
← All concepts

CONCEPT

What are virtual nodes?

Virtual nodes (vnodes) give each physical server many positions on a hash ring instead of one. Its share becomes the sum of many small arcs, so load evens out, and when a server leaves, its data spreads over several neighbors instead of landing on one.

Also called: vnodes, tokens. By Zeeshan Adil··Databases·Distributed Systems·Performance

A hash ring where each of databases A, B and C appears three times; after database B is removed, its three arcs fall to the next tokens clockwise, which belong to A and C.

The problem it solves

On a consistent hashing ring with one position per server, ownership depends on where the hashes happen to land. Three servers can easily end up with 50%, 30% and 20% of the keys, and a real run can be far worse. Also, when one server leaves, all its keys fall onto the single next server, which can double that machine's load.

How it works

Give each server many tokens, for example by hashing "A#0", "A#1", "A#2" and so on. These are logical positions, not extra machines: three database servers with three tokens each are nine tokens on the ring.

DatabaseIts tokens
A10, 50, 80
B20, 60, 90
C30, 40, 70

A token owns the arc from just after the previous token up to itself. Because each server owns many small, scattered arcs, bad luck in one place is canceled by good luck in another.

Remove DB B and each of its arcs goes to the next token clockwise: B60 falls to C70, B20 falls to C30, B90 wraps to A10. B's data went to two databases, so both neighbors share the work.

Two more uses follow. A bigger machine can take more tokens than a smaller one. And a join becomes many small copies from many sources rather than one large copy from one.

What it does not do

  • Vnodes are not replicas. All of B's tokens sit on the same machine and fail together. Replicas must be on different physical servers, so a replica list takes the next different servers clockwise and counts each machine once.
  • They are not free. More tokens mean a bigger ring to distribute and more small range copies during a join.
  • They do not fix a hot key. Balanced keys is not balanced traffic. One hot key still goes to one server.

Measured

In my sharding challenge, the ring was built over 4 databases with different token counts, and the share of the ring each database owned was measured.

Tokens eachBusiest databaseQuietest database
198.6%0.20%
1629.8%19.1%
6429.4%19.2%
25626.5%23.0%

The fair share is 25%. Sixteen tokens each was already decent. The biggest single handoff to a fifth database also fell, from 61.5% of the keys with one token to 0.32% with 256.

In the same challenge, a ring with 64 tokens each moved 28.3% of 100,000 posts when a fourth database joined (the fair share is 25%).

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesWhere does each comment live?
What is consistent hashing?What is a hot key?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.