-

@ Gzuuus
2025-05-09 09:30:31
Hey, how's it going? I'm going to share some stuff I was working on this week. I started playing with Rely, the relay framework by nostr:nprofile1qqs0dqlgwq6l0t20gnstnr8mm9fhu9j9t2fv6wxwl3xtx8dh24l4auswr6u0j which is pretty good and lightweight. It's also super customizable, which means you have to create your own logic to handle events and filters.
I was wanting to find the best way to handle ephemeral events, which are events that are supposed to just be relayed and not stored. I realized that a time-to-live feature would be beneficial to make them more robust, as they could be found in relays during a specific period of time, like 5 minutes for example.
While researching ways to implement this, I realized that having a TTL would most likely introduce overhead in operations. So I decided to go in a different direction using circular or ring buffers, which are very interesting for the ephemeral relay use case. Since the buffer has a fixed size, when the maximum size is reached, it wraps around and starts to overwrite the oldest events. In this way, the memory allocations are more predictable, which results in more performant code.
Also, using a ring buffer means that the time events will live in the relay and can be queried is more unpredictable. There isn't a defined time they must be stored, they just stay there until their time arrives and they're overwritten by a new incoming event. From my point of view, this is a totally fine drawback since ephemeral events are just supposed to be relayed, but this adds a layer of robustness as the events could be found stored in the buffer after their publication.
With this in mind, I coded different versions of the ring buffer, iterating over it and benchmarking the different implementations. I ended up with an atomic ring buffer implementation that's very performant and lightweight, thanks to nostr:nprofile1qqs0dqlgwq6l0t20gnstnr8mm9fhu9j9t2fv6wxwl3xtx8dh24l4auswr6u0j as well for the suggestions for the code and pointing out a race condition, it was fixed. Now the code is available here https://github.com/gzuuus/rely-lab with the results of the benchmarks.
This was an interesting research project, and I'll be implementing this for the relay behind dvmcpfun as the new spec is based mainly on ephemeral events. I'll probably also create a Go library similar to eventStore to make it easy for others to use this atomic ring buffer implementation for relay storage, you can use it for any event kind if you want not just ephemeral.
A part of this, I made significant progress in refactoring the DVMCP packages. The bridge is almost finished and the new specification have been implemented. However, refactoring tasks are not as exciting to talk about as the research into ring buffers.