Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Write person ID and properties to redis #9377

Merged
merged 7 commits into from
Apr 13, 2022
Merged

Conversation

tiina303
Copy link
Contributor

@tiina303 tiina303 commented Apr 10, 2022

Problem

For #9179

Storing person info in redis:

  1. teamId, distinctId -> personId
  2. teamId, personId -> createdAt
  3. teamId, personId -> properties

Changes

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

How did you test this code?

Ran locally with PERSON_INFO_TO_REDIS_TEAMS="1,2,3,4,5" ./bin/start
Sent an event with posthog python >>> posthog.capture('test-id3', 'test-event', properties={ '$set': { 'userProperty': 16 } })
Verified that info was in Redis as expected:

$ redis-cli
>  keys person*
1) "person_props:1:5"
2) "person_create_at:1:5"
3) "person_id:1:test-id3"
127.0.0.1:6379> keys person*
1) "person_props:1:5"
2) "person_create_at:1:5"
3) "person_id:1:test-id3"
127.0.0.1:6379> get person_props:1:5
"\"{\\\"$initial_geoip_city_name\\\":\\\"Sydney\\\",\\\"$initial_geoip_country_name\\\":\\\"Australia\\\",\\\"$initial_geoip_country_code\\\":\\\"AU\\\",\\\"$initial_geoip_continent_name\\\":\\\"Oceania\\\",\\\"$initial_geoip_continent_code\\\":\\\"OC\\\",\\\"$initial_geoip_postal_code\\\":\\\"1148\\\",\\\"$initial_geoip_latitude\\\":-33.8715,\\\"$initial_geoip_longitude\\\":151.2006,\\\"$initial_geoip_time_zone\\\":\\\"Australia/Sydney\\\",\\\"$initial_geoip_subdivision_1_code\\\":\\\"NSW\\\",\\\"$initial_geoip_subdivision_1_name\\\":\\\"New South Wales\\\",\\\"userProperty\\\":16,\\\"$geoip_city_name\\\":\\\"Sydney\\\",\\\"$geoip_country_name\\\":\\\"Australia\\\",\\\"$geoip_country_code\\\":\\\"AU\\\",\\\"$geoip_continent_name\\\":\\\"Oceania\\\",\\\"$geoip_continent_code\\\":\\\"OC\\\",\\\"$geoip_postal_code\\\":\\\"1148\\\",\\\"$geoip_latitude\\\":-33.8715,\\\"$geoip_longitude\\\":151.2006,\\\"$geoip_time_zone\\\":\\\"Australia/Sydney\\\",\\\"$geoip_subdivision_1_code\\\":\\\"NSW\\\",\\\"$geoip_subdivision_1_name\\\":\\\"New South Wales\\\"}\""
127.0.0.1:6379> get person_create_at:1:5
"\"2022-04-12T13:24:00.825Z\""
127.0.0.1:6379> get person_id:1:test-id3
"5"

Changed the userProperty with py >>> posthog.capture('test-id3', 'test-event', properties={ '$set': { 'userProperty': 334 } })
In redis:

127.0.0.1:6379> get person_props:1:5
"\"{\\\"userProperty\\\":334,\\\"$geoip_latitude\\\":-33.8715,\\\"$geoip_city_name\\\":\\\"Sydney\\\",\\\"$geoip_longitude\\\":151.2006,\\\"$geoip_time_zone\\\":\\\"Australia/Sydney\\\",\\\"$geoip_postal_code\\\":\\\"1148\\\",\\\"$geoip_country_code\\\":\\\"AU\\\",\\\"$geoip_country_name\\\":\\\"Australia\\\",\\\"$geoip_continent_code\\\":\\\"OC\\\",\\\"$geoip_continent_name\\\":\\\"Oceania\\\",\\\"$initial_geoip_latitude\\\":-33.8715,\\\"$initial_geoip_city_name\\\":\\\"Sydney\\\",\\\"$initial_geoip_longitude\\\":151.2006,\\\"$initial_geoip_time_zone\\\":\\\"Australia/Sydney\\\",\\\"$geoip_subdivision_1_code\\\":\\\"NSW\\\",\\\"$geoip_subdivision_1_name\\\":\\\"New South Wales\\\",\\\"$initial_geoip_postal_code\\\":\\\"1148\\\",\\\"$initial_geoip_country_code\\\":\\\"AU\\\",\\\"$initial_geoip_country_name\\\":\\\"Australia\\\",\\\"$initial_geoip_continent_code\\\":\\\"OC\\\",\\\"$initial_geoip_continent_name\\\":\\\"Oceania\\\",\\\"$initial_geoip_subdivision_1_code\\\":\\\"NSW\\\",\\\"$initial_geoip_subdivision_1_name\\\":\\\"New South Wales\\\"}\""

Added second user and merged

>>> posthog.capture('test-id4', 'test-event', properties={ '$set': { 'userProperty': 600 } })
>>> posthog.alias('test-id3', 'test-id4')

redis. Note that we didn't expire the old props and created_at, but that personId isn't used anymore.

> keys person*
1) "person_props:1:5"
2) "person_create_at:1:5"
3) "person_id:1:test-id4"
4) "person_props:1:6"
5) "person_id:1:test-id3"
6) "person_create_at:1:6"
> get person_id:1:test-id4
"5"
127.0.0.1:6379> get person_id:1:test-id3
"5"
9> get person_props:1:5
"\"{\\\"userProperty\\\":334,\\\"$geoip_latitude\\\":-33.8715,\\\"$geoip_city_name\\\":\\\"Sydney\\\",\\\"$geoip_longitude\\\":151.2006,\\\"$geoip_time_zone\\\":\\\"Australia/Sydney\\\",\\\"$geoip_postal_code\\\":\\\"1148\\\",\\\"$geoip_country_code\\\":\\\"AU\\\",\\\"$geoip_country_name\\\":\\\"Australia\\\",\\\"$geoip_continent_code\\\":\\\"OC\\\",\\\"$geoip_continent_name\\\":\\\"Oceania\\\",\\\"$initial_geoip_latitude\\\":-33.8715,\\\"$initial_geoip_city_name\\\":\\\"Sydney\\\",\\\"$initial_geoip_longitude\\\":151.2006,\\\"$initial_geoip_time_zone\\\":\\\"Australia/Sydney\\\",\\\"$geoip_subdivision_1_code\\\":\\\"NSW\\\",\\\"$geoip_subdivision_1_name\\\":\\\"New South Wales\\\",\\\"$initial_geoip_postal_code\\\":\\\"1148\\\",\\\"$initial_geoip_country_code\\\":\\\"AU\\\",\\\"$initial_geoip_country_name\\\":\\\"Australia\\\",\\\"$initial_geoip_continent_code\\\":\\\"OC\\\",\\\"$initial_geoip_continent_name\\\":\\\"Oceania\\\",\\\"$initial_geoip_subdivision_1_code\\\":\\\"NSW\\\",\\\"$initial_geoip_subdivision_1_name\\\":\\\"New South Wales\\\"}\""

@tiina303 tiina303 marked this pull request as draft April 10, 2022 16:49
@yakkomajuri
Copy link
Contributor

Thanks for submitting the WIP PR, appreciate the visibility into the work

Copy link
Contributor

@yakkomajuri yakkomajuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. One comment that the buffer depends on this work, and it will use the person created_at field to determine what events should be sent there.

Could you (either in this PR or the next) add a cache for created_at (either as its own thing or bundled in the props by caching a larger subset of "person" instead of just props at the top level).

plugin-server/src/utils/db/db.ts Outdated Show resolved Hide resolved
plugin-server/src/utils/db/db.ts Outdated Show resolved Hide resolved
plugin-server/src/utils/db/db.ts Outdated Show resolved Hide resolved
@tiina303 tiina303 marked this pull request as ready for review April 12, 2022 14:28
Copy link
Contributor

@yakkomajuri yakkomajuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good - a few nits.

also, can we add a metric for each of the cache functions? redisSet already has metrics but I'd love to see a breakdown across each of the operations more clearly

plugin-server/src/utils/db/db.ts Outdated Show resolved Hide resolved
plugin-server/src/utils/db/db.ts Outdated Show resolved Hide resolved
plugin-server/src/utils/db/db.ts Show resolved Hide resolved
@tiina303 tiina303 force-pushed the poe-write-to-redis branch from 5ca62a2 to f5dbbe4 Compare April 12, 2022 21:32
@tiina303
Copy link
Contributor Author

also, can we add a metric for each of the cache functions? redisSet already has metrics but I'd love to see a breakdown across each of the operations more clearly

They are all just calling redisSet directly are you predicting it to be slower for setting properties or what would you use it for? That said this could be good for the fetching part, which might end up going to DB and updating the cache. - we'd want to know the cache misses there etc.

constructor(
postgres: Pool,
redisPool: GenericPool<Redis.Redis>,
kafkaProducer: KafkaProducerWrapper | undefined,
clickhouse: ClickHouse | undefined,
statsd: StatsD | undefined
statsd: StatsD | undefined,
personInfoCacheTtl = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be best to have this to the same default as the config but sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set it like that on purpose to expire fast if not set.

@yakkomajuri yakkomajuri merged commit 38bf3ef into master Apr 13, 2022
@yakkomajuri yakkomajuri deleted the poe-write-to-redis branch April 13, 2022 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants