|
| 1 | +# Segmentation Service |
| 2 | + |
| 3 | +Independent NestJS microservice that manages user cohorts for targeted campaigns, |
| 4 | +A/B testing, and personalization inside the Quest Service platform. |
| 5 | + |
| 6 | +## Highlights |
| 7 | + |
| 8 | +- **Segments** defined declaratively by ordered rule groups (AND / OR combinators). |
| 9 | +- **Rule-based**, **behavioral**, and **demographic** segmentation engines. |
| 10 | +- **Real-time membership updates** through an event feed (signals + scheduled |
| 11 | + evaluations + Redis cache invalidation). |
| 12 | +- **Overlap analysis** between two or more segments and a flexible **size metric** |
| 13 | + stream for dashboards. |
| 14 | +- **A/B Experiment assignments** with deterministic hashing, sticky variants, and |
| 15 | + configurable traffic splits. |
| 16 | +- **Docker** support (Postgres + Redis + service) with one-command bring-up. |
| 17 | +- **Independent runtime**: the service can be deployed, scaled, and tested in |
| 18 | + isolation from the rest of the platform. |
| 19 | + |
| 20 | +## Project Layout |
| 21 | + |
| 22 | +``` |
| 23 | +microservices/segmentation-service/ |
| 24 | +├── Dockerfile |
| 25 | +├── docker-compose.yml |
| 26 | +├── nest-cli.json |
| 27 | +├── package.json |
| 28 | +├── tsconfig.json |
| 29 | +├── tsconfig.build.json |
| 30 | +├── .env.example |
| 31 | +├── .eslintrc.cjs |
| 32 | +├── .prettierrc |
| 33 | +├── .gitignore |
| 34 | +└── src/ |
| 35 | + ├── main.ts |
| 36 | + ├── app.module.ts |
| 37 | + ├── app.controller.ts |
| 38 | + └── segmentation/ |
| 39 | + ├── segmentation.module.ts |
| 40 | + ├── segmentation.controller.ts |
| 41 | + ├── segmentation.service.ts |
| 42 | + ├── segmentation-rule-engine.service.ts |
| 43 | + ├── segmentation.scheduler.ts |
| 44 | + ├── redis-cache.service.ts |
| 45 | + ├── entities/ |
| 46 | + │ ├── segment.entity.ts |
| 47 | + │ ├── rule.entity.ts |
| 48 | + │ ├── membership.entity.ts |
| 49 | + │ ├── segment-event.entity.ts |
| 50 | + │ └── ab-experiment.entity.ts |
| 51 | + ├── dto/ |
| 52 | + │ ├── create-segment.dto.ts |
| 53 | + │ ├── update-segment.dto.ts |
| 54 | + │ ├── create-rule.dto.ts |
| 55 | + │ ├── ingest-user-event.dto.ts |
| 56 | + │ ├── evaluate-segment.dto.ts |
| 57 | + │ ├── create-experiment.dto.ts |
| 58 | + │ └── assign-experiment.dto.ts |
| 59 | + └── interfaces/ |
| 60 | + └── user-signal.interface.ts |
| 61 | +``` |
| 62 | + |
| 63 | +## Run Locally |
| 64 | + |
| 65 | +```bash |
| 66 | +cp .env.example .env |
| 67 | +npm install |
| 68 | +npm run start:dev |
| 69 | +``` |
| 70 | + |
| 71 | +Or bring the service up together with Postgres and Redis: |
| 72 | + |
| 73 | +```bash |
| 74 | +docker compose up --build |
| 75 | +``` |
| 76 | + |
| 77 | +## Useful Endpoints |
| 78 | + |
| 79 | +| Method | Path | Purpose | |
| 80 | +| ------ | --------------------------------------- | -------------------------------------- | |
| 81 | +| GET | `/api/health` | Liveness probe | |
| 82 | +| GET | `/api/segments` | List every segment | |
| 83 | +| POST | `/api/segments` | Create a segment (with inline rules) | |
| 84 | +| GET | `/api/segments/:id` | Fetch a single segment with rules | |
| 85 | +| PATCH | `/api/segments/:id` | Update segment metadata / status | |
| 86 | +| DELETE | `/api/segments/:id` | Archive / delete a segment | |
| 87 | +| POST | `/api/segments/:id/rules` | Append a new rule to a segment | |
| 88 | +| DELETE | `/api/segments/:id/rules/:ruleId` | Remove a rule from a segment | |
| 89 | +| POST | `/api/segments/:id/evaluate` | Force a re-evaluation of members | |
| 90 | +| POST | `/api/segments/:id/membership` | Manually add / remove members | |
| 91 | +| GET | `/api/segments/:id/members` | List current members of a segment | |
| 92 | +| GET | `/api/segments/:id/size` | Read cached size + last computed time | |
| 93 | +| POST | `/api/segments/:id/check/:userId` | Check whether a user matches a segment | |
| 94 | +| POST | `/api/events` | Send a behavioral signal | |
| 95 | +| POST | `/api/segments/overlap` | Compute overlap between segment ids | |
| 96 | +| POST | `/api/experiments` | Create a new A/B experiment | |
| 97 | +| POST | `/api/experiments/:id/assign` | Assign (and cache) a variant for user | |
| 98 | +| GET | `/api/dashboard` | Service-wide summary | |
| 99 | + |
| 100 | +## Rule Schema |
| 101 | + |
| 102 | +```jsonc |
| 103 | +{ |
| 104 | + "field": "country", |
| 105 | + "operator": "equals", |
| 106 | + "value": "US", |
| 107 | + "combinator": "AND", |
| 108 | + "order": 0 |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Operators: `equals`, `notEquals`, `in`, `notIn`, `contains`, `notContains`, |
| 113 | +`gt`, `gte`, `lt`, `lte`, `between`, `exists`, `notExists`, `regex`. |
| 114 | + |
| 115 | +Behavioral fields: `action`, `eventCount`, `lastEventAt`, `totalSpend`, |
| 116 | +`level`, `xp`, `streak`, `consecutiveDays`. They are evaluated against the |
| 117 | +freshest user signal stored under `segmentation:user:{userId}` in Redis. |
| 118 | + |
| 119 | +## Tests |
| 120 | + |
| 121 | +```bash |
| 122 | +npm run lint:check |
| 123 | +npm run type-check |
| 124 | +npm run test |
| 125 | +``` |
| 126 | + |
| 127 | +## Docker Build |
| 128 | + |
| 129 | +```bash |
| 130 | +docker build -t segmentation-service . |
| 131 | +docker run --rm -p 3023:3023 segmentation-service |
| 132 | +``` |
0 commit comments