Projections should be decoupled from the Event Stream because they are business driven while the Event Stream is purely a technical aspect.
I assume you are going to use SQL for persisting the projections to simplify the answer, but any Key/Value data store will do.
You can create a DestinationEvents
table with the following structure:
+------------------+-----------------+-------------------+
| Destination | Connections | Disconnections |
+------------------+-----------------+-------------------+
| sftp.alex.com | 3 | 1 |
| sftp.liza.com | 1 | 1 |
+------------------+-----------------+-------------------+
With proper indexing this should give both fast reads and writes. For extra speed consider something like Redis to cache your projections.
The tricky bit is in solution design, you want it to scale.
A naive approach may be to set up a SQL trigger for each write into the Event Stream, but this will slow you down if you have loads of writes.
If you want scalability, you need to start thinking about budget(time and money) and business requirements. Do the projections need to be available in real time?
- if Not, you can have a scheduled process that computes/updates projections at a certain interval: daily, hourly, weekly, etc.
- if Yes, you need to start looking into Queues/Message Brokers (RabbitMQ, Kafka, etc). Now you are entering in Producer/Consumer logic. Your App is the Producer, it publishes events. The EventStream and Projections storage are Consumers, they listen, transform and persist the incoming events. It is possible for the Queue/MessageBroker itself to replace your Event Stream table, it is easy with Kafka.
If you just want to learn, start with defining an in memory projection storage using a Dictionary<string, (int Connections, int Disconnections)>
where Destination acts as Key and (int Connections, int Disconnections)
is a tuple/class.
If you want to support other Projections, the in memory storage can be a Dictionary<string, Dictionary<string, (int Connections, int Disconnections)>>
where the outer dictionary Key is the Projection name.