|
| 1 | +--- |
| 2 | +title: Kafkaesque Internet |
| 3 | +description: The Internet is broken. |
| 4 | +date: Oct 02, 2021 |
| 5 | +tags: [ internet, streams, logs ] |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +# Kafkaesque Internet |
| 10 | + |
| 11 | +> **DISCLAIMER**: this is an article about the internet from purely |
| 12 | +> technological point of view. |
| 13 | +
|
| 14 | +## What's the internet |
| 15 | + |
| 16 | +When we talk the Internet, we really mean the web. |
| 17 | +And web is really just HTTP. |
| 18 | + |
| 19 | +## How it works |
| 20 | + |
| 21 | +1. Send `OPTIONS` to see available methods |
| 22 | +2. `GET` to retrieve content |
| 23 | +3. `PUT` to add content |
| 24 | +4. `POST` can update or do anything |
| 25 | + |
| 26 | +Except that's not how it works at all. |
| 27 | + |
| 28 | +## How it actually works |
| 29 | + |
| 30 | +Pages update asynchronously. |
| 31 | +Content comes from multiple sources. |
| 32 | +Users submit multiple actions concurrently. |
| 33 | +Analytics are collected about everything. |
| 34 | +Content is generated on demand. |
| 35 | + |
| 36 | +And no one wants to see it fail or worse freeze while loading. |
| 37 | +And we would like everything to work offline. |
| 38 | + |
| 39 | +## Intermission : GraphQL |
| 40 | + |
| 41 | +A nice real world example of a real world project recognizing the issues |
| 42 | +of pulling content from multiple sources in various formats is GraphQL. |
| 43 | + |
| 44 | +Send a single query describing the data you want. |
| 45 | +Get it the way you want on a best effort basis. |
| 46 | +(Even across multiple responses based on the priority.) |
| 47 | + |
| 48 | +Updates are iffy, because we want to push the user actions to the server |
| 49 | +ASAP, not wait and batch them. |
| 50 | +Also can still fail... |
| 51 | + |
| 52 | +## Eventual consistency required eventual delivery |
| 53 | + |
| 54 | +To avoid failure, let's log every action locally before trying to |
| 55 | +execute it. |
| 56 | +Write-ahead logs are a very elegant solution from the database world. |
| 57 | + |
| 58 | +1. Log the user action to a local persistent log. |
| 59 | +2. Materialize the change into the local view. |
| 60 | +3. At convenience / when available, sync the log with the server. |
| 61 | + |
| 62 | +When we synchronize the logs we make them available for other devices to |
| 63 | +pull and materialize the collected actions locally themselves. |
| 64 | +Careful about conflicts -- we need to be deliberate about the way we |
| 65 | +handle conflicts. |
| 66 | + |
| 67 | +Conflict resolution is very application specific and requires careful |
| 68 | +planning. |
| 69 | +Sometimes simple last write wins can work just fine, sometimes we need |
| 70 | +to design the actions to not create conflicts (CRDTS), and sometimes we |
| 71 | +need to be very granular and have multiple resolution strategies. |
| 72 | +More about that some other time. |
| 73 | + |
| 74 | +## Globally Persistent Connections |
| 75 | + |
| 76 | +A `User` can have multiple `Connection`s -- one per client. |
| 77 | +`Connection` can be `active` or `inactive`. |
| 78 | +`Active Connection` has a reserved data channel at the `Server` and will get |
| 79 | +an update whenever connected. |
| 80 | +An `active Connection` can be downgraded to `inactive` by the `Server`. |
| 81 | +`Inactive Connection` will not get updated but overwritten by a clone of |
| 82 | +an `active Connection` when connected to the `Server`. |
| 83 | + |
| 84 | +Setting resource limits is necessary to guarantee quality of service. |
| 85 | + |
| 86 | +We can be smart about transitioning `Connection`s to inactive though. |
| 87 | + |
| 88 | +## What do we get? |
| 89 | + |
| 90 | +More like what do we not get? |
| 91 | + |
| 92 | +- No errors! (just delays) |
| 93 | +- No contradictions in state (if we materialize correctly) |
| 94 | +- Full history (if we keep the logs) |
| 95 | +- User authentication (partially, through the logs; still need initial) |
| 96 | + |
| 97 | +All of that for free, just by using persistent connections. |
| 98 | +And remember: |
| 99 | + |
| 100 | +> Persistent connections are just write ahead logs. |
| 101 | +
|
| 102 | +## Honorable mentions |
| 103 | + |
| 104 | +### Kafka |
| 105 | + |
| 106 | +Can we run it in browser? |
| 107 | + |
| 108 | +### ZeroMQ |
| 109 | + |
| 110 | +Very cool project providing good abstractions over basic connections. |
| 111 | +Not quite new Internet, just Internet on steroids. Still cool though. |
0 commit comments