@@ -297,6 +297,69 @@ defmodule MyApp.Events.User do
297
297
end
298
298
```
299
299
300
+ ### Advanced usages
301
+
302
+ ##### Durable slot
303
+
304
+ By default WalEx will create a temporary replication slot in Postgres.
305
+
306
+ This means that if the connection between WalEx and Postgres gets interrupted (crash / disconnection / etc.),
307
+ the replication slot will get dropped by Postgres.
308
+ This makes using WalEx safer as there is not risk of filling up the disk of the Postgres writer instance in
309
+ case of downtime.
310
+
311
+ The downside being that this event-loss is more than likely.
312
+ If this is a no-go, WalEx also supports durable replication.
313
+
314
+ ``` elixir
315
+ # config.exs
316
+
317
+ config :my_app , WalEx ,
318
+ # ...
319
+ durable_slot: true ,
320
+ slot_name: my_app_replication_slot
321
+ ```
322
+
323
+ Only a single process can be connected to a durable slot at once,
324
+ in case the slot is already used ` WalEx.Supervisor ` will fail to start with a ` RuntimeError ` .
325
+
326
+ Be warned that there are many additional potential gotchas (a detailed guide is planned).
327
+
328
+ ##### Event middleware / Back-pressure
329
+
330
+ WalEx receives events from Postgres in ` WalEx.Replication.Server ` and then ` cast ` those to ` WalEx.Replication.Publisher ` .
331
+ It's then ` WalEx.Replication.Publisher ` that is responsible to join these events together and process them.
332
+
333
+ In the event where you'd expect Postgres to overwhelm WalEx and potentially cause OOMs,
334
+ WalEx provides a config option that should help you implement back-pressure.
335
+
336
+ As it's a quite advanced use, with many strong requirements,
337
+ it's recommended instead to increase the amount of RAM of your instance.
338
+
339
+ Never the less, if it's not an option or would like to control the consumption rate of events,
340
+ WalEx provide the following configuration option:
341
+
342
+ ```
343
+ config :my_app, WalEx,
344
+ # ...
345
+ message_middleware: fn message, app_name -> ... end
346
+ ```
347
+
348
+ ` message_middleware ` allows you to define the way ` WalEx.Replication.Server ` and ` WalEx.Replication.Publisher ` communicate.
349
+
350
+ If for instance you'd like to store these events to disk before processing them you would need to:
351
+
352
+ - provide a ` message_middleware ` callback. It should serialize messages and store them to disk
353
+ - add a supervised strictly-ordered disk consumer. On each event it would call one of:
354
+ - ` WalEx.Replication.Publisher.process_message_async(message, app_name) `
355
+ - ` WalEx.Replication.Publisher.process_message_sync(message, app_name) `
356
+
357
+ Any back-pressure implementation needs to guarantee:
358
+
359
+ - exact message ordering
360
+ - exactly-once-delivery
361
+ - that each running walex has an isolated back-pressure system (for instance one queue per instance)
362
+
300
363
## Test
301
364
302
365
You'll need a local Postgres instance running
0 commit comments