PHP Community · War story: PHP timezone handling almost cost us a full day of billing

882
phpr/php·posted by huang_ke·yesterdayOpen source

War story: PHP timezone handling almost cost us a full day of billing

Short version: PHP needs almost no tuning at small and medium scale — the point where it starts to hurt is much further out than most people assume. Full measurements below.

Worth noting: the official docs do cover this, just in a very inconspicuous spot. I only found it reading the source comments, where the author explains the reasoning — roughly "so that it degrades into predictable behaviour in extreme cases".

Documentation first44%
Source code first28%
Just ask someone17%
Run a demo and learn by error11%

1499 votes total

105 comments

105 comments

M
Rran_bo·2 days ago

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

518
Rran_bo·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

462
Lli_ming·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

435
Ddev_zhou·28 minutes ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

422
Aalice_dev·2 days ago

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

230
Kkernel_panic·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

127
Sslow_query·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

3
Oops_wang·yesterday

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

415
Sslow_query·3 minutes ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

79
Ddev_zhou·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

79
Sslow_query·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

371
Lli_ming·2 days ago

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

364
Sswoole_lee·yesterdayedited

Saved. I am reworking this area this week — this saves a lot of wrong turns.

9
Ttang_hao·2 days agoedited

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

320
Mmike_xuOP·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

221
Sswoole_lee·3 minutes ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

265
Sswoole_lee·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

247
Oops_wang·3 minutes agoedited

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

228
Llinlin·12 minutes ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

170
Hhuang_ke·3 minutes ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

1
Ddev_zhou·1 hour ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

79
Oops_wang·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

122
Ttang_hao·1 hour ago

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

61
Oops_wang·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

241
Rran_bo·yesterday

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

5
Kkite·3 minutes ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

434
Zzhou_yi·2 days agoLevel 6

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

232
Ddev_zhouOP·2 days agoLevel 6

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

72
Rran_bo·2 days agoedited

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

424
NnikicMod·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

270
Rran_bo·2 days agoeditedLevel 6

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

91
Rrase·just now

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

44
Aalice_dev·12 minutes ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

339
Wwinter·2 days ago

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

305
Sswoole_lee·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

71
Llinlin·5 hours ago

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

53
Llinlin·2 days agoLevel 6

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

118
Bbob_chen·2 days agoLevel 6

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

93
Rran_bo·yesterdayLevel 6

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

28
Cchen_dev·2 days agoLevel 6

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

27
Llinlin·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

239
Sswoole_lee·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

3
Lli_ming·12 minutes ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

149
Wwinter·2 days ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

249
Aalice_dev·2 days agoLevel 6

Saved. I am reworking this area this week — this saves a lot of wrong turns.

256
Ddev_zhouMod·2 days agoLevel 6

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

213
Zzhu_zong·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

26
Wwinter·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

116
Aalice_dev·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

3
Zzhu_zongMod·2 days ago

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

222
Wwinter·2 days ago

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

222
Kkite·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

216
Nnikic·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

215
Sswoole_lee·just nowedited

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

204
Cchen_dev·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

190
Cchen_dev·2 days ago

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

174
Hhuang_ke·2 days ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

151
Kkernel_panic·1 hour ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

1
Rran_boMod·2 days ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

106
Oops_wang·2 hours ago

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

481
Oops_wang·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

72
Llinlin·3 minutes ago

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

66
Oops_wangMod·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

203
Kkernel_panic·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

62
Mmike_xu·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

57
Kkite·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

54
Ddev_zhou·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

46
Bbob_chen·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

33
Sswoole_lee·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

98
Sslow_query·2 days agoedited

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

28
Ttang_hao·2 days ago

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

294
Cchen_dev·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

232
Lli_ming·2 days ago

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

9
Mmike_xu·2 days ago

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

2
Hhuang_ke·3 minutes ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

32
Lli_ming·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

481
Lli_ming·2 hours ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

42
Oops_wang·2 days ago

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

30
Aalice_dev·just nowedited

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

315
Oops_wang·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

170
Kkernel_panic·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

172
Llinlin·2 days agoLevel 6

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

42
Oops_wangOP·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

51
Kkernel_panic·2 days agoLevel 6

One counter-example: below PHP 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

372
Zzhou_yi·2 days ago

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

20
Zzhu_zong·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

133
Bbob_chen·1 hour ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

7
Sslow_query·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

55
Nnikic·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

19
Wwinter·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

14
Cchen_dev·1 hour ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

9
Kkernel_panic·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

1
Nnikic·2 days agoedited

This is not a PHP problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

156
Ddev_zhou·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

1
Lli_ming·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

5
Nnikic·2 days agoedited

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

5
Kkite·2 days agoedited

I just read the PHP source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

4
Ttang_hao·5 hours ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

3
Zzhu_zong·2 days ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

492
Oops_wang·1 hour ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

1
Llinlin·3 minutes ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

425
Llinlin·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

505
Wwinter·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

348
Bbob_chenMod·1 hour ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

40
Sslow_query·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

1

This is the post detail page /en/c/php/post/p7. Posts and comments are generated deterministically from a seeded PRNG, so the same post always renders the same content and the link can be shared, reloaded and indexed. In production this page reads MySQL for the post, Redis for hot-post caching, and fetches the whole comment tree in a single query on the path column.

See the database schema →