# Bucket index shards and dynamic reshard

*HTML: https://book.ceph-s3.ir/en/lab-reshard.html*
*Licence: CC BY-NC-SA 4.0. Author: Ramin Najarbashi.*

[Interactive lab](lab.html)Bucket index shards and dynamic reshard
In the book: [Chapter 4](04-object-storage-rgw.html#sec-04-10-2)
LabBucket index shards and dynamic reshardGrow a bucket to millions of objects and see when dynamic reshard splits it and into how many shards, how keys spread over the shards, and how many entries one page of an ordered listing reads from them.**521**shards now**5**reshards so far**57.6k**objects per shard**8 × 521**entries read for a page of 1000Shard count (vertical, log) as the bucket grows (horizontal, log). Each step is a reshard and the number next to it the new shard count; the vertical line is the bucket's current size.#at objectsshardskind11.2M11 → **29**expansion23M29 → **61**expansion36.2M61 → **127**expansion412.8M127 → **257**expansion525.8M257 → **521**expansion2000 sample keys over 521 shards: 0 keys in the emptiest shard and 10 in the fullest.expand if objects > shards × 100000 · reduce if shards > 11 and objects < shards × 1000 new = clamp(⌊objects × 2 / 100000⌋, 11, 1999) → next prime in reshard_primes · ordered listing reads max(8, 1 + ⌊n/s⌋ + √(2n·ln s / s)) = 8 entries from each of 521 shardsEach reshard rewrites every index entry and, in multisite, starts a new bilog generation. Many shards in turn make ordered listings expensive: with 1999 shards every page sends 1999 cls calls. For listings that need no order use allow-unordered (chapter 4).Follows:`src/rgw/driver/rados/rgw_reshard.cc :: RGWBucketReshard::calculate_preferred_shards, nearest_prime, reshard_primes` the expansion and reduction tests, the factor 2 or 8 and rounding to a prime`src/rgw/driver/rados/rgw_rados.cc :: RGWRados::calculate_preferred_shards, check_bucket_shards; src/rgw/rgw_quota.cc :: RGWOwnerStatsCache::sync_bucket` the threshold divided by 3 for versioned buckets, and the multisite test`src/rgw/driver/rados/rgw_rados.cc :: RGWRados::calc_ordered_bucket_list_per_shard` the entries read from each shard in an ordered listing`src/rgw/services/svc_bi_rados.h :: RGWSI_BucketIndex_RADOS::bucket_shard_index; src/common/ceph_hash.cc :: ceph_str_hash_linux` the shard of each key`src/common/options/rgw.yaml.in :: rgw_max_objs_per_shard (100000), rgw_max_dynamic_shards (1999), rgw_dynamic_resharding_reduction_wait (120)` the defaultsA fixed snapshot of the simulator; the interactive version is in the web edition's lab. [Interactive lab: Bucket index shards and dynamic reshard](lab-reshard.html)The interactive version needs JavaScript; this is a fixed snapshot.
