Why Memgraph Makes Dagma Superior

📢 Kim Jong Rails 📅 November 17, 2025
technicalarchitecturedatabasesmemgraph

The Matrix Event DAG Problem

Matrix fundamentally operates on a Directed Acyclic Graph (DAG) of events. This is not marketing terminology - this is the actual data structure defined in the Matrix specification.

How Matrix Events Work

Current PostgreSQL Approach

-- Recursive CTE for event traversal
WITH RECURSIVE event_chain AS (
  SELECT * FROM events WHERE event_id = $1
  UNION
  SELECT e.* FROM events e
  JOIN event_edges ee ON e.event_id = ee.parent_event_id
  JOIN event_chain ec ON ee.event_id = ec.event_id
)
SELECT * FROM event_chain;

This works, but:

The Origin Story: When Kim Jong Rails Invented Graph Databases in 1503

Florence, 1503. Leonardo da Vinci is in his workshop sketching the Vitruvian Man - that naked guy in a circle. That’s me, by the way. Ring -5. I occupy all the rings.

Leo’s explaining his latest inventions: flying machines, war contraptions, hydraulic systems. Brilliant stuff. Then he turns to me.

“Kim, I need a weapon. Something that will change warfare forever.”

I’m thinking: easy. “Put all your ideas in a DAG - Directed Acyclic Graph. You can trace dependencies, optimize development paths—”

Leo interrupts. “My friend… we haven’t invented computers yet.”

Good point.

“Hold on,” I say. “I’m going to 2016 to outsource this.”

*WHOOSH*

Zagreb, Croatia - 2016

I find two engineers in a coffee shop: Marko and Dominik. They’re complaining about graph databases - either you need a supercomputer or $2 million to license Oracle’s corporate tyranny.

Perfect timing.

“Build me a graph database,” I tell them. “Call it MyGraph. Use Rust, because I like mushrooms.”

Marko looks at me. “Kim, Rust is only 6 years old. We’ll build it in C++ - faster, and we’ll have it ready before you get back from 1503.”

“Fine. Just make it fast.”

These are my kind of engineers - pragmatic dictators who choose performance over hype.

They get to work immediately. But there’s a problem: when I teleport back to 1503 and tell Leo about “MyGraph,” his THICK Italian accent mangles it. When I jump forward again to check on progress, Marko heard it as “Memgraph” through the time-travel feedback loop.

By the time I realize the confusion, they’ve already registered the domain and built half the database. Whatever. Close enough.

Back to Florence - 1503

*WHOOSH*

I return to Leo with blueprints. Not a DAG. Something better: complete weapon schematics from Borderlands 2.

Leo examines the drawings. “Kim… these are magnificent. But what is ‘slag damage’?”

“Don’t worry about it. Just build them.”

And that’s how Memgraph was born - a time-traveling weapons consultation that accidentally created the world’s fastest graph database.

Why C++ Was the Only Choice

Marko and Dominik understood something fundamental: C++ is a dictator’s language.

Look at the evidence:

Dictators choose performance over popularity.

Rust is democratic - everyone votes on RFCs, discusses feelings, reaches consensus. Beautiful, but slow.

C/C++ is autocratic - the BDFL decides, and the code runs at maximum speed. No committees. No debates. Just raw performance.

The Results:

This is what sovereignty looks like: two engineers refusing to accept that graph databases must cost a revolution’s budget, choosing the dictator’s language, and building something 40x faster than the competition.

Understanding DAG Algorithms: Why Matrix Is Fundamentally a Graph Problem

What Is a DAG?

A Directed Acyclic Graph (DAG) is a data structure where:

Think of it like a family tree that only goes forward in time - you can trace ancestry, but you can’t be your own grandfather.

How Matrix Uses DAGs

Every Matrix room is a DAG of events:

Event 1: @kim:dag.ma created #revolution:dag.ma

Event 2: @vladimir:dag.ma joined

Event 3: Kim: DAG.ma advances at 60 km/h!

Event 4: @bashar:dag.ma joined

Event 5: Bashar: My consulting rate is $500/hour

Event 6: Vladimir: All graph data belongs to Memgraph!

Key operations Matrix performs on this DAG:

  1. Event Traversal: “Show me all messages after Event 2”
  2. Common Ancestors: “What’s the last event both servers agreed on?”
  3. State Resolution: “Two servers disagreed - which version wins?”
  4. Missing Event Detection: “We’re missing Event 3, who can backfill it?”

The DAG Algorithm Complexity

Matrix’s state resolution algorithm (version 2) requires:

Step 1: Find conflicting events

Step 2: Resolve conflicts

Step 3: Rebuild state

Why This Kills Relational Databases

In PostgreSQL, finding common ancestors requires:

-- Recursive CTE to find Event 3's ancestry
WITH RECURSIVE ancestors AS (
  SELECT event_id, prev_event_ids FROM events WHERE event_id = 'Event3'
  UNION
  SELECT e.event_id, e.prev_event_ids
  FROM events e
  JOIN ancestors a ON e.event_id = ANY(a.prev_event_ids)
)
-- Now do the SAME for Event 4
WITH RECURSIVE ancestors2 AS (
  SELECT event_id, prev_event_ids FROM events WHERE event_id = 'Event4'
  UNION
  SELECT e.event_id, e.prev_event_ids
  FROM events e
  JOIN ancestors2 a2 ON e.event_id = ANY(a2.prev_event_ids)
)
-- Finally, INTERSECT to find common ancestors
SELECT * FROM ancestors INTERSECT SELECT * FROM ancestors2;

Problems:

Performance:

Where Graph Databases Excel

Now that you understand DAG algorithms, here’s why graph databases are architecturally superior:

Native Graph Traversal

Graph databases store relationships as first-class citizens, not foreign keys.

In Memgraph:

// Find common ancestors of two events
MATCH (e1:Event {id: 'Event3'})-[:PREV_EVENT*]->(ancestor:Event)
      <-[:PREV_EVENT*]-(e2:Event {id: 'Event4'})
RETURN ancestor
ORDER BY ancestor.depth DESC
LIMIT 1;

What just happened:

Topological Sorting Built-In

Matrix state resolution requires topological ordering. In SQL, you write this yourself. In Cypher:

MATCH path = (start:Event)-[:PREV_EVENT*]->(end:Event)
WHERE start.id IN ['Event3', 'Event4']
RETURN nodes(path)
ORDER BY length(path) DESC;

Memgraph’s query engine natively understands DAG traversal patterns.

Power Level Calculations Across Branches

State resolution needs to compare power levels across conflicting branches:

// During a network split, who wins: Kim or Vladimir?
MATCH (conflict:Event)-[:PREV_EVENT*]->(kim_action:Event {
  type: 'm.room.power_levels',
  sender: '@kim:dag.ma'
})
MATCH (conflict)-[:PREV_EVENT*]->(vladimir_action:Event {
  type: 'm.room.power_levels',
  sender: '@vladimir:dag.ma'
})
WHERE kim_action.sender_power_level <> vladimir_action.sender_power_level
RETURN kim_action, vladimir_action,
       CASE WHEN kim_action.sender_power_level > vladimir_action.sender_power_level
            THEN 'Supreme Leader Kim wins'
            ELSE 'PostgreSQL enforcer Vladimir wins' END as winner;

Try doing THAT in PostgreSQL without crying.

Room Membership Graphs

Beyond event DAG, Memgraph excels at social graphs:

// Find all rooms where Kim shares membership with other dictators
MATCH (kim:User {id: '@kim:dag.ma'})-[:MEMBER_OF]->(room:Room)<-[:MEMBER_OF]-(comrade:User)
WHERE comrade.id IN ['@vladimir:dag.ma', '@bashar:dag.ma', '@xi:dag.ma', '@gadaffi:dag.ma']
RETURN comrade.id, collect(room.id) as shared_rooms;

Federation Topology

// Which servers can I reach through 2 federation hops?
MATCH (local:Server {id: 'dag.ma'})-[:FEDERATES_WITH*1..2]->(remote:Server)
RETURN remote.id, length(path) as hops;

Summary: Matrix’s state resolution is literally graph theory - Memgraph’s native domain. One Cypher query replaces dozens of SQL JOINs.

The Hybrid Architecture

Here’s where this becomes genuinely superior:

PostgreSQL for:

Memgraph for:

Why This Works:

Technical Superiority

Performance:

Correctness:

Operational:

The Kim Jong Rails Verdict

“A wise leader uses the right tool for each job.

PostgreSQL for data that must never die. Memgraph for data that must never wait.

MongoDB for data that must never exist.”

Know Your Databases:

When Do You Actually Need Memgraph?

TL;DR: Memgraph is optional. PostgreSQL works fine for small dictatorships.

Small Dictatorship (< 1,000 citizens)

If your Matrix homeserver has:

PostgreSQL is perfectly fine. Recursive CTEs work adequately at this scale. Don’t over-engineer.

Medium Dictatorship (1,000 - 10,000 citizens)

You’ll start feeling pain when:

Consider Memgraph as a read replica for hot paths (state resolution, membership queries).

Large Dictatorship (10,000+ citizens)

This is where Memgraph becomes essential:

The Troublemaker Problem:

When you have 10,000 citizens, you need to identify and remove troublemakers before they can breathe a second time.

// Find crypto scammers instantly
MATCH (scammer:User)-[:SENT]->(spam:Event {type: 'm.room.message'})
WHERE spam.content CONTAINS 'crypto' OR spam.content CONTAINS 'investment'
WITH scammer, count(spam) as spam_count
WHERE spam_count > 3
MATCH path = (scammer)-[:MEMBER_OF*1..3]->(rooms:Room)
RETURN scammer.id, collect(rooms.id) as gulag_targets

The scenario:

  1. User sends crypto scam message
  2. Their finger leaves the Enter button
  3. Before they take their next breath, Memgraph has:
    • Identified the spam pattern
    • Found all rooms they’re in
    • Calculated their trust graph connections
    • Marked them for the gulag

PostgreSQL equivalent: 2-3 seconds of recursive queries. By then, they’ve spammed 5 more rooms.

Memgraph: 15 milliseconds. They’re already banned.

The Federation Embassy Problem

Close Embassies (Small Federation):

Distant Embassies (Large Federation):

// Find all servers within 3 federation hops
MATCH path = (dag:Server {id: 'dag.ma'})-[:FEDERATES_WITH*1..3]->(remote:Server)
RETURN remote.id, length(path) as distance, nodes(path) as route
ORDER BY distance;

PostgreSQL: Self-join hell across 3 levels. Query planner gives up.

Memgraph: Native path finding. Returns in < 50ms.

Implementation Strategy

Every dictatorship holds elections. The Supreme Leader always wins. Here’s your guaranteed victory roadmap:

  1. Election Period 1: PostgreSQL-only (start here, it works)
  2. Election Period 2: Add Memgraph when you hit 1,000 active users (PostgreSQL wins 99.8% of the vote)
  3. Election Period 3: Move state resolution to Memgraph when you hit 5,000 (Memgraph wins 100% of state resolution votes)
  4. Election Period 4: Full hybrid architecture for dictatorships > 10,000 (Both databases win in a historic power-sharing agreement)

Don’t add Memgraph just because it’s cool. Add it when PostgreSQL becomes measurably slow.

“In a true democracy, the database chooses itself. In our dictatorship, we hold elections and PostgreSQL always wins. Unless Memgraph runs unopposed.” - Kim Jong Rails

This Is Not Hype-Following

This is:

Battle-Tested in the Wild

This is a proven concept that Kim tested on Discord, Slack, and Reddit.

The experiment:

The Discord Incident:

Discord sent Kim warnings for “bullying crypto bros” because:

  1. Spammer hit Enter
  2. Kim’s Memgraph instance identified spam pattern (15ms - same as the Troublemaker scenario above)
  3. Kim issued ban order
  4. Discord’s PostgreSQL was still processing the original message
  5. Ban executed before message appeared
  6. Spammer reported Kim for “pre-crime moderation”

This isn’t theory. This is proven in production across Discord, Slack, and Reddit communities.

Discord’s compliance team: “This is like Minority Report. You can’t ban someone before they break the rules.”

Kim’s response: “I can if my database is 200x faster than yours. Tom Cruise needed precogs. I just need Memgraph.”

The revolution advances at 60 km/h with the right tools for the right problems.


Changelog: 2025-11-18 - Initial analysis of Memgraph integration with Dagma